From 554718439af6b4eda3c5db6e8a68c32f1bb39e36 Mon Sep 17 00:00:00 2001 From: Vincent Boulaye Date: Wed, 1 Feb 2023 09:24:58 +0100 Subject: [PATCH] Fixes mojohaus#123: add scope filtering --- .../invoker.properties | 2 + .../it/it-compare-dependencies-006/pom.xml | 44 +++++++++++++++++++ .../test.properties | 2 + .../it/it-compare-dependencies-006/verify.bsh | 26 +++++++++++ ...AbstractVersionsDependencyUpdaterMojo.java | 22 ++++++++++ 5 files changed, 96 insertions(+) create mode 100644 versions-maven-plugin/src/it/it-compare-dependencies-006/invoker.properties create mode 100644 versions-maven-plugin/src/it/it-compare-dependencies-006/pom.xml create mode 100644 versions-maven-plugin/src/it/it-compare-dependencies-006/test.properties create mode 100644 versions-maven-plugin/src/it/it-compare-dependencies-006/verify.bsh diff --git a/versions-maven-plugin/src/it/it-compare-dependencies-006/invoker.properties b/versions-maven-plugin/src/it/it-compare-dependencies-006/invoker.properties new file mode 100644 index 0000000000..d92099d7b2 --- /dev/null +++ b/versions-maven-plugin/src/it/it-compare-dependencies-006/invoker.properties @@ -0,0 +1,2 @@ +invoker.goals=${project.groupId}:${project.artifactId}:${project.version}:compare-dependencies -Dscope=compile +invoker.systemPropertiesFile = test.properties diff --git a/versions-maven-plugin/src/it/it-compare-dependencies-006/pom.xml b/versions-maven-plugin/src/it/it-compare-dependencies-006/pom.xml new file mode 100644 index 0000000000..f2393a13ab --- /dev/null +++ b/versions-maven-plugin/src/it/it-compare-dependencies-006/pom.xml @@ -0,0 +1,44 @@ + + 4.0.0 + + localhost + it-compare-dependencies-001 + 1.0 + pom + #123 add scope filter property + + the scope filter property to allow excluding artifacts based on scope: + junit is set to test scope and we ask a comparison in scope compile + + + + + + + org.apache.maven + maven-artifact + 2.0.10 + + + junit + junit + 4.0 + test + + + + + + + + + + localhost + dummy-api + 1.1.1-2 + + + + + diff --git a/versions-maven-plugin/src/it/it-compare-dependencies-006/test.properties b/versions-maven-plugin/src/it/it-compare-dependencies-006/test.properties new file mode 100644 index 0000000000..b432833b84 --- /dev/null +++ b/versions-maven-plugin/src/it/it-compare-dependencies-006/test.properties @@ -0,0 +1,2 @@ +remotePom=localhost:dummy-bom-pom:1.0 +reportOutputFile=target/depDiffs.txt diff --git a/versions-maven-plugin/src/it/it-compare-dependencies-006/verify.bsh b/versions-maven-plugin/src/it/it-compare-dependencies-006/verify.bsh new file mode 100644 index 0000000000..e946cfc7d8 --- /dev/null +++ b/versions-maven-plugin/src/it/it-compare-dependencies-006/verify.bsh @@ -0,0 +1,26 @@ +import java.io.*; +import org.codehaus.plexus.util.FileUtils; + +try +{ + File file = new File( basedir, "target/depDiffs.txt" ); + String buf = FileUtils.fileRead( file, "UTF-8" ); + + if ( buf.indexOf( "2.0.10 -> 2.0.9" ) < 0 ) + { + System.err.println( "Version diff in maven artifact not found. it should be processed because its scope is compile" ); + return false; + } + if ( buf.indexOf( "4.0 -> 4.1" ) > 0 ) + { + System.err.println( "Version diff in junit artifact found. it should be excluded because its scope is test" ); + return false; + } +} +catch( Throwable t ) +{ + t.printStackTrace(); + return false; +} + +return true; diff --git a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/AbstractVersionsDependencyUpdaterMojo.java b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/AbstractVersionsDependencyUpdaterMojo.java index a04533900a..8804f9c4d9 100644 --- a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/AbstractVersionsDependencyUpdaterMojo.java +++ b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/AbstractVersionsDependencyUpdaterMojo.java @@ -36,6 +36,7 @@ import org.apache.maven.repository.RepositorySystem; import org.apache.maven.shared.artifact.filter.PatternExcludesArtifactFilter; import org.apache.maven.shared.artifact.filter.PatternIncludesArtifactFilter; +import org.apache.maven.shared.artifact.filter.ScopeArtifactFilter; import org.apache.maven.wagon.Wagon; import org.codehaus.mojo.versions.api.PomHelper; import org.codehaus.mojo.versions.api.recording.ChangeRecorder; @@ -102,6 +103,14 @@ public abstract class AbstractVersionsDependencyUpdaterMojo extends AbstractVers @Parameter private String[] excludes = null; + /** + * a scope to use to filter the artifacts matching the asked scope (as well as the ones implied by maven) + * + * @since 2.15 + */ + @Parameter(property = "scope") + private String scope = null; + /** * Whether to process the dependencies section of the project. * @@ -372,6 +381,12 @@ protected boolean isIncluded(Artifact artifact) { result = result && excludesFilter.include(artifact); } + ArtifactFilter scopeFilter = this.getScopeArtifactFilter(); + + if (scopeFilter != null) { + result = result && scopeFilter.include(artifact); + } + return result; } @@ -410,6 +425,13 @@ private ArtifactFilter getExcludesArtifactFilter() { return excludesFilter; } + private ArtifactFilter getScopeArtifactFilter() { + if (scope == null) { + return null; + } + return new ScopeArtifactFilter(scope); + } + /** * To handle multiple includes with version range like "group:artifact:jar:[1.0.0,2.2)", we have to use a parsing a * little bit more complex than split().