Skip to content

Commit f9a90e5

Browse files
andrzejj0slawekjaranowski
authored andcommitted
Amended #258: Adding plugin and plugin management dependency includes/excludes
1 parent 5fa3693 commit f9a90e5

File tree

11 files changed

+264
-16
lines changed

11 files changed

+264
-16
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
invoker.goals.1=${project.groupId}:${project.artifactId}:${project.version}:display-dependency-updates -Dverbose=true -Dversions.outputFile=./output1.txt -DoutputEncoding=UTF-8 -DprocessPluginDependenciesInPluginManagement=false -DpluginDependencyIncludes=localhost:dummy-api
2+
3+
invoker.goals.2=${project.groupId}:${project.artifactId}:${project.version}:display-dependency-updates -Dverbose=true -Dversions.outputFile=./output2.txt -DoutputEncoding=UTF-8 -DprocessPluginDependenciesInPluginManagement=false -DpluginDependencyExcludes=localhost:dummy-api
4+
5+
invoker.goals.3=${project.groupId}:${project.artifactId}:${project.version}:display-dependency-updates -Dverbose=true -Dversions.outputFile=./output3.txt -DoutputEncoding=UTF-8 -DprocessPluginDependencies=false -DpluginManagementDependencyIncludes=localhost:dummy-impl
6+
7+
invoker.goals.4=${project.groupId}:${project.artifactId}:${project.version}:display-dependency-updates -Dverbose=true -Dversions.outputFile=./output4.txt -DoutputEncoding=UTF-8 -DprocessPluginDependencies=false -DpluginManagementDependencyExcludes=localhost:dummy-impl
8+
9+
invoker.goals.5=${project.groupId}:${project.artifactId}:${project.version}:display-dependency-updates -Dverbose=true -Dversions.outputFile=./output5.txt -DoutputEncoding=UTF-8 -DprocessPluginDependencies=false -DpluginManagementDependencyExcludes=localhost
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
3+
>
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>localhost</groupId>
6+
<artifactId>it-display-dependency-updates-issue-258-dependencyIncludesAndExcludes</artifactId>
7+
<version>1.0</version>
8+
<packaging>pom</packaging>
9+
10+
<build>
11+
<plugins>
12+
<plugin>
13+
<groupId>localhost</groupId>
14+
<artifactId>dummy-maven-plugin</artifactId>
15+
<version>1.0</version>
16+
<dependencies>
17+
<dependency>
18+
<groupId>localhost</groupId>
19+
<artifactId>dummy-api</artifactId>
20+
<version>1.0</version>
21+
</dependency>
22+
</dependencies>
23+
</plugin>
24+
</plugins>
25+
<pluginManagement>
26+
<plugins>
27+
<plugin>
28+
<groupId>localhost</groupId>
29+
<artifactId>dummy-maven-plugin</artifactId>
30+
<version>1.0</version>
31+
<dependencies>
32+
<dependency>
33+
<groupId>localhost</groupId>
34+
<artifactId>dummy-impl</artifactId>
35+
<version>1.0</version>
36+
</dependency>
37+
<dependency>
38+
<groupId>localhost</groupId>
39+
<artifactId>dummy-parent2</artifactId>
40+
<version>1.0</version>
41+
</dependency>
42+
</dependencies>
43+
</plugin>
44+
</plugins>
45+
</pluginManagement>
46+
</build>
47+
</project>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
output = new File(basedir, "output1.txt").text
2+
assert output =~ /localhost:dummy-api/
3+
assert output !=~ /localhost:dummy-impl/
4+
5+
output = new File(basedir, "output2.txt").text
6+
assert output !=~ /localhost:dummy-api/
7+
assert output =~ /localhost:dummy-impl/
8+
9+
output = new File(basedir, "output3.txt").text
10+
assert output =~ /localhost:dummy-impl/
11+
assert output !=~ /localhost:dummy-parent2/
12+
13+
output = new File(basedir, "output4.txt").text
14+
assert output !=~ /localhost:dummy-impl/
15+
assert output =~ /localhost:dummy-parent2/
16+
17+
output = new File(basedir, "output5.txt")
18+
assert !output.exists( )

src/it/it-display-dependency-updates-issue-318-dependencyExcludes/verify.groovy

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ assert buildLog.text.contains( """
44
[INFO] The following dependencies in Dependencies have newer versions:
55
[INFO] localhost:dummy-api ....................................... 1.0 -> 3.0
66
[INFO]
7-
[INFO] ------------------------------------------------------------------------
87
""".replaceAll( "\n", System.lineSeparator() ) )
98

109
return true

