Commit dd83b58b authored by Mark Ingram's avatar Mark Ingram Committed by Stephane Nicoll

Exact match for groupId excludes

Previous to this commit, any groupId starting with one of the
configured exclude would be excluded as well. This potentially
leads to unintentional dependency filtering: for example the
GroupIdFilter with an exclusion of "org.springframework"
also removes "org.springframework.boot" dependencies.

Add MatchingGroupIdFilter that uses an exact match instead.

See #649
parent 77ae7903
...@@ -28,7 +28,6 @@ import org.apache.maven.shared.artifact.filter.collection.ArtifactFilterExceptio ...@@ -28,7 +28,6 @@ import org.apache.maven.shared.artifact.filter.collection.ArtifactFilterExceptio
import org.apache.maven.shared.artifact.filter.collection.ArtifactIdFilter; import org.apache.maven.shared.artifact.filter.collection.ArtifactIdFilter;
import org.apache.maven.shared.artifact.filter.collection.ArtifactsFilter; import org.apache.maven.shared.artifact.filter.collection.ArtifactsFilter;
import org.apache.maven.shared.artifact.filter.collection.FilterArtifacts; import org.apache.maven.shared.artifact.filter.collection.FilterArtifacts;
import org.apache.maven.shared.artifact.filter.collection.GroupIdFilter;
/** /**
* A base mojo filtering the dependencies of the project. * A base mojo filtering the dependencies of the project.
...@@ -48,14 +47,14 @@ public abstract class AbstractDependencyFilterMojo extends AbstractMojo { ...@@ -48,14 +47,14 @@ public abstract class AbstractDependencyFilterMojo extends AbstractMojo {
private List<Exclude> excludes; private List<Exclude> excludes;
/** /**
* Comma separated list of groupId names to exclude. * Comma separated list of groupId names to exclude (exact match).
* @since 1.1 * @since 1.1
*/ */
@Parameter(property = "excludeGroupIds", defaultValue = "") @Parameter(property = "excludeGroupIds", defaultValue = "")
private String excludeGroupIds; private String excludeGroupIds;
/** /**
* Comma separated list of artifact names to exclude. * Comma separated list of artifact names to exclude (exact match).
* @since 1.1 * @since 1.1
*/ */
@Parameter(property = "excludeArtifactIds", defaultValue = "") @Parameter(property = "excludeArtifactIds", defaultValue = "")
...@@ -96,7 +95,7 @@ public abstract class AbstractDependencyFilterMojo extends AbstractMojo { ...@@ -96,7 +95,7 @@ public abstract class AbstractDependencyFilterMojo extends AbstractMojo {
} }
filters.addFilter(new ArtifactIdFilter("", filters.addFilter(new ArtifactIdFilter("",
cleanFilterConfig(this.excludeArtifactIds))); cleanFilterConfig(this.excludeArtifactIds)));
filters.addFilter(new GroupIdFilter("", cleanFilterConfig(this.excludeGroupIds))); filters.addFilter(new MatchingGroupIdFilter(cleanFilterConfig(this.excludeGroupIds)));
if (this.excludes != null) { if (this.excludes != null) {
filters.addFilter(new ExcludeFilter(this.excludes)); filters.addFilter(new ExcludeFilter(this.excludes));
} }
......
package org.springframework.boot.maven;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.shared.artifact.filter.collection.AbstractArtifactFeatureFilter;
/**
* An {@link org.apache.maven.shared.artifact.filter.collection.ArtifactsFilter
* ArtifactsFilter} that filters by matching groupId.
*
* Preferred over the {@link org.apache.maven.shared.artifact.filter.collection.GroupIdFilter} due
* to that classes use of {@link String#startsWith} to match on prefix.
*
* @author Mark Ingram
* @since 1.1
*/
public class MatchingGroupIdFilter extends AbstractArtifactFeatureFilter {
/**
* Create a new instance with the CSV groupId values that should be excluded.
*/
public MatchingGroupIdFilter(String exclude) {
super("", exclude);
}
protected String getArtifactFeature(Artifact artifact) {
return artifact.getGroupId();
}
}
...@@ -51,6 +51,19 @@ public class DependencyFilterMojoTests { ...@@ -51,6 +51,19 @@ public class DependencyFilterMojoTests {
assertSame("Wrong filtered artifact", artifact, artifacts.iterator().next()); assertSame("Wrong filtered artifact", artifact, artifacts.iterator().next());
} }
@Test
public void filterGroupIdExactMatch() throws MojoExecutionException {
TestableDependencyFilterMojo mojo = new TestableDependencyFilterMojo(
Collections.<Exclude>emptyList(), "com.foo", "");
Artifact artifact = createArtifact("com.foo.bar", "one");
Set<Artifact> artifacts = mojo.filterDependencies(
createArtifact("com.foo", "one"), createArtifact("com.foo", "two"),
artifact);
assertEquals("wrong filtering of artifacts", 1, artifacts.size());
assertSame("Wrong filtered artifact", artifact, artifacts.iterator().next());
}
private Artifact createArtifact(String groupId, String artifactId) { private Artifact createArtifact(String groupId, String artifactId) {
Artifact a = mock(Artifact.class); Artifact a = mock(Artifact.class);
given(a.getGroupId()).willReturn(groupId); given(a.getGroupId()).willReturn(groupId);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment