Skip to content

Commit 8fc59e3

Browse files
committed
Improve bom checking and address reported problems
Closes gh-38250
1 parent fc6d4ef commit 8fc59e3

File tree

5 files changed

+84
-40
lines changed

5 files changed

+84
-40
lines changed

buildSrc/src/main/java/org/springframework/boot/build/bom/BomPlugin.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ public void apply(Project project) {
6262
createApiEnforcedConfiguration(project);
6363
BomExtension bom = project.getExtensions()
6464
.create("bom", BomExtension.class, project.getDependencies(), project);
65-
project.getTasks().create("bomrCheck", CheckBom.class, bom);
65+
CheckBom checkBom = project.getTasks().create("bomrCheck", CheckBom.class, bom);
66+
project.getTasks().named("check").configure((check) -> check.dependsOn(checkBom));
6667
project.getTasks().create("bomrUpgrade", UpgradeBom.class, bom);
6768
project.getTasks().create("moveToSnapshots", MoveToSnapshots.class, bom);
6869
new PublishingCustomizer(project, bom).customize();

buildSrc/src/main/java/org/springframework/boot/build/bom/CheckBom.java

Lines changed: 65 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,25 @@
1616

1717
package org.springframework.boot.build.bom;
1818

19+
import java.util.ArrayList;
20+
import java.util.List;
1921
import java.util.Set;
2022
import java.util.TreeSet;
2123
import java.util.stream.Collectors;
2224

2325
import javax.inject.Inject;
2426

27+
import org.apache.maven.artifact.versioning.ArtifactVersion;
28+
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
29+
import org.apache.maven.artifact.versioning.Restriction;
30+
import org.apache.maven.artifact.versioning.VersionRange;
2531
import org.gradle.api.DefaultTask;
26-
import org.gradle.api.InvalidUserDataException;
32+
import org.gradle.api.GradleException;
2733
import org.gradle.api.tasks.TaskAction;
2834

2935
import org.springframework.boot.build.bom.Library.Group;
3036
import org.springframework.boot.build.bom.Library.Module;
37+
import org.springframework.boot.build.bom.Library.ProhibitedVersion;
3138
import org.springframework.boot.build.bom.bomr.version.DependencyVersion;
3239

3340
/**
@@ -46,18 +53,41 @@ public CheckBom(BomExtension bom) {
4653

4754
@TaskAction
4855
void checkBom() {
56+
List<String> errors = new ArrayList<>();
4957
for (Library library : this.bom.getLibraries()) {
50-
for (Group group : library.getGroups()) {
51-
for (Module module : group.getModules()) {
52-
if (!module.getExclusions().isEmpty()) {
53-
checkExclusions(group.getId(), module, library.getVersion().getVersion());
54-
}
58+
checkLibrary(library, errors);
59+
}
60+
if (!errors.isEmpty()) {
61+
System.out.println();
62+
errors.forEach(System.out::println);
63+
System.out.println();
64+
throw new GradleException("Bom check failed. See previous output for details.");
65+
}
66+
}
67+
68+
private void checkLibrary(Library library, List<String> errors) {
69+
List<String> libraryErrors = new ArrayList<>();
70+
checkExclusions(library, libraryErrors);
71+
checkProhibitedVersions(library, libraryErrors);
72+
if (!libraryErrors.isEmpty()) {
73+
errors.add(library.getName());
74+
for (String libraryError : libraryErrors) {
75+
errors.add(" - " + libraryError);
76+
}
77+
}
78+
}
79+
80+
private void checkExclusions(Library library, List<String> errors) {
81+
for (Group group : library.getGroups()) {
82+
for (Module module : group.getModules()) {
83+
if (!module.getExclusions().isEmpty()) {
84+
checkExclusions(group.getId(), module, library.getVersion().getVersion(), errors);
5585
}
5686
}
5787
}
5888
}
5989

60-
private void checkExclusions(String groupId, Module module, DependencyVersion version) {
90+
private void checkExclusions(String groupId, Module module, DependencyVersion version, List<String> errors) {
6191
Set<String> resolved = getProject().getConfigurations()
6292
.detachedConfiguration(
6393
getProject().getDependencies().create(groupId + ":" + module.getName() + ":" + version))
@@ -87,8 +117,34 @@ private void checkExclusions(String groupId, Module module, DependencyVersion ve
87117
}
88118
exclusions.removeAll(resolved);
89119
if (!unused.isEmpty()) {
90-
throw new InvalidUserDataException(
91-
"Unnecessary exclusions on " + groupId + ":" + module.getName() + ": " + exclusions);
120+
errors.add("Unnecessary exclusions on " + groupId + ":" + module.getName() + ": " + exclusions);
121+
}
122+
}
123+
124+
private void checkProhibitedVersions(Library library, List<String> errors) {
125+
ArtifactVersion currentVersion = new DefaultArtifactVersion(library.getVersion().getVersion().toString());
126+
for (ProhibitedVersion prohibited : library.getProhibitedVersions()) {
127+
if (prohibited.isProhibited(library.getVersion().getVersion().toString())) {
128+
errors.add("Current version " + currentVersion + " is prohibited");
129+
}
130+
else {
131+
VersionRange versionRange = prohibited.getRange();
132+
if (versionRange != null) {
133+
for (Restriction restriction : versionRange.getRestrictions()) {
134+
ArtifactVersion upperBound = restriction.getUpperBound();
135+
if (upperBound == null) {
136+
return;
137+
}
138+
int comparison = currentVersion.compareTo(upperBound);
139+
if ((restriction.isUpperBoundInclusive() && comparison <= 0)
140+
|| ((!restriction.isUpperBoundInclusive()) && comparison < 0)) {
141+
return;
142+
}
143+
}
144+
errors.add("Version range " + versionRange + " is ineffective as the current version, "
145+
+ currentVersion + ", is greater than its upper bound");
146+
}
147+
}
92148
}
93149
}
94150

buildSrc/src/main/java/org/springframework/boot/build/bom/Library.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.List;
2121
import java.util.Locale;
2222

23+
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
2324
import org.apache.maven.artifact.versioning.VersionRange;
2425

2526
import org.springframework.boot.build.bom.bomr.version.DependencyVersion;
@@ -141,6 +142,16 @@ public String getReason() {
141142
return this.reason;
142143
}
143144

145+
public boolean isProhibited(String candidate) {
146+
boolean result = false;
147+
result = result
148+
|| (this.range != null && this.range.containsVersion(new DefaultArtifactVersion(candidate)));
149+
result = result || this.startsWith.stream().anyMatch(candidate::startsWith);
150+
result = result || this.endsWith.stream().anyMatch(candidate::endsWith);
151+
result = result || this.contains.stream().anyMatch(candidate::contains);
152+
return result;
153+
}
154+
144155
}
145156

146157
public static class LibraryVersion {

buildSrc/src/main/java/org/springframework/boot/build/bom/bomr/UpgradeDependencies.java

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@
3434

3535
import javax.inject.Inject;
3636

37-
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
38-
import org.apache.maven.artifact.versioning.VersionRange;
3937
import org.gradle.api.DefaultTask;
4038
import org.gradle.api.InvalidUserDataException;
4139
import org.gradle.api.internal.tasks.userinput.UserInputHandler;
@@ -49,7 +47,6 @@
4947

5048
import org.springframework.boot.build.bom.BomExtension;
5149
import org.springframework.boot.build.bom.Library;
52-
import org.springframework.boot.build.bom.Library.ProhibitedVersion;
5350
import org.springframework.boot.build.bom.bomr.github.GitHub;
5451
import org.springframework.boot.build.bom.bomr.github.GitHubRepository;
5552
import org.springframework.boot.build.bom.bomr.github.Issue;
@@ -247,17 +244,7 @@ private boolean isAnUpgrade(Library library, DependencyVersion candidate) {
247244
private boolean isNotProhibited(Library library, DependencyVersion candidate) {
248245
return !library.getProhibitedVersions()
249246
.stream()
250-
.anyMatch((prohibited) -> isProhibited(prohibited, candidate.toString()));
251-
}
252-
253-
private boolean isProhibited(ProhibitedVersion prohibited, String candidate) {
254-
boolean result = false;
255-
VersionRange range = prohibited.getRange();
256-
result = result || (range != null && range.containsVersion(new DefaultArtifactVersion(candidate)));
257-
result = result || prohibited.getStartsWith().stream().anyMatch(candidate::startsWith);
258-
result = result || prohibited.getStartsWith().stream().anyMatch(candidate::endsWith);
259-
result = result || prohibited.getStartsWith().stream().anyMatch(candidate::contains);
260-
return result;
247+
.anyMatch((prohibited) -> prohibited.isProhibited(candidate.toString()));
261248
}
262249

263250
private List<Library> matchingLibraries() {

spring-boot-project/spring-boot-dependencies/build.gradle

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ bom {
3535
"activemq-jms-pool",
3636
"activemq-kahadb-store",
3737
"activemq-karaf",
38-
"activemq-leveldb-store" {
39-
exclude group: "commons-logging", module: "commons-logging"
40-
},
38+
"activemq-leveldb-store",
4139
"activemq-log4j-appender",
4240
"activemq-mqtt",
4341
"activemq-openwire-generator",
@@ -86,22 +84,15 @@ bom {
8684
"artemis-commons" {
8785
exclude group: "commons-logging", module: "commons-logging"
8886
},
89-
"artemis-core-client" {
90-
exclude group: "org.apache.geronimo.specs", module: "geronimo-json_1.0_spec"
91-
},
87+
"artemis-core-client",
9288
"artemis-jdbc-store",
93-
"artemis-jms-client" {
94-
exclude group: "org.apache.geronimo.specs", module: "geronimo-json_1.0_spec"
95-
},
96-
"artemis-jms-server" {
97-
exclude group: "org.apache.geronimo.specs", module: "geronimo-json_1.0_spec"
98-
},
89+
"artemis-jms-client",
90+
"artemis-jms-server",
9991
"artemis-journal",
10092
"artemis-quorum-api",
10193
"artemis-selector",
10294
"artemis-server" {
10395
exclude group: "commons-logging", module: "commons-logging"
104-
exclude group: "org.apache.geronimo.specs", module: "geronimo-json_1.0_spec"
10596
},
10697
"artemis-service-extensions"
10798
]
@@ -197,9 +188,7 @@ bom {
197188
"java-driver-bom"
198189
]
199190
modules = [
200-
"java-driver-core" {
201-
exclude group: "org.slf4j", module: "jcl-over-slf4j"
202-
}
191+
"java-driver-core"
203192
]
204193
}
205194
}

0 commit comments

Comments
 (0)