src/it/it-display-dependency-updates-issue-318-dependencyIncludes-multi/verify.groovy

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ assert buildLog.text.contains("""
55
[INFO] localhost:dummy-api ....................................... 1.0 -> 3.0
66
[INFO] localhost:dummy-impl ...................................... 1.0 -> 2.2
77
[INFO]
8-
[INFO] ------------------------------------------------------------------------
98
""".replaceAll( "\n", System.lineSeparator() ) )
109

1110
return true

src/it/it-display-dependency-updates-issue-318-dependencyIncludes/verify.groovy

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ assert buildLog.text.contains("""
55
[INFO] localhost:dummy-api ....................................... 1.0 -> 3.0
66
[INFO] localhost:dummy-impl ...................................... 1.0 -> 2.2
77
[INFO]
8-
[INFO] ------------------------------------------------------------------------
98
""".replaceAll( "\n", System.lineSeparator() ) )
109

1110
return true

src/it/it-display-dependency-updates-issue-318-dependencyIncludesAndExcludes/verify.groovy

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ assert buildLog.text.contains("""
44
[INFO] The following dependencies in Dependencies have newer versions:
55
[INFO] localhost:dummy-api ....................................... 1.0 -> 3.0
66
[INFO]
7-
[INFO] ------------------------------------------------------------------------
87
""".replaceAll( "\n", System.lineSeparator() ) )
98

109
return true

src/it/it-display-dependency-updates-issue-318-dependencyManagementExcludes/verify.groovy

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ assert buildLog.text.contains("""
55
[INFO] localhost:dummy-api ....................................... 1.0 -> 3.0
66
[INFO] localhost:dummy-impl ...................................... 1.0 -> 2.2
77
[INFO]
8-
[INFO] ------------------------------------------------------------------------
98
""".replaceAll( "\n", System.lineSeparator() ) )
109

1110
return true

src/it/it-display-dependency-updates-issue-318-dependencyManagementIncludes/verify.groovy

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ assert buildLog.text.contains("""
55
[INFO] localhost:dummy-api ....................................... 1.0 -> 3.0
66
[INFO] localhost:dummy-impl ...................................... 1.0 -> 2.2
77
[INFO]
8-
[INFO] ------------------------------------------------------------------------
98
""".replaceAll( "\n", System.lineSeparator() ) )
109

1110
return true

src/main/java/org/codehaus/mojo/versions/DisplayDependencyUpdatesMojo.java

Lines changed: 143 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.Objects;
2929
import java.util.Set;
3030
import java.util.TreeSet;
31+
import java.util.stream.Collectors;
3132

3233
import org.apache.maven.artifact.ArtifactUtils;
3334
import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException;
@@ -49,6 +50,8 @@
4950
import org.codehaus.mojo.versions.utils.DependencyComparator;
5051
import org.codehaus.plexus.util.StringUtils;
5152

