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
pull/890/merge
Mark Ingram 11 years ago committed by Stephane Nicoll
parent 77ae790363
commit dd83b58b05

@ -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.ArtifactsFilter;
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.
@ -48,14 +47,14 @@ public abstract class AbstractDependencyFilterMojo extends AbstractMojo {
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
*/
@Parameter(property = "excludeGroupIds", defaultValue = "")
private String excludeGroupIds;
/**
* Comma separated list of artifact names to exclude.
* Comma separated list of artifact names to exclude (exact match).
* @since 1.1
*/
@Parameter(property = "excludeArtifactIds", defaultValue = "")
@ -96,7 +95,7 @@ public abstract class AbstractDependencyFilterMojo extends AbstractMojo {
}
filters.addFilter(new ArtifactIdFilter("",
cleanFilterConfig(this.excludeArtifactIds)));
filters.addFilter(new GroupIdFilter("", cleanFilterConfig(this.excludeGroupIds)));
filters.addFilter(new MatchingGroupIdFilter(cleanFilterConfig(this.excludeGroupIds)));
if (this.excludes != null) {
filters.addFilter(new ExcludeFilter(this.excludes));
}

@ -0,0 +1,28 @@
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 {
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) {
Artifact a = mock(Artifact.class);
given(a.getGroupId()).willReturn(groupId);

Loading…
Cancel
Save