Skip to content

Commit

Permalink
Amended #258: Adding plugin and plugin management dependency includes…
Browse files Browse the repository at this point in the history
…/excludes
  • Loading branch information
jarmoniuk authored and slawekjaranowski committed Sep 6, 2022
1 parent 5fa3693 commit f9a90e5
Show file tree
Hide file tree
Showing 11 changed files with 264 additions and 16 deletions.
@@ -0,0 +1,9 @@
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

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

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

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

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
@@ -0,0 +1,47 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<modelVersion>4.0.0</modelVersion>
<groupId>localhost</groupId>
<artifactId>it-display-dependency-updates-issue-258-dependencyIncludesAndExcludes</artifactId>
<version>1.0</version>
<packaging>pom</packaging>

<build>
<plugins>
<plugin>
<groupId>localhost</groupId>
<artifactId>dummy-maven-plugin</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>localhost</groupId>
<artifactId>dummy-api</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>localhost</groupId>
<artifactId>dummy-maven-plugin</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>localhost</groupId>
<artifactId>dummy-impl</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>localhost</groupId>
<artifactId>dummy-parent2</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
@@ -0,0 +1,18 @@
output = new File(basedir, "output1.txt").text
assert output =~ /localhost:dummy-api/
assert output !=~ /localhost:dummy-impl/

output = new File(basedir, "output2.txt").text
assert output !=~ /localhost:dummy-api/
assert output =~ /localhost:dummy-impl/

output = new File(basedir, "output3.txt").text
assert output =~ /localhost:dummy-impl/
assert output !=~ /localhost:dummy-parent2/

output = new File(basedir, "output4.txt").text
assert output !=~ /localhost:dummy-impl/
assert output =~ /localhost:dummy-parent2/

output = new File(basedir, "output5.txt")
assert !output.exists( )
Expand Up @@ -4,7 +4,6 @@ assert buildLog.text.contains( """
[INFO] The following dependencies in Dependencies have newer versions:
[INFO] localhost:dummy-api ....................................... 1.0 -> 3.0
[INFO]
[INFO] ------------------------------------------------------------------------
""".replaceAll( "\n", System.lineSeparator() ) )

return true
Expand Up @@ -5,7 +5,6 @@ assert buildLog.text.contains("""
[INFO] localhost:dummy-api ....................................... 1.0 -> 3.0
[INFO] localhost:dummy-impl ...................................... 1.0 -> 2.2
[INFO]
[INFO] ------------------------------------------------------------------------
""".replaceAll( "\n", System.lineSeparator() ) )

return true
Expand Up @@ -5,7 +5,6 @@ assert buildLog.text.contains("""
[INFO] localhost:dummy-api ....................................... 1.0 -> 3.0
[INFO] localhost:dummy-impl ...................................... 1.0 -> 2.2
[INFO]
[INFO] ------------------------------------------------------------------------
""".replaceAll( "\n", System.lineSeparator() ) )

return true
Expand Up @@ -4,7 +4,6 @@ assert buildLog.text.contains("""
[INFO] The following dependencies in Dependencies have newer versions:
[INFO] localhost:dummy-api ....................................... 1.0 -> 3.0
[INFO]
[INFO] ------------------------------------------------------------------------
""".replaceAll( "\n", System.lineSeparator() ) )

return true
Expand Up @@ -5,7 +5,6 @@ assert buildLog.text.contains("""
[INFO] localhost:dummy-api ....................................... 1.0 -> 3.0
[INFO] localhost:dummy-impl ...................................... 1.0 -> 2.2
[INFO]
[INFO] ------------------------------------------------------------------------
""".replaceAll( "\n", System.lineSeparator() ) )

return true
Expand Up @@ -5,7 +5,6 @@ assert buildLog.text.contains("""
[INFO] localhost:dummy-api ....................................... 1.0 -> 3.0
[INFO] localhost:dummy-impl ...................................... 1.0 -> 2.2
[INFO]
[INFO] ------------------------------------------------------------------------
""".replaceAll( "\n", System.lineSeparator() ) )

return true
Expand Up @@ -28,6 +28,7 @@
import java.util.Objects;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;

import org.apache.maven.artifact.ArtifactUtils;
import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException;
Expand All @@ -49,6 +50,8 @@
import org.codehaus.mojo.versions.utils.DependencyComparator;
import org.codehaus.plexus.util.StringUtils;

import static org.apache.commons.lang3.StringUtils.countMatches;

/**
* Displays all dependencies that have newer versions available.
* It will also display dependencies which are used by a plugin or
Expand Down Expand Up @@ -107,7 +110,7 @@ public class DisplayDependencyUpdatesMojo
* </p>
*
* <p>
* Example: "mygroup:artifact:*,*:*:*:*:*:compile"
* Example: {@code "mygroup:artifact:*,*:*:*:*:*:compile"}
* </p>
*
* @since 2.12.0
Expand All @@ -129,7 +132,7 @@ public class DisplayDependencyUpdatesMojo
* </p>
*
* <p>
* Example: "mygroup:artifact:*,*:*:*:*:*:provided,*:*:*:*:*:system"
* Example: {@code "mygroup:artifact:*,*:*:*:*:*:provided,*:*:*:*:*:system"}
* </p>
*
* @since 2.12.0
Expand Down Expand Up @@ -159,7 +162,7 @@ public class DisplayDependencyUpdatesMojo
* </p>
*
* <p>
* Example: "mygroup:artifact:*,*:*:*:*:*:compile"
* Example: {@code "mygroup:artifact:*,*:*:*:*:*:compile"}
* </p>
*
* @since 2.12.0
Expand All @@ -181,7 +184,7 @@ public class DisplayDependencyUpdatesMojo
* </p>
*
* <p>
* Example: "mygroup:artifact:*,*:*:*:*:*:provided,*:*:*:*:*:system"
* Example: {@code "mygroup:artifact:*,*:*:*:*:*:provided,*:*:*:*:*:system"}
* </p>
*
* @since 2.12.0
Expand Down Expand Up @@ -257,6 +260,77 @@ public class DisplayDependencyUpdatesMojo
@Parameter( property = "verbose", defaultValue = "false" )
private boolean verbose;

/**
* <p>Only take these artifacts into consideration:<br/>
* Comma-separated list of {@code groupId:[artifactId[:version]]} patterns</p>
*
* <p>
* The wildcard "*" can be used as the only, first, last or both characters in each token.
* The version token does support version ranges.
* </p>
*
* <p>
* Example: {@code "mygroup:artifact:*,othergroup:*,anothergroup"}
* </p>
*
* @since 2.12.0
*/
@Parameter( property = "pluginDependencyIncludes", defaultValue = WildcardMatcher.WILDCARD )
private List<String> pluginDependencyIncludes;

/**
* <p>Exclude these artifacts into consideration:<br/>
* Comma-separated list of {@code groupId:[artifactId[:version]]} patterns</p>
*
* <p>
* The wildcard "*" can be used as the only, first, last or both characters in each token.
* The version token does support version ranges.
* </p>
*
* <p>
* Example: {@code "mygroup:artifact:*,othergroup:*,anothergroup"}
* </p>
*
* @since 2.12.0
*/
@Parameter( property = "pluginDependencyExcludes" )
private List<String> pluginDependencyExcludes;

/**
* <p>Only take these artifacts into consideration:<br/>
* Comma-separated list of {@code groupId:[artifactId[:version]]} patterns</p>
* The wildcard "*" can be used as the only, first, last or both characters in each token.
* The version token does support version ranges.
* </p>
*
* <p>
* Example: {@code "mygroup:artifact:*,othergroup:*,anothergroup"}
* </p>
*
* @since 2.12.0
*/
@Parameter( property = "pluginManagementDependencyIncludes", defaultValue = WildcardMatcher.WILDCARD )
private List<String> pluginManagementDependencyIncludes;

/**
* <p>Exclude these artifacts into consideration:<br/>
* Comma-separated list of {@code groupId:[artifactId[:version]]} patterns</p>
*
* <p>
* The wildcard "*" can be used as the only, first, last or both characters in each token.
* The version token does support version ranges.
* </p>
*
* <p>
* Example: {@code "mygroup:artifact:*,othergroup:*,anothergroup"}
* </p>
*
* @since 2.12.0
*/
@Parameter( property = "pluginManagementDependencyExcludes" )
private List<String> pluginManagementDependencyExcludes;

// --------------------- GETTER / SETTER METHODS ---------------------