53+
import static org.apache.commons.lang3.StringUtils.countMatches;
54+
5255
/**
5356
* Displays all dependencies that have newer versions available.
5457
* It will also display dependencies which are used by a plugin or
@@ -107,7 +110,7 @@ public class DisplayDependencyUpdatesMojo
107110
* </p>
108111
*
109112
* <p>
110-
* Example: "mygroup:artifact:*,*:*:*:*:*:compile"
113+
* Example: {@code "mygroup:artifact:*,*:*:*:*:*:compile"}
111114
* </p>
112115
*
113116
* @since 2.12.0
@@ -129,7 +132,7 @@ public class DisplayDependencyUpdatesMojo
129132
* </p>
130133
*
131134
* <p>
132-
* Example: "mygroup:artifact:*,*:*:*:*:*:provided,*:*:*:*:*:system"
135+
* Example: {@code "mygroup:artifact:*,*:*:*:*:*:provided,*:*:*:*:*:system"}
133136
* </p>
134137
*
135138
* @since 2.12.0
@@ -159,7 +162,7 @@ public class DisplayDependencyUpdatesMojo
159162
* </p>
160163
*
161164
* <p>
162-
* Example: "mygroup:artifact:*,*:*:*:*:*:compile"
165+
* Example: {@code "mygroup:artifact:*,*:*:*:*:*:compile"}
163166
* </p>
164167
*
165168
* @since 2.12.0
@@ -181,7 +184,7 @@ public class DisplayDependencyUpdatesMojo
181184
* </p>
182185
*
183186
* <p>
184-
* Example: "mygroup:artifact:*,*:*:*:*:*:provided,*:*:*:*:*:system"
187+
* Example: {@code "mygroup:artifact:*,*:*:*:*:*:provided,*:*:*:*:*:system"}
185188
* </p>
186189
*
187190
* @since 2.12.0
@@ -257,6 +260,77 @@ public class DisplayDependencyUpdatesMojo
257260
@Parameter( property = "verbose", defaultValue = "false" )
258261
private boolean verbose;
259262

263+
/**
264+
* <p>Only take these artifacts into consideration:<br/>
265+
* Comma-separated list of {@code groupId:[artifactId[:version]]} patterns</p>
266+
*
267+
* <p>
268+
* The wildcard "*" can be used as the only, first, last or both characters in each token.
269+
* The version token does support version ranges.
270+
* </p>
271+
*
272+
* <p>
273+
* Example: {@code "mygroup:artifact:*,othergroup:*,anothergroup"}
274+
* </p>
275+
*
276+
* @since 2.12.0
277+
*/
278+
@Parameter( property = "pluginDependencyIncludes", defaultValue = WildcardMatcher.WILDCARD )
279+
private List<String> pluginDependencyIncludes;
280+
281+
/**
282+
* <p>Exclude these artifacts into consideration:<br/>
283+
* Comma-separated list of {@code groupId:[artifactId[:version]]} patterns</p>
284+
*
285+
* <p>
286+
* The wildcard "*" can be used as the only, first, last or both characters in each token.
287+
* The version token does support version ranges.
288+
* </p>
289+
*
290+
* <p>
291+
* Example: {@code "mygroup:artifact:*,othergroup:*,anothergroup"}
292+
* </p>
293+
*
294+
* @since 2.12.0
295+
*/
296+
@Parameter( property = "pluginDependencyExcludes" )
297+
private List<String> pluginDependencyExcludes;
298+
299+
/**
300+
* <p>Only take these artifacts into consideration:<br/>
301+
* Comma-separated list of {@code groupId:[artifactId[:version]]} patterns</p>
302+
303+
* The wildcard "*" can be used as the only, first, last or both characters in each token.
304+
* The version token does support version ranges.
305+
* </p>
306+
*
307+
* <p>
308+
* Example: {@code "mygroup:artifact:*,othergroup:*,anothergroup"}
309+
* </p>
310+
*
311+
* @since 2.12.0
312+
*/
313+
@Parameter( property = "pluginManagementDependencyIncludes", defaultValue = WildcardMatcher.WILDCARD )
314+
private List<String> pluginManagementDependencyIncludes;
315+
316+
/**
317+
* <p>Exclude these artifacts into consideration:<br/>
318+
* Comma-separated list of {@code groupId:[artifactId[:version]]} patterns</p>
319+
*
320+
* <p>
321+
* The wildcard "*" can be used as the only, first, last or both characters in each token.
322+
* The version token does support version ranges.
323+
* </p>
324+
*
325+
* <p>
326+
* Example: {@code "mygroup:artifact:*,othergroup:*,anothergroup"}
327+
* </p>
328+
*
329+
* @since 2.12.0
330+
*/
331+
@Parameter( property = "pluginManagementDependencyExcludes" )
332+
private List<String> pluginManagementDependencyExcludes;
333+
260334
// --------------------- GETTER / SETTER METHODS ---------------------
261335

262336
private static Set<Dependency> extractPluginDependenciesFromPluginsInPluginManagement( Build build )
@@ -392,6 +466,8 @@ public void execute()
392466
{
393467
logInit();
394468

469+
validateInput();
470+
395471
Set<Dependency> dependencyManagement = new TreeSet<>( new DependencyComparator() );
396472
DependencyManagement projectDependencyManagement = getProjectDependencyManagement( getProject() );
397473
if ( projectDependencyManagement != null )
@@ -480,11 +556,16 @@ public void execute()
480556
}
481557
if ( isProcessPluginDependenciesInDependencyManagement() )
482558
{
559+
pluginDependenciesInPluginManagement =
560+
filterPluginManagementIncludes( pluginDependenciesInPluginManagement );
561+
483562
logUpdates( getHelper().lookupDependenciesUpdates( pluginDependenciesInPluginManagement, false ),
484563
"pluginManagement of plugins" );
485564
}
486565
if ( isProcessingPluginDependencies() )
487566
{
567+
pluginDependencies = filterPluginDependencyIncludes( pluginDependencies );
568+
488569
logUpdates( getHelper().lookupDependenciesUpdates( pluginDependencies, false ), "Plugin Dependencies" );
489570
}
490571
}
@@ -494,15 +575,56 @@ public void execute()
494575
}
495576
}
496577

