Skip to content

Commit e95b0b5

Browse files
committed
Polish
1 parent 670d167 commit e95b0b5

File tree

1 file changed

+54
-31
lines changed

1 file changed

+54
-31
lines changed

buildSrc/src/main/java/org/springframework/boot/build/classpath/CheckClasspathForUnnecessaryExclusions.java

Lines changed: 54 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.gradle.api.artifacts.ModuleDependency;
3939
import org.gradle.api.artifacts.component.ModuleComponentIdentifier;
4040
import org.gradle.api.artifacts.dsl.DependencyHandler;
41+
import org.gradle.api.artifacts.result.ResolvedArtifactResult;
4142
import org.gradle.api.tasks.Input;
4243
import org.gradle.api.tasks.TaskAction;
4344

@@ -48,6 +49,9 @@
4849
*/
4950
public class CheckClasspathForUnnecessaryExclusions extends DefaultTask {
5051

52+
private static final Map<String, String> SPRING_BOOT_DEPENDENCIES_PROJECT = Collections.singletonMap("path",
53+
":spring-boot-project:spring-boot-dependencies");
54+
5155
private final Map<String, Set<String>> exclusionsByDependencyId = new TreeMap<>();
5256

5357
private final Map<String, Dependency> dependencyById = new HashMap<>();
@@ -63,27 +67,31 @@ public CheckClasspathForUnnecessaryExclusions(DependencyHandler dependencyHandle
6367
ConfigurationContainer configurations) {
6468
this.dependencyHandler = getProject().getDependencies();
6569
this.configurations = getProject().getConfigurations();
66-
this.platform = this.dependencyHandler.create(this.dependencyHandler.platform(this.dependencyHandler
67-
.project(Collections.singletonMap("path", ":spring-boot-project:spring-boot-dependencies"))));
70+
this.platform = this.dependencyHandler.create(
71+
this.dependencyHandler.platform(this.dependencyHandler.project(SPRING_BOOT_DEPENDENCIES_PROJECT)));
6872
getOutputs().upToDateWhen((task) -> true);
6973
}
7074

7175
public void setClasspath(Configuration classpath) {
7276
this.exclusionsByDependencyId.clear();
7377
this.dependencyById.clear();
74-
classpath.getAllDependencies().all((dependency) -> {
75-
if (dependency instanceof ModuleDependency) {
76-
String dependencyId = dependency.getGroup() + ":" + dependency.getName();
77-
Set<ExcludeRule> excludeRules = ((ModuleDependency) dependency).getExcludeRules();
78-
TreeSet<String> exclusions = excludeRules.stream()
79-
.map((rule) -> rule.getGroup() + ":" + rule.getModule())
80-
.collect(Collectors.toCollection(TreeSet::new));
81-
this.exclusionsByDependencyId.put(dependencyId, exclusions);
82-
if (!exclusions.isEmpty()) {
83-
this.dependencyById.put(dependencyId, getProject().getDependencies().create(dependencyId));
84-
}
85-
}
86-
});
78+
classpath.getAllDependencies().all(this::processDependency);
79+
}
80+
81+
private void processDependency(Dependency dependency) {
82+
if (dependency instanceof ModuleDependency) {
83+
processDependency((ModuleDependency) dependency);
84+
}
85+
}
86+
87+
private void processDependency(ModuleDependency dependency) {
88+
String dependencyId = getId(dependency);
89+
TreeSet<String> exclusions = dependency.getExcludeRules().stream().map(this::getId)
90+
.collect(Collectors.toCollection(TreeSet::new));
91+
this.exclusionsByDependencyId.put(dependencyId, exclusions);
92+
if (!exclusions.isEmpty()) {
93+
this.dependencyById.put(dependencyId, getProject().getDependencies().create(dependencyId));
94+
}
8795
}
8896

8997
@Input
@@ -94,33 +102,48 @@ Map<String, Set<String>> getExclusionsByDependencyId() {
94102
@TaskAction
95103
public void checkForUnnecessaryExclusions() {
96104
Map<String, Set<String>> unnecessaryExclusions = new HashMap<>();
97-
for (Entry<String, Set<String>> entry : this.exclusionsByDependencyId.entrySet()) {
98-
String dependencyId = entry.getKey();
99-
Set<String> exclusions = entry.getValue();
105+
this.exclusionsByDependencyId.forEach((dependencyId, exclusions) -> {
100106
if (!exclusions.isEmpty()) {
101107
Dependency toCheck = this.dependencyById.get(dependencyId);
102108
List<String> dependencies = this.configurations.detachedConfiguration(toCheck, this.platform)
103-
.getIncoming().getArtifacts().getArtifacts().stream().map((artifact) -> {
104-
ModuleComponentIdentifier id = (ModuleComponentIdentifier) artifact.getId()
105-
.getComponentIdentifier();
106-
return id.getGroup() + ":" + id.getModule();
107-
}).collect(Collectors.toList());
109+
.getIncoming().getArtifacts().getArtifacts().stream().map(this::getId)
110+
.collect(Collectors.toList());
108111
exclusions.removeAll(dependencies);
109112
if (!exclusions.isEmpty()) {
110113
unnecessaryExclusions.put(dependencyId, exclusions);
111114
}
112115
}
113-
}
116+
});
114117
if (!unnecessaryExclusions.isEmpty()) {
115-
StringBuilder message = new StringBuilder("Unnecessary exclusions detected:");
116-
for (Entry<String, Set<String>> entry : unnecessaryExclusions.entrySet()) {
117-
message.append(String.format("%n %s", entry.getKey()));
118-
for (String exclusion : entry.getValue()) {
119-
message.append(String.format("%n %s", exclusion));
120-
}
118+
throw new GradleException(getExceptionMessage(unnecessaryExclusions));
119+
}
120+
}
121+
122+
private String getExceptionMessage(Map<String, Set<String>> unnecessaryExclusions) {
123+
StringBuilder message = new StringBuilder("Unnecessary exclusions detected:");
124+
for (Entry<String, Set<String>> entry : unnecessaryExclusions.entrySet()) {
125+
message.append(String.format("%n %s", entry.getKey()));
126+
for (String exclusion : entry.getValue()) {
127+
message.append(String.format("%n %s", exclusion));
121128
}
122-
throw new GradleException(message.toString());
123129
}
130+
return message.toString();
131+
}
132+
133+
private String getId(ResolvedArtifactResult artifact) {
134+
return getId((ModuleComponentIdentifier) artifact.getId().getComponentIdentifier());
135+
}
136+
137+
private String getId(ModuleDependency dependency) {
138+
return dependency.getGroup() + ":" + dependency.getName();
139+
}
140+
141+
private String getId(ExcludeRule rule) {
142+
return rule.getGroup() + ":" + rule.getModule();
143+
}
144+
145+
private String getId(ModuleComponentIdentifier identifier) {
146+
return identifier.getGroup() + ":" + identifier.getModule();
124147
}
125148

126149
}

0 commit comments

Comments
 (0)