Skip to content

Commit 2fb8c8d

Browse files
committed
Check starters for unnecessary exclusions
Closes gh-28332
1 parent 26d0afc commit 2fb8c8d

File tree

6 files changed

+140
-13
lines changed

6 files changed

+140
-13
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/*
2+
* Copyright 2012-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.build.classpath;
18+
19+
import java.util.Collections;
20+
import java.util.HashMap;
21+
import java.util.List;
22+
import java.util.Map;
23+
import java.util.Map.Entry;
24+
import java.util.Set;
25+
import java.util.TreeMap;
26+
import java.util.TreeSet;
27+
import java.util.stream.Collectors;
28+
29+
import javax.inject.Inject;
30+
31+
import org.gradle.api.DefaultTask;
32+
import org.gradle.api.GradleException;
33+
import org.gradle.api.Task;
34+
import org.gradle.api.artifacts.Configuration;
35+
import org.gradle.api.artifacts.ConfigurationContainer;
36+
import org.gradle.api.artifacts.Dependency;
37+
import org.gradle.api.artifacts.ExcludeRule;
38+
import org.gradle.api.artifacts.ModuleDependency;
39+
import org.gradle.api.artifacts.component.ModuleComponentIdentifier;
40+
import org.gradle.api.artifacts.dsl.DependencyHandler;
41+
import org.gradle.api.tasks.Input;
42+
import org.gradle.api.tasks.TaskAction;
43+
44+
/**
45+
* A {@link Task} for checking the classpath for unnecessary exclusions.
46+
*
47+
* @author Andy Wilkinson
48+
*/
49+
public class CheckClasspathForUnnecessaryExclusions extends DefaultTask {
50+
51+
private final Map<String, Set<String>> exclusionsByDependencyId = new TreeMap<>();
52+
53+
private final Map<String, Dependency> dependencyById = new HashMap<>();
54+
55+
private final Dependency platform;
56+
57+
private final DependencyHandler dependencyHandler;
58+
59+
private final ConfigurationContainer configurations;
60+
61+
@Inject
62+
public CheckClasspathForUnnecessaryExclusions(DependencyHandler dependencyHandler,
63+
ConfigurationContainer configurations) {
64+
this.dependencyHandler = getProject().getDependencies();
65+
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"))));
68+
getOutputs().upToDateWhen((task) -> true);
69+
}
70+
71+
public void setClasspath(Configuration classpath) {
72+
this.exclusionsByDependencyId.clear();
73+
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+
});
87+
}
88+
89+
@Input
90+
Map<String, Set<String>> getExclusionsByDependencyId() {
91+
return this.exclusionsByDependencyId;
92+
}
93+
94+
@TaskAction
95+
public void checkForUnnecessaryExclusions() {
96+
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();
100+
if (!exclusions.isEmpty()) {
101+
Dependency toCheck = this.dependencyById.get(dependencyId);
102+
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());
108+
exclusions.removeAll(dependencies);
109+
if (!exclusions.isEmpty()) {
110+
unnecessaryExclusions.put(dependencyId, exclusions);
111+
}
112+
}
113+
}
114+
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+
}
121+
}
122+
throw new GradleException(message.toString());
123+
}
124+
}
125+
126+
}

buildSrc/src/main/java/org/springframework/boot/build/starters/StarterPlugin.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.springframework.boot.build.DeployedPlugin;
3535
import org.springframework.boot.build.classpath.CheckClasspathForConflicts;
3636
import org.springframework.boot.build.classpath.CheckClasspathForProhibitedDependencies;
37+
import org.springframework.boot.build.classpath.CheckClasspathForUnnecessaryExclusions;
3738
import org.springframework.util.StringUtils;
3839

3940
/**
@@ -62,6 +63,7 @@ public void apply(Project project) {
6263
(artifact) -> artifact.builtBy(starterMetadata));
6364
createClasspathConflictsCheck(runtimeClasspath, project);
6465
createProhibitedDependenciesCheck(runtimeClasspath, project);
66+
createUnnecessaryExclusionsCheck(runtimeClasspath, project);
6567
configureJarManifest(project);
6668
}
6769

@@ -81,6 +83,14 @@ private void createProhibitedDependenciesCheck(Configuration classpath, Project
8183
project.getTasks().getByName(JavaBasePlugin.CHECK_TASK_NAME).dependsOn(checkClasspathForProhibitedDependencies);
8284
}
8385

86+
private void createUnnecessaryExclusionsCheck(Configuration classpath, Project project) {
87+
CheckClasspathForUnnecessaryExclusions checkClasspathForUnnecessaryExclusions = project.getTasks().create(
88+
"check" + StringUtils.capitalize(classpath.getName() + "ForUnnecessaryExclusions"),
89+
CheckClasspathForUnnecessaryExclusions.class);
90+
checkClasspathForUnnecessaryExclusions.setClasspath(classpath);
91+
project.getTasks().getByName(JavaBasePlugin.CHECK_TASK_NAME).dependsOn(checkClasspathForUnnecessaryExclusions);
92+
}
93+
8494
private void configureJarManifest(Project project) {
8595
project.getTasks().withType(Jar.class, (jar) -> project.afterEvaluate((evaluated) -> {
8696
jar.manifest((manifest) -> {

spring-boot-project/spring-boot-starters/spring-boot-starter-data-couchbase-reactive/build.gradle

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,5 @@ dependencies {
88
api(project(":spring-boot-project:spring-boot-starters:spring-boot-starter"))
99
api("io.projectreactor:reactor-core")
1010
api("io.reactivex:rxjava-reactive-streams")
11-
api("org.springframework.data:spring-data-couchbase") {
12-
exclude group: "com.couchbase.client", module: "encryption"
13-
}
11+
api("org.springframework.data:spring-data-couchbase")
1412
}

spring-boot-project/spring-boot-starters/spring-boot-starter-data-couchbase/build.gradle

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,5 @@ description = "Starter for using Couchbase document-oriented database and Spring
66

77
dependencies {
88
api(project(":spring-boot-project:spring-boot-starters:spring-boot-starter"))
9-
api("org.springframework.data:spring-data-couchbase") {
10-
exclude group: "com.couchbase.client", module: "encryption"
11-
}
9+
api("org.springframework.data:spring-data-couchbase")
1210
}

spring-boot-project/spring-boot-starters/spring-boot-starter-data-solr/build.gradle

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,5 @@ description = "Starter for using the Apache Solr search platform with Spring Dat
66

77
dependencies {
88
api(project(":spring-boot-project:spring-boot-starters:spring-boot-starter"))
9-
api("org.springframework.data:spring-data-solr") {
10-
exclude group: "commons-logging", module: "commons-logging"
11-
exclude group: "org.slf4j", module: "jcl-over-slf4j"
12-
}
9+
api("org.springframework.data:spring-data-solr")
1310
}

spring-boot-project/spring-boot-starters/spring-boot-starter-test/build.gradle

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,5 @@ dependencies {
1818
api("org.skyscreamer:jsonassert")
1919
api("org.springframework:spring-core")
2020
api("org.springframework:spring-test")
21-
api("org.xmlunit:xmlunit-core") {
22-
exclude group: "javax.xml.bind", module: "jaxb-api"
23-
}
21+
api("org.xmlunit:xmlunit-core")
2422
}

0 commit comments

Comments
 (0)