Skip to content

Commit 19b4833

Browse files
committed
Keep order when filtering artifacts
This commit makes sure that the order of dependencies is kept when they are filtered. Closes gh-8397
1 parent d3fe982 commit 19b4833

File tree

2 files changed

+88
-8
lines changed

2 files changed

+88
-8
lines changed

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.boot.maven;
1818

19+
import java.util.Iterator;
1920
import java.util.List;
2021
import java.util.Set;
2122
import java.util.StringTokenizer;
@@ -89,14 +90,29 @@ protected void setExcludeArtifactIds(String excludeArtifactIds) {
8990
@SuppressWarnings("unchecked")
9091
protected Set<Artifact> filterDependencies(Set<Artifact> dependencies,
9192
FilterArtifacts filters) throws MojoExecutionException {
93+
List<ArtifactsFilter> artifactsFilters = filters.getFilters();
9294
try {
93-
return filters.filter(dependencies);
95+
for (ArtifactsFilter filter : artifactsFilters) {
96+
Set<Artifact> result = filter.filter(dependencies);
97+
applyFiltering(dependencies, result);
98+
}
99+
return dependencies;
94100
}
95101
catch (ArtifactFilterException e) {
96102
throw new MojoExecutionException(e.getMessage(), e);
97103
}
98104
}
99105

106+
private void applyFiltering(Set<Artifact> original, Set<Artifact> filtered) {
107+
Iterator<Artifact> iterator = original.iterator();
108+
while (iterator.hasNext()) {
109+
Artifact element = iterator.next();
110+
if (!filtered.contains(element)) {
111+
iterator.remove();
112+
}
113+
}
114+
}
115+
100116
/**
101117
* Return artifact filters configured for this MOJO.
102118
* @param additionalFilters optional additional filters to apply

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

Lines changed: 70 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,13 +18,15 @@
1818

1919
import java.util.Arrays;
2020
import java.util.Collections;
21-
import java.util.HashSet;
21+
import java.util.LinkedHashSet;
2222
import java.util.List;
2323
import java.util.Set;
2424

2525
import org.apache.maven.artifact.Artifact;
2626
import org.apache.maven.plugin.MojoExecutionException;
2727
import org.apache.maven.plugin.MojoFailureException;
28+
import org.apache.maven.shared.artifact.filter.collection.ArtifactsFilter;
29+
import org.apache.maven.shared.artifact.filter.collection.ScopeFilter;
2830
import org.junit.Test;
2931

3032
import static org.assertj.core.api.Assertions.assertThat;
@@ -64,27 +66,89 @@ public void filterGroupIdExactMatch() throws MojoExecutionException {
6466
assertThat(artifacts.iterator().next()).isSameAs(artifact);
6567
}
6668

67-
private Artifact createArtifact(String groupId, String artifactId) {
69+
@Test
70+
public void filterScopeKeepOrder() throws MojoExecutionException {
71+
TestableDependencyFilterMojo mojo = new TestableDependencyFilterMojo(
72+
Collections.<Exclude>emptyList(), "", "",
73+
new ScopeFilter(null, Artifact.SCOPE_SYSTEM));
74+
Artifact one = createArtifact("com.foo", "one");
75+
Artifact two = createArtifact("com.foo", "two", Artifact.SCOPE_SYSTEM);
76+
Artifact three = createArtifact("com.foo", "three", Artifact.SCOPE_RUNTIME);
77+
Set<Artifact> artifacts = mojo.filterDependencies(one, two, three);
78+
assertThat(artifacts).containsExactly(one, three);
79+
}
80+
81+
@Test
82+
public void filterArtifactIdKeepOrder() throws MojoExecutionException {
83+
TestableDependencyFilterMojo mojo = new TestableDependencyFilterMojo(
84+
Collections.<Exclude>emptyList(), "", "one,three");
85+
Artifact one = createArtifact("com.foo", "one");
86+
Artifact two = createArtifact("com.foo", "two");
87+
Artifact three = createArtifact("com.foo", "three");
88+
Artifact four = createArtifact("com.foo", "four");
89+
Set<Artifact> artifacts = mojo.filterDependencies(one, two, three, four);
90+
assertThat(artifacts).containsExactly(two, four);
91+
}
92+
93+
@Test
94+
public void filterGroupIdKeepOrder() throws MojoExecutionException {
95+
TestableDependencyFilterMojo mojo = new TestableDependencyFilterMojo(
96+
Collections.<Exclude>emptyList(), "com.foo", "");
97+
Artifact one = createArtifact("com.foo", "one");
98+
Artifact two = createArtifact("com.bar", "two");
99+
Artifact three = createArtifact("com.bar", "three");
100+
Artifact four = createArtifact("com.foo", "four");
101+
Set<Artifact> artifacts = mojo.filterDependencies(one, two, three, four);
102+
assertThat(artifacts).containsExactly(two, three);
103+
}
104+
105+
@Test
106+
public void filterExcludeKeepOrder() throws MojoExecutionException {
107+
Exclude exclude = new Exclude();
108+
exclude.setGroupId("com.bar");
109+
exclude.setArtifactId("two");
110+
TestableDependencyFilterMojo mojo = new TestableDependencyFilterMojo(
111+
Collections.singletonList(exclude), "", "");
112+
Artifact one = createArtifact("com.foo", "one");
113+
Artifact two = createArtifact("com.bar", "two");
114+
Artifact three = createArtifact("com.bar", "three");
115+
Artifact four = createArtifact("com.foo", "four");
116+
Set<Artifact> artifacts = mojo.filterDependencies(one, two, three, four);
117+
assertThat(artifacts).containsExactly(one, three, four);
118+
}
119+
120+
private static Artifact createArtifact(String groupId, String artifactId) {
121+
return createArtifact(groupId, artifactId, null);
122+
}
123+
124+
private static Artifact createArtifact(String groupId, String artifactId, String scope) {
68125
Artifact a = mock(Artifact.class);
69126
given(a.getGroupId()).willReturn(groupId);
70127
given(a.getArtifactId()).willReturn(artifactId);
128+
if (scope != null) {
129+
given(a.getScope()).willReturn(scope);
130+
}
71131
return a;
72132
}
73133

74134
private static final class TestableDependencyFilterMojo
75135
extends AbstractDependencyFilterMojo {
76136

137+
private final ArtifactsFilter[] additionalFilters;
138+
77139
private TestableDependencyFilterMojo(List<Exclude> excludes,
78-
String excludeGroupIds, String excludeArtifactIds) {
140+
String excludeGroupIds, String excludeArtifactIds,
141+
ArtifactsFilter... additionalFilters) {
79142
setExcludes(excludes);
80143
setExcludeGroupIds(excludeGroupIds);
81144
setExcludeArtifactIds(excludeArtifactIds);
145+
this.additionalFilters = additionalFilters;
82146
}
83147

84148
public Set<Artifact> filterDependencies(Artifact... artifacts)
85149
throws MojoExecutionException {
86-
Set<Artifact> input = new HashSet<Artifact>(Arrays.asList(artifacts));
87-
return filterDependencies(input, getFilters());
150+
Set<Artifact> input = new LinkedHashSet<Artifact>(Arrays.asList(artifacts));
151+
return filterDependencies(input, getFilters(this.additionalFilters));
88152
}
89153

90154
@Override

0 commit comments

Comments
 (0)