Skip to content

Commit

Permalink
Fixes #123: add scope filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
vboulaye authored and slawekjaranowski committed Feb 12, 2023
1 parent 2c22da0 commit 5547184
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
invoker.goals=${project.groupId}:${project.artifactId}:${project.version}:compare-dependencies -Dscope=compile
invoker.systemPropertiesFile = test.properties
44 changes: 44 additions & 0 deletions versions-maven-plugin/src/it/it-compare-dependencies-006/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<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-compare-dependencies-001</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<name>#123 add scope filter property</name>
<description>
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
</description>
<dependencyManagement>

<dependencies>

<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-artifact</artifactId>
<version>2.0.10</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.0</version>
<scope>test</scope>
</dependency>

</dependencies>

</dependencyManagement>

<dependencies>

<dependency>
<groupId>localhost</groupId>
<artifactId>dummy-api</artifactId>
<version>1.1.1-2</version>
</dependency>

</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
remotePom=localhost:dummy-bom-pom:1.0
reportOutputFile=target/depDiffs.txt
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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().
Expand Down

0 comments on commit 5547184

Please sign in to comment.