Skip to content

Commit eb7e7b6

Browse files
committed
Merge branch '3.1.x' into 3.2.x
Closes gh-40532
2 parents 5707ca8 + d2a5bb3 commit eb7e7b6

File tree

6 files changed

+221
-24
lines changed

6 files changed

+221
-24
lines changed

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

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,17 @@ public void library(String name, Action<LibraryHandler> action) {
111111

112112
public void library(String name, String version, Action<LibraryHandler> action) {
113113
ObjectFactory objects = this.project.getObjects();
114-
LibraryHandler libraryHandler = objects.newInstance(LibraryHandler.class, (version != null) ? version : "");
114+
LibraryHandler libraryHandler = objects.newInstance(LibraryHandler.class, this.project,
115+
(version != null) ? version : "");
115116
action.execute(libraryHandler);
116117
LibraryVersion libraryVersion = new LibraryVersion(DependencyVersion.parse(libraryHandler.version));
117-
VersionAlignment versionAlignment = (libraryHandler.alignWithVersion != null)
118-
? new VersionAlignment(libraryHandler.alignWithVersion.from, libraryHandler.alignWithVersion.managedBy,
119-
this.project, this.libraries, libraryHandler.groups)
118+
VersionAlignment versionAlignment = (libraryHandler.alignWith.version != null)
119+
? new VersionAlignment(libraryHandler.alignWith.version.from,
120+
libraryHandler.alignWith.version.managedBy, this.project, this.libraries, libraryHandler.groups)
120121
: null;
121122
addLibrary(new Library(name, libraryHandler.calendarName, libraryVersion, libraryHandler.groups,
122-
libraryHandler.prohibitedVersions, libraryHandler.considerSnapshots, versionAlignment));
123+
libraryHandler.prohibitedVersions, libraryHandler.considerSnapshots, versionAlignment,
124+
libraryHandler.alignWith.dependencyManagementDeclaredIn));
123125
}
124126