578+
private void validateInput() throws MojoExecutionException
579+
{
580+
validateGAVList( dependencyIncludes, 6, "dependencyIncludes" );
581+
validateGAVList( dependencyExcludes, 6, "dependencyExcludes" );
582+
validateGAVList( dependencyManagementIncludes, 6, "dependencyManagementIncludes" );
583+
validateGAVList( dependencyManagementIncludes, 6, "dependencyManagementExcludes" );
584+
validateGAVList( pluginDependencyIncludes, 3, "pluginDependencyIncludes" );
585+
validateGAVList( pluginDependencyExcludes, 3, "pluginDependencyExcludes" );
586+
validateGAVList( pluginManagementDependencyIncludes, 3, "pluginManagementDependencyIncludes" );
587+
validateGAVList( pluginManagementDependencyExcludes, 3, "pluginManagementDependencyExcludes" );
588+
}
589+
590+
/**
591+
* Validates a list of GAV strings
592+
* @param gavList list of the input GAV strings
593+
* @param numSections number of sections in the GAV to verify against
594+
* @param argumentName argument name to indicate in the exception
595+
* @throws MojoExecutionException if the argument is invalid
596+
*/
597+
static void validateGAVList( List<String> gavList, int numSections, String argumentName )
598+
throws MojoExecutionException
599+
{
600+
if ( gavList != null && gavList.stream().anyMatch( gav -> countMatches( gav, ":" ) >= numSections ) )
601+
{
602+
throw new MojoExecutionException( argumentName + " should not contain more than 6 segments" );
603+
}
604+
}
605+
497606
private Set<Dependency> filterDependencyIncludes( Set<Dependency> dependencies )
498607
{
499-
return filterDependencies( dependencies, dependencyIncludes, dependencyExcludes, "dependencies" );
608+
return filterDependencies( dependencies, dependencyIncludes, dependencyExcludes, "Dependencies" );
500609
}
501610

502611
private Set<Dependency> filterDependencyManagementIncludes( Set<Dependency> dependencyManagement )
503612
{
504613
return filterDependencies( dependencyManagement,
505-
dependencyManagementIncludes, dependencyManagementExcludes, "dependecyManagement" );
614+
dependencyManagementIncludes, dependencyManagementExcludes, "Dependecy Management" );
615+
}
616+
617+
private Set<Dependency> filterPluginDependencyIncludes( Set<Dependency> dependencies )
618+
{
619+
return filterDependencies( dependencies, pluginDependencyIncludes, pluginDependencyExcludes,
620+
"Plugin Dependencies" );
621+
}
622+
623+
private Set<Dependency> filterPluginManagementIncludes( Set<Dependency> dependencyManagement )
624+
{
625+
return filterDependencies( dependencyManagement,
626+
pluginManagementDependencyIncludes, pluginManagementDependencyExcludes,
627+
"Plugin Management Dependencies" );
506628
}
507629

508630
private Set<Dependency> filterDependencies(
@@ -515,15 +637,26 @@ private Set<Dependency> filterDependencies(
515637
DependencyFilter includeDeps = DependencyFilter.parseFrom( includes );
516638
DependencyFilter excludeDeps = DependencyFilter.parseFrom( excludes );
517639

518-
getLog().debug( String.format( "parsed includes in %s: %s -> %s", section, includes, includeDeps ) );
519-
getLog().debug( String.format( "parsed excludes in %s: %s -> %s", section, excludes, excludeDeps ) );
640+
Set<Dependency> filtered = includeDeps.retainingIn( dependencies );
641+
filtered = excludeDeps.removingFrom( filtered );
520642

521-
Set<Dependency> onlyIncludes = includeDeps.retainingIn( dependencies );
522-
Set<Dependency> filtered = excludeDeps.removingFrom( onlyIncludes );
643+
if ( getLog().isDebugEnabled() )
644+
{
645+
getLog().debug( String.format( "parsed includes in %s: %s -> %s", section, includes, includeDeps ) );
646+
getLog().debug( String.format( "parsed excludes in %s: %s -> %s", section, excludes, excludeDeps ) );
647+
getLog().debug( String.format( "Unfiltered %s: ", section ) + output( dependencies ) );
648+
getLog().debug( String.format( "Filtered %s: ", section ) + output( filtered ) );
649+
}
523650

524651
return filtered;
525652
}
526653

654+
private String output( Set<Dependency> dependencies )
655+
{
656+
return dependencies.stream()
657+
.map( d -> String.format( "%s:%s:%s", d.getGroupId(), d.getArtifactId(), d.getVersion() ) )
658+
.collect( Collectors.joining( ", " ) );
659+
}
527660
private DependencyManagement getProjectDependencyManagement( MavenProject project )
528661
{
529662
if ( processDependencyManagementTransitive )

0 commit comments

Comments
 (0)