Skip to content

Commit 1fd81be

Browse files
MarkIngramTrafiguramdeinum
authored andcommitted
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 spring-projects#649
1 parent 6e49909 commit 1fd81be

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/AbstractDependencyFilterMojo.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import org.apache.maven.shared.artifact.filter.collection.ArtifactIdFilter;
2929
import org.apache.maven.shared.artifact.filter.collection.ArtifactsFilter;
3030
import org.apache.maven.shared.artifact.filter.collection.FilterArtifacts;
31-
import org.apache.maven.shared.artifact.filter.collection.GroupIdFilter;
3231

3332
/**
3433
* A base mojo filtering the dependencies of the project.
@@ -48,14 +47,14 @@ public abstract class AbstractDependencyFilterMojo extends AbstractMojo {
4847
private List<Exclude> excludes;
4948

5049
/**
51-
* Comma separated list of groupId names to exclude.
50+
* Comma separated list of groupId names to exclude (exact match).
5251
* @since 1.1
5352
*/
5453
@Parameter(property = "excludeGroupIds", defaultValue = "")
5554
private String excludeGroupIds;
5655

5756
/**
58-
* Comma separated list of artifact names to exclude.
57+
* Comma separated list of artifact names to exclude (exact match).
5958
* @since 1.1
6059
*/
6160
@Parameter(property = "excludeArtifactIds", defaultValue = "")
@@ -96,7 +95,7 @@ protected final FilterArtifacts getFilters(ArtifactsFilter... additionalFilters)
9695
}
9796
filters.addFilter(new ArtifactIdFilter("",
9897
cleanFilterConfig(this.excludeArtifactIds)));
99-
filters.addFilter(new GroupIdFilter("", cleanFilterConfig(this.excludeGroupIds)));
98+
filters.addFilter(new MatchingGroupIdFilter(cleanFilterConfig(this.excludeGroupIds)));
10099
if (this.excludes != null) {
101100
filters.addFilter(new ExcludeFilter(this.excludes));
102101
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.springframework.boot.maven;
2+
3+
import org.apache.maven.artifact.Artifact;
4+
import org.apache.maven.shared.artifact.filter.collection.AbstractArtifactFeatureFilter;
5+
6+
/**
7+
* An {@link org.apache.maven.shared.artifact.filter.collection.ArtifactsFilter
8+
* ArtifactsFilter} that filters by matching groupId.
9+
*
10+
* Preferred over the {@link org.apache.maven.shared.artifact.filter.collection.GroupIdFilter} due
11+
* to that classes use of {@link String#startsWith} to match on prefix.
12+
*
13+
* @author Mark Ingram
14+
* @since 1.1
15+
*/
16+
public class MatchingGroupIdFilter extends AbstractArtifactFeatureFilter {
17+
18+
/**
19+
* Create a new instance with the CSV groupId values that should be excluded.
20+
*/
21+
public MatchingGroupIdFilter(String exclude) {
22+
super("", exclude);
23+
}
24+
25+
protected String getArtifactFeature(Artifact artifact) {
26+
return artifact.getGroupId();
27+
}
28+
}

spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/DependencyFilterMojoTests.java

+13
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,19 @@ public void filterDependencies() throws MojoExecutionException {
5151
assertSame("Wrong filtered artifact", artifact, artifacts.iterator().next());
5252
}
5353

54+
@Test
55+
public void filterGroupIdExactMatch() throws MojoExecutionException {
56+
TestableDependencyFilterMojo mojo = new TestableDependencyFilterMojo(
57+
Collections.<Exclude>emptyList(), "com.foo", "");
58+
59+
Artifact artifact = createArtifact("com.foo.bar", "one");
60+
Set<Artifact> artifacts = mojo.filterDependencies(
61+
createArtifact("com.foo", "one"), createArtifact("com.foo", "two"),
62+
artifact);
63+
assertEquals("wrong filtering of artifacts", 1, artifacts.size());
64+
assertSame("Wrong filtered artifact", artifact, artifacts.iterator().next());
65+
}
66+
5467
private Artifact createArtifact(String groupId, String artifactId) {
5568
Artifact a = mock(Artifact.class);
5669
given(a.getGroupId()).willReturn(groupId);

0 commit comments

Comments
 (0)