Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes mojohaus#123: add scope filtering #915

Merged
merged 1 commit into from
Feb 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -40,6 +40,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.ChangeRecord;
Expand Down Expand Up @@ -106,6 +107,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 @@ -376,6 +385,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 @@ -414,6 +429,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