|
| 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 | +} |
0 commit comments