125127
public void effectiveBomArtifact() {
@@ -219,17 +221,18 @@ public static class LibraryHandler {
219221

220222
private final List<ProhibitedVersion> prohibitedVersions = new ArrayList<>();
221223

224+
private final AlignWithHandler alignWith;
225+
222226
private boolean considerSnapshots = false;
223227

224228
private String version;
225229

226230
private String calendarName;
227231

228-
private AlignWithVersionHandler alignWithVersion;
229-
230232
@Inject
231-
public LibraryHandler(String version) {
233+
public LibraryHandler(Project project, String version) {
232234
this.version = version;
235+
this.alignWith = project.getObjects().newInstance(AlignWithHandler.class);
233236
}
234237

235238
public void version(String version) {
@@ -258,9 +261,8 @@ public void prohibit(Action<ProhibitedHandler> action) {
258261
handler.endsWith, handler.contains, handler.reason));
259262
}
260263

261-
public void alignWithVersion(Action<AlignWithVersionHandler> action) {
262-
this.alignWithVersion = new AlignWithVersionHandler();
263-
action.execute(this.alignWithVersion);
264+
public void alignWith(Action<AlignWithHandler> action) {
265+
action.execute(this.alignWith);
264266
}
265267

266268
public static class ProhibitedHandler {
@@ -380,18 +382,35 @@ public void setClassifier(String classifier) {
380382

381383
}
382384

383-
public static class AlignWithVersionHandler {
385+
public static class AlignWithHandler {
386+
387+
private VersionHandler version;
384388

385-
private String from;
389+
private String dependencyManagementDeclaredIn;
386390

387-
private String managedBy;
391+
public void version(Action<VersionHandler> action) {
392+
this.version = new VersionHandler();
393+
action.execute(this.version);
394+
}
388395

389-
public void from(String from) {
390-
this.from = from;
396+
public void dependencyManagementDeclaredIn(String bomCoordinates) {
397+
this.dependencyManagementDeclaredIn = bomCoordinates;
391398
}
392399

393-
public void managedBy(String managedBy) {
394-
this.managedBy = managedBy;
400+
public static class VersionHandler {
401+
402+
private String from;
403+
404+
private String managedBy;
405+
406+
public void from(String from) {
407+
this.from = from;
408+
}
409+
410+
public void managedBy(String managedBy) {
411+
this.managedBy = managedBy;
412+
}
413+
395414
}
396415

397416
}

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

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

19+
import java.io.File;
1920
import java.util.ArrayList;
2021
import java.util.List;
2122
import java.util.Set;
@@ -36,6 +37,7 @@
3637
import org.springframework.boot.build.bom.Library.Module;
3738
import org.springframework.boot.build.bom.Library.ProhibitedVersion;
3839
import org.springframework.boot.build.bom.Library.VersionAlignment;
40+
import org.springframework.boot.build.bom.ManagedDependencies.Difference;
3941
import org.springframework.boot.build.bom.bomr.version.DependencyVersion;
4042

4143
/**
@@ -71,6 +73,7 @@ private void checkLibrary(Library library, List<String> errors) {
7173
checkExclusions(library, libraryErrors);
7274
checkProhibitedVersions(library, libraryErrors);
7375
checkVersionAlignment(library, libraryErrors);
76+
checkDependencyManagementAlignment(library, libraryErrors);
7477
if (!libraryErrors.isEmpty()) {
7578
errors.add(library.getName());
7679
for (String libraryError : libraryErrors) {
@@ -174,4 +177,40 @@ private void checkVersionAlignment(Library library, List<String> errors) {
174177
}
175178
}
176179

180+
private void checkDependencyManagementAlignment(Library library, List<String> errors) {
181+
String alignsWithBom = library.getAlignsWithBom();
182+
if (alignsWithBom == null) {
183+
return;
184+
}
185+
File bom = resolveBom(library, alignsWithBom);
186+
ManagedDependencies managedByBom = ManagedDependencies.ofBom(bom);
187+
ManagedDependencies managedByLibrary = ManagedDependencies.ofLibrary(library);
188+
Difference diff = managedByBom.diff(managedByLibrary);
189+
if (!diff.isEmpty()) {
190+
String error = "Dependency management does not align with " + library.getAlignsWithBom() + ":";
191+
if (!diff.missing().isEmpty()) {
192+
error = error + "%n - Missing:%n %s"
193+
.formatted(String.join("\n ", diff.missing()));
194+
}
195+
if (!diff.unexpected().isEmpty()) {
196+
error = error + "%n - Unexpected:%n %s"
197+
.formatted(String.join("\n ", diff.unexpected()));
198+
}
199+
errors.add(error);
200+
}
201+
}
202+
203+
private File resolveBom(Library library, String alignsWithBom) {
204+
String coordinates = alignsWithBom + ":" + library.getVersion().getVersion() + "@pom";
205+
Set<File> files = getProject().getConfigurations()
206+
.detachedConfiguration(getProject().getDependencies().create(coordinates))
207+
.getResolvedConfiguration()
208+
.getFiles();
209+
if (files.size() != 1) {
210+
throw new IllegalStateException(
211+
"Expected a single file but '" + coordinates + "' resolved to " + files.size());
212+
}
213+
return files.iterator().next();
214+
}
215+
177216
}

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ public class Library {
5959

6060
private final VersionAlignment versionAlignment;
6161

62+
private final String alignsWithBom;
63+
6264
/**
6365
* Create a new {@code Library} with the given {@code name}, {@code version}, and
6466
* {@code groups}.
@@ -70,9 +72,12 @@ public class Library {
7072
* @param prohibitedVersions version of the library that are prohibited
7173
* @param considerSnapshots whether to consider snapshots
7274
* @param versionAlignment version alignment, if any, for the library
75+
* @param alignsWithBom the coordinates of the bom, if any, that this library should
76+
* inline
7377
*/
7478
public Library(String name, String calendarName, LibraryVersion version, List<Group> groups,
75-
List<ProhibitedVersion> prohibitedVersions, boolean considerSnapshots, VersionAlignment versionAlignment) {
79+
List<ProhibitedVersion> prohibitedVersions, boolean considerSnapshots, VersionAlignment versionAlignment,
80+
String alignsWithBom) {
7681
this.name = name;
7782
this.calendarName = (calendarName != null) ? calendarName : name;
7883
this.version = version;
@@ -82,6 +87,7 @@ public Library(String name, String calendarName, LibraryVersion version, List<Gr
8287
this.prohibitedVersions = prohibitedVersions;
8388
this.considerSnapshots = considerSnapshots;
8489
this.versionAlignment = versionAlignment;
90+
this.alignsWithBom = alignsWithBom;
8591
}
8692

8793
public String getName() {
@@ -116,6 +122,10 @@ public VersionAlignment getVersionAlignment() {
116122
return this.versionAlignment;
117123
}
118124

125+
public String getAlignsWithBom() {
126+
return this.alignsWithBom;
127+
}
128+
119129
/**
120130
* A version or range of versions that are prohibited from being used in a bom.
121131
*/
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/*
2+
* Copyright 2012-2024 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.bom;
18+
19+
import java.io.File;
20+
import java.io.FileReader;
21+
import java.util.HashMap;
22+
import java.util.HashSet;
23+
import java.util.Map;
24+
import java.util.Set;
25+
26+
import javax.xml.parsers.DocumentBuilderFactory;
27+
import javax.xml.xpath.XPath;
28+
import javax.xml.xpath.XPathConstants;
29+
import javax.xml.xpath.XPathFactory;
30+
31+
import org.w3c.dom.Document;
32+
import org.w3c.dom.Node;
33+
import org.w3c.dom.NodeList;
34+
import org.xml.sax.InputSource;
35+
36+
import org.springframework.boot.build.bom.Library.Group;
37+
import org.springframework.boot.build.bom.Library.Module;
38+
39+
/**
40+
* Managed dependencies from a bom or library.
41+
*
42+
* @author Andy Wilkinson
43+
*/
44+
class ManagedDependencies {
45+
46+
private final Set<String> ids;
47+
48+
ManagedDependencies(Set<String> ids) {
49+
this.ids = ids;
50+
}
51+
52+
Set<String> getIds() {
53+
return this.ids;
54+
}
55+
56+
Difference diff(ManagedDependencies other) {
57+
Set<String> missing = new HashSet<>(this.ids);
58+
missing.removeAll(other.ids);
59+
Set<String> unexpected = new HashSet<>(other.ids);
60+
unexpected.removeAll(this.ids);
61+
return new Difference(missing, unexpected);
62+
}
63+
64+
static ManagedDependencies ofBom(File bom) {
65+
try {
66+
Document bomDocument = DocumentBuilderFactory.newInstance()
67+
.newDocumentBuilder()
68+
.parse(new InputSource(new FileReader(bom)));
69+
XPath xpath = XPathFactory.newInstance().newXPath();
70+
NodeList dependencyNodes = (NodeList) xpath
71+
.evaluate("/project/dependencyManagement/dependencies/dependency", bomDocument, XPathConstants.NODESET);
72+
NodeList propertyNodes = (NodeList) xpath.evaluate("/project/properties/*", bomDocument,
73+
XPathConstants.NODESET);
74+
Map<String, String> properties = new HashMap<>();
75+
for (int i = 0; i < propertyNodes.getLength(); i++) {
76+
Node property = propertyNodes.item(i);
77+
String name = property.getNodeName();
78+
String value = property.getTextContent();
79+
properties.put("${%s}".formatted(name), value);
80+
}
81+
Set<String> managedDependencies = new HashSet<>();
82+
for (int i = 0; i < dependencyNodes.getLength(); i++) {
83+
Node dependency = dependencyNodes.item(i);
84+
String groupId = (String) xpath.evaluate("groupId/text()", dependency, XPathConstants.STRING);
85+
String artifactId = (String) xpath.evaluate("artifactId/text()", dependency, XPathConstants.STRING);
86+
String version = (String) xpath.evaluate("version/text()", dependency, XPathConstants.STRING);
87+
String classifier = (String) xpath.evaluate("classifier/text()", dependency, XPathConstants.STRING);
88+
if (version.startsWith("${") && version.endsWith("}")) {
89+
version = properties.get(version);
90+
}
91+
managedDependencies.add(asId(groupId, artifactId, version, classifier));
92+
}
93+
return new ManagedDependencies(managedDependencies);
94+
}
95+
catch (Exception ex) {
96+
throw new RuntimeException(ex);
97+
}
98+
}
99+
100+
static String asId(String groupId, String artifactId, String version, String classifier) {
101+
String id = groupId + ":" + artifactId + ":" + version;
102+
if (classifier != null && classifier.length() > 0) {
103+
id = id + ":" + classifier;
104+
}
105+
return id;
106+
}
107+
108+
static ManagedDependencies ofLibrary(Library library) {
109+
Set<String> managedByLibrary = new HashSet<>();
110+
for (Group group : library.getGroups()) {
111+
for (Module module : group.getModules()) {
112+
managedByLibrary.add(asId(group.getId(), module.getName(), library.getVersion().getVersion().toString(),
113+
module.getClassifier()));
114+
}
115+
}
116+
return new ManagedDependencies(managedByLibrary);
117+
}
118+
119+
record Difference(Set<String> missing, Set<String> unexpected) {
120+
121+
boolean isEmpty() {
122+
return this.missing.isEmpty() && this.unexpected.isEmpty();
123+
}
124+
125+
}
126+
127+
}

buildSrc/src/test/java/org/springframework/boot/build/bom/bomr/UpgradeApplicatorTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ void whenUpgradeIsAppliedToLibraryWithVersionThenBomIsUpdated() throws IOExcepti
5353
FileCopyUtils.copy(new File("src/test/resources/gradle.properties"), gradleProperties);
5454
new UpgradeApplicator(bom.toPath(), gradleProperties.toPath())
5555
.apply(new Upgrade(new Library("ActiveMQ", null, new LibraryVersion(DependencyVersion.parse("5.15.11")),
56-
null, null, false, null), DependencyVersion.parse("5.16")));
56+
null, null, false, null, null), DependencyVersion.parse("5.16")));
5757
String bomContents = Files.readString(bom.toPath());
5858
assertThat(bomContents).hasSize(originalContents.length() - 3);
5959
}
@@ -66,7 +66,7 @@ void whenUpgradeIsAppliedToLibraryWithVersionPropertyThenGradlePropertiesIsUpdat
6666
FileCopyUtils.copy(new File("src/test/resources/gradle.properties"), gradleProperties);
6767
new UpgradeApplicator(bom.toPath(), gradleProperties.toPath())
6868
.apply(new Upgrade(new Library("Kotlin", null, new LibraryVersion(DependencyVersion.parse("1.3.70")), null,
69-
null, false, null), DependencyVersion.parse("1.4")));
69+
null, false, null, null), DependencyVersion.parse("1.4")));
7070
Properties properties = new Properties();
7171
try (InputStream in = new FileInputStream(gradleProperties)) {
7272
properties.load(in);

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,9 +1071,11 @@ bom {
10711071
}
10721072
}
10731073
library("Neo4j Java Driver", "5.19.0") {
1074-
alignWithVersion {
1075-
from "org.springframework.data:spring-data-neo4j"
1076-
managedBy "Spring Data Bom"
1074+
alignWith {
1075+
version {
1076+
from "org.springframework.data:spring-data-neo4j"
1077+
managedBy "Spring Data Bom"
1078+
}
10771079
}
10781080
group("org.neo4j.driver") {
10791081
modules = [

0 commit comments

Comments
 (0)