private static Set<Dependency> extractPluginDependenciesFromPluginsInPluginManagement( Build build )
Expand Down Expand Up @@ -392,6 +466,8 @@ public void execute()
{
logInit();

validateInput();

Set<Dependency> dependencyManagement = new TreeSet<>( new DependencyComparator() );
DependencyManagement projectDependencyManagement = getProjectDependencyManagement( getProject() );
if ( projectDependencyManagement != null )
Expand Down Expand Up @@ -480,11 +556,16 @@ public void execute()
}
if ( isProcessPluginDependenciesInDependencyManagement() )
{
pluginDependenciesInPluginManagement =
filterPluginManagementIncludes( pluginDependenciesInPluginManagement );

logUpdates( getHelper().lookupDependenciesUpdates( pluginDependenciesInPluginManagement, false ),
"pluginManagement of plugins" );
}
if ( isProcessingPluginDependencies() )
{
pluginDependencies = filterPluginDependencyIncludes( pluginDependencies );

logUpdates( getHelper().lookupDependenciesUpdates( pluginDependencies, false ), "Plugin Dependencies" );
}
}
Expand All @@ -494,15 +575,56 @@ public void execute()
}
}

private void validateInput() throws MojoExecutionException
{
validateGAVList( dependencyIncludes, 6, "dependencyIncludes" );
validateGAVList( dependencyExcludes, 6, "dependencyExcludes" );
validateGAVList( dependencyManagementIncludes, 6, "dependencyManagementIncludes" );
validateGAVList( dependencyManagementIncludes, 6, "dependencyManagementExcludes" );
validateGAVList( pluginDependencyIncludes, 3, "pluginDependencyIncludes" );
validateGAVList( pluginDependencyExcludes, 3, "pluginDependencyExcludes" );
validateGAVList( pluginManagementDependencyIncludes, 3, "pluginManagementDependencyIncludes" );
validateGAVList( pluginManagementDependencyExcludes, 3, "pluginManagementDependencyExcludes" );
}

/**
* Validates a list of GAV strings
* @param gavList list of the input GAV strings
* @param numSections number of sections in the GAV to verify against
* @param argumentName argument name to indicate in the exception
* @throws MojoExecutionException if the argument is invalid
*/
static void validateGAVList( List<String> gavList, int numSections, String argumentName )
throws MojoExecutionException
{
if ( gavList != null && gavList.stream().anyMatch( gav -> countMatches( gav, ":" ) >= numSections ) )
{
throw new MojoExecutionException( argumentName + " should not contain more than 6 segments" );
}
}

private Set<Dependency> filterDependencyIncludes( Set<Dependency> dependencies )
{
return filterDependencies( dependencies, dependencyIncludes, dependencyExcludes, "dependencies" );
return filterDependencies( dependencies, dependencyIncludes, dependencyExcludes, "Dependencies" );
}

private Set<Dependency> filterDependencyManagementIncludes( Set<Dependency> dependencyManagement )
{
return filterDependencies( dependencyManagement,
dependencyManagementIncludes, dependencyManagementExcludes, "dependecyManagement" );
dependencyManagementIncludes, dependencyManagementExcludes, "Dependecy Management" );
}

private Set<Dependency> filterPluginDependencyIncludes( Set<Dependency> dependencies )
{
return filterDependencies( dependencies, pluginDependencyIncludes, pluginDependencyExcludes,
"Plugin Dependencies" );
}

private Set<Dependency> filterPluginManagementIncludes( Set<Dependency> dependencyManagement )
{
return filterDependencies( dependencyManagement,
pluginManagementDependencyIncludes, pluginManagementDependencyExcludes,
"Plugin Management Dependencies" );
}

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

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

Set<Dependency> onlyIncludes = includeDeps.retainingIn( dependencies );
Set<Dependency> filtered = excludeDeps.removingFrom( onlyIncludes );
if ( getLog().isDebugEnabled() )
{
getLog().debug( String.format( "parsed includes in %s: %s -> %s", section, includes, includeDeps ) );
getLog().debug( String.format( "parsed excludes in %s: %s -> %s", section, excludes, excludeDeps ) );
getLog().debug( String.format( "Unfiltered %s: ", section ) + output( dependencies ) );
getLog().debug( String.format( "Filtered %s: ", section ) + output( filtered ) );
}

return filtered;
}

private String output( Set<Dependency> dependencies )
{
return dependencies.stream()
.map( d -> String.format( "%s:%s:%s", d.getGroupId(), d.getArtifactId(), d.getVersion() ) )
.collect( Collectors.joining( ", " ) );
}
private DependencyManagement getProjectDependencyManagement( MavenProject project )
{
if ( processDependencyManagementTransitive )
Expand Down

0 comments on commit f9a90e5

Please sign in to comment.