Skip to content

Commit 910c9e2

Browse files
authored
[MPLUGIN-386] Exclude maven-archiver and maven-jxr from warning (#58)
Alter original too strict code to count on two exceptions (archiver and jxr) but also make it extensible and also possible to shut it off completely.
1 parent 70469f3 commit 910c9e2

File tree

4 files changed

+62
-23
lines changed

4 files changed

+62
-23
lines changed

maven-plugin-plugin/src/it/mplugin-370-maven-deps-scope-bad/verify.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@
1919

2020
File buildLog = new File( basedir, "build.log" );
2121
assert buildLog.isFile()
22-
assert buildLog.text.contains( "Maven dependencies of Maven Plugins should be in provided scope." )
22+
assert buildLog.text.contains( "Some dependencies of Maven Plugins are expected to be in provided scope." )

maven-plugin-plugin/src/it/mplugin-370-maven-deps-scope-good/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ under the License.
3838
</properties>
3939

4040
<dependencies>
41+
<dependency>
42+
<groupId>org.apache.maven</groupId>
43+
<artifactId>maven-jxr</artifactId>
44+
<version>3.1.1</version>
45+
<scope>compile</scope>
46+
</dependency>
4147
<dependency>
4248
<groupId>org.apache.maven</groupId>
4349
<artifactId>maven-plugin-api</artifactId>

maven-plugin-plugin/src/it/mplugin-370-maven-deps-scope-good/verify.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@
1919

2020
File buildLog = new File( basedir, "build.log" );
2121
assert buildLog.isFile()
22-
assert !buildLog.text.contains( "Maven dependencies of Maven Plugins should be in provided scope." )
22+
assert !buildLog.text.contains( "Some dependencies of Maven Plugins are expected to be in provided scope." )

maven-plugin-plugin/src/main/java/org/apache/maven/plugin/plugin/AbstractGeneratorMojo.java

Lines changed: 54 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343

4444
import java.io.File;
4545
import java.util.Arrays;
46+
import java.util.Collections;
4647
import java.util.LinkedHashSet;
4748
import java.util.List;
4849
import java.util.Set;
@@ -139,7 +140,7 @@ public abstract class AbstractGeneratorMojo
139140
* @since 3.5
140141
*/
141142
@Parameter
142-
private List<String> mojoDependencies;
143+
private List<String> mojoDependencies = null;
143144

144145
/**
145146
* List of Remote Repositories used by the resolver
@@ -163,7 +164,36 @@ public abstract class AbstractGeneratorMojo
163164
* @since 3.3
164165
*/
165166
@Parameter
166-
protected List<String> packagingTypes = Arrays.asList( "maven-plugin" );
167+
protected List<String> packagingTypes = Collections.singletonList( "maven-plugin" );
168+
169+
/**
170+
* Flag controlling is "expected dependencies in provided scope" check to be performed or not. Default value:
171+
* {@code true}.
172+
*
173+
* @since 3.6.3
174+
*/
175+
@Parameter( defaultValue = "true", property = "maven.plugin.checkExpectedProvidedScope" )
176+
private boolean checkExpectedProvidedScope = true;
177+
178+
/**
179+
* List of {@code groupId} strings of artifact coordinates that are expected to be in "provided" scope. Default
180+
* value: {@code ["org.apache.maven"]}.
181+
*
182+
* @since 3.6.3
183+
*/
184+
@Parameter
185+
private List<String> expectedProvidedScopeGroupIds = Collections.singletonList( "org.apache.maven" );
186+
187+
/**
188+
* List of {@code groupId:artifactId} strings of artifact coordinates that are to be excluded from "expected
189+
* provided scope" check. Default value: {@code ["org.apache.maven:maven-archiver", "org.apache.maven:maven-jxr"]}.
190+
*
191+
* @since 3.6.3
192+
*/
193+
@Parameter
194+
private List<String> expectedProvidedScopeExclusions = Arrays.asList(
195+
"org.apache.maven:maven-archiver",
196+
"org.apache.maven:maven-jxr" );
167197

168198
/**
169199
* @return the output directory where files will be generated.
@@ -209,23 +239,25 @@ public void execute()
209239
+ "In the future this error will break the build." + LS + LS );
210240
}
211241

212-
Set<Artifact> wrongScopedArtifacts = mavenDependenciesNotInProvidedScope();
213-
if ( !wrongScopedArtifacts.isEmpty() )
242+
if ( checkExpectedProvidedScope )
214243
{
215-
StringBuilder errorMessage = new StringBuilder(
216-
LS + LS + "Maven dependencies of Maven Plugins should be in provided scope." + LS
217-
+ "Please make sure that all your dependencies declared in POM whose group ID is" + LS
218-
+ "org.apache.maven have set '<scope>provided</scope>' as well." + LS
219-
+ "In the future this error will break the build." + LS + LS
220-
+ "The following dependencies are in wrong scope:" + LS
221-
);
222-
for ( Artifact artifact : wrongScopedArtifacts )
244+
Set<Artifact> wrongScopedArtifacts = dependenciesNotInProvidedScope();
245+
if ( !wrongScopedArtifacts.isEmpty() )
223246
{
224-
errorMessage.append( " * " ).append( artifact ).append( LS );
225-
}
226-
errorMessage.append( LS ).append( "Please fix your build!" ).append( LS ).append( LS );
247+
StringBuilder errorMessage = new StringBuilder(
248+
LS + LS + "Some dependencies of Maven Plugins are expected to be in provided scope." + LS
249+
+ "Please make sure that dependencies listed below declared in POM" + LS
250+
+ "have set '<scope>provided</scope>' as well." + LS + LS
251+
+ "The following dependencies are in wrong scope:" + LS
252+
);
253+
for ( Artifact artifact : wrongScopedArtifacts )
254+
{
255+
errorMessage.append( " * " ).append( artifact ).append( LS );
256+
}
257+
errorMessage.append( LS ).append( LS );
227258

228-
getLog().error( errorMessage.toString() );
259+
getLog().error( errorMessage.toString() );
260+
}
229261
}
230262

231263
String defaultGoalPrefix = getDefaultGoalPrefix( project );
@@ -294,7 +326,7 @@ else if ( !goalPrefix.equals( defaultGoalPrefix ) )
294326
}
295327
catch ( InvalidPluginDescriptorException | ExtractionException e )
296328
{
297-
throw new MojoExecutionException( "Error extracting plugin descriptor: \'" + e.getLocalizedMessage() + "\'",
329+
throw new MojoExecutionException( "Error extracting plugin descriptor: '" + e.getLocalizedMessage() + "'",
298330
e );
299331
}
300332
catch ( LinkageError e )
@@ -319,16 +351,17 @@ static String getDefaultGoalPrefix( MavenProject project )
319351
}
320352

321353
/**
322-
* Collects all dependencies having {@code org.apache.maven} group ID that are NOT in provided scope.
354+
* Collects all dependencies expected to be in "provided" scope but are NOT in "provided" scope.
323355
*/
324-
private Set<Artifact> mavenDependenciesNotInProvidedScope()
356+
private Set<Artifact> dependenciesNotInProvidedScope()
325357
{
326358
LinkedHashSet<Artifact> wrongScopedDependencies = new LinkedHashSet<>();
327359

328360
for ( Artifact dependency : project.getArtifacts() )
329361
{
330-
if ( "org.apache.maven".equals( dependency.getGroupId() )
331-
&& dependency.getArtifactId().startsWith( "maven-" )
362+
String ga = dependency.getGroupId() + ":" + dependency.getArtifactId();
363+
if ( expectedProvidedScopeGroupIds.contains( dependency.getGroupId() )
364+
&& !expectedProvidedScopeExclusions.contains( ga )
332365
&& !Artifact.SCOPE_PROVIDED.equals( dependency.getScope() ) )
333366
{
334367
wrongScopedDependencies.add( dependency );

0 commit comments

Comments
 (0)