Skip to content

Commit 845c97f

Browse files
committed
Merge branch '2.7.x' into 3.0.x
Closes gh-35924
2 parents 4c8b93b + c46bef1 commit 845c97f

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright 2023 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.Set;
20+
import java.util.stream.Collectors;
21+
22+
import org.gradle.api.DefaultTask;
23+
import org.gradle.api.GradleException;
24+
import org.gradle.api.artifacts.Configuration;
25+
import org.gradle.api.artifacts.component.ModuleComponentSelector;
26+
import org.gradle.api.artifacts.result.DependencyResult;
27+
import org.gradle.api.artifacts.result.ResolutionResult;
28+
import org.gradle.api.file.FileCollection;
29+
import org.gradle.api.tasks.Classpath;
30+
import org.gradle.api.tasks.TaskAction;
31+
32+
/**
33+
* Tasks to check that none of classpath's direct dependencies are unconstrained.
34+
*
35+
* @author Andy Wilkinson
36+
*/
37+
public class CheckClasspathForUnconstrainedDirectDependencies extends DefaultTask {
38+
39+
private Configuration classpath;
40+
41+
public CheckClasspathForUnconstrainedDirectDependencies() {
42+
getOutputs().upToDateWhen((task) -> true);
43+
}
44+
45+
@Classpath
46+
public FileCollection getClasspath() {
47+
return this.classpath;
48+
}
49+
50+
public void setClasspath(Configuration classpath) {
51+
this.classpath = classpath;
52+
}
53+
54+
@TaskAction
55+
void checkForUnconstrainedDirectDependencies() {
56+
ResolutionResult resolutionResult = this.classpath.getIncoming().getResolutionResult();
57+
Set<? extends DependencyResult> dependencies = resolutionResult.getRoot().getDependencies();
58+
Set<String> unconstrainedDependencies = dependencies.stream()
59+
.map(DependencyResult::getRequested)
60+
.filter(ModuleComponentSelector.class::isInstance)
61+
.map(ModuleComponentSelector.class::cast)
62+
.map((selector) -> selector.getGroup() + ":" + selector.getModule())
63+
.collect(Collectors.toSet());
64+
Set<String> constraints = resolutionResult.getAllDependencies()
65+
.stream()
66+
.filter(DependencyResult::isConstraint)
67+
.map(DependencyResult::getRequested)
68+
.filter(ModuleComponentSelector.class::isInstance)
69+
.map(ModuleComponentSelector.class::cast)
70+
.map((selector) -> selector.getGroup() + ":" + selector.getModule())
71+
.collect(Collectors.toSet());
72+
unconstrainedDependencies.removeAll(constraints);
73+
if (!unconstrainedDependencies.isEmpty()) {
74+
throw new GradleException("Found unconstrained direct dependencies: " + unconstrainedDependencies);
75+
}
76+
}
77+
78+
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.springframework.boot.build.ConventionsPlugin;
3434
import org.springframework.boot.build.DeployedPlugin;
3535
import org.springframework.boot.build.classpath.CheckClasspathForConflicts;
36+
import org.springframework.boot.build.classpath.CheckClasspathForUnconstrainedDirectDependencies;
3637
import org.springframework.boot.build.classpath.CheckClasspathForUnnecessaryExclusions;
3738
import org.springframework.util.StringUtils;
3839

@@ -63,6 +64,7 @@ public void apply(Project project) {
6364
(artifact) -> artifact.builtBy(starterMetadata));
6465
createClasspathConflictsCheck(runtimeClasspath, project);
6566
createUnnecessaryExclusionsCheck(runtimeClasspath, project);
67+
createUnconstrainedDirectDependenciesCheck(runtimeClasspath, project);
6668
configureJarManifest(project);
6769
}
6870

@@ -82,6 +84,17 @@ private void createUnnecessaryExclusionsCheck(Configuration classpath, Project p
8284
project.getTasks().getByName(JavaBasePlugin.CHECK_TASK_NAME).dependsOn(checkClasspathForUnnecessaryExclusions);
8385
}
8486

87+
private void createUnconstrainedDirectDependenciesCheck(Configuration classpath, Project project) {
88+
CheckClasspathForUnconstrainedDirectDependencies checkClasspathForUnconstrainedDirectDependencies = project
89+
.getTasks()
90+
.create("check" + StringUtils.capitalize(classpath.getName() + "ForUnconstrainedDirectDependencies"),
91+
CheckClasspathForUnconstrainedDirectDependencies.class);
92+
checkClasspathForUnconstrainedDirectDependencies.setClasspath(classpath);
93+
project.getTasks()
94+
.getByName(JavaBasePlugin.CHECK_TASK_NAME)
95+
.dependsOn(checkClasspathForUnconstrainedDirectDependencies);
96+
}
97+
8598
private void configureJarManifest(Project project) {
8699
project.getTasks().withType(Jar.class, (jar) -> project.afterEvaluate((evaluated) -> {
87100
jar.manifest((manifest) -> {

0 commit comments

Comments
 (0)