1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package net.brabenetz.app.springstompserver.tools;
21
22 import org.w3c.dom.Document;
23 import org.w3c.dom.Element;
24 import org.w3c.dom.Node;
25 import org.w3c.dom.NodeList;
26 import org.xml.sax.SAXException;
27
28 import javax.xml.parsers.DocumentBuilder;
29 import javax.xml.parsers.DocumentBuilderFactory;
30 import javax.xml.parsers.ParserConfigurationException;
31 import javax.xml.transform.Transformer;
32 import javax.xml.transform.TransformerFactory;
33 import javax.xml.transform.dom.DOMSource;
34 import javax.xml.transform.stream.StreamResult;
35
36 import java.io.File;
37 import java.io.IOException;
38 import java.util.ArrayList;
39 import java.util.Collections;
40 import java.util.HashMap;
41 import java.util.List;
42 import java.util.Map;
43
44 import static org.junit.jupiter.api.Assertions.assertTrue;
45
46
47
48
49 public class ExtractPomVersions {
50
51
52
53
54 public static void main(final String[] args) throws Exception {
55 new ExtractPomVersions().extractVersionsIntoProperties();
56 }
57
58 private void extractVersionsIntoProperties() throws ParserConfigurationException, SAXException, IOException, Exception {
59 File pomFile = new File("pom.xml");
60
61 DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
62 DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
63 Document doc = dBuilder.parse(pomFile);
64
65 Map<String, String> extractedProperties = new HashMap<>();
66
67 extractedProperties.putAll(extractProperties(doc.getElementsByTagName("plugin"), "plugin-"));
68 extractedProperties.putAll(extractProperties(doc.getElementsByTagName("dependency"), "dependency-"));
69 addProperties(doc, extractedProperties);
70
71 writeDocument(doc, pomFile);
72 System.out.println("Success updated pom.xml");
73 }
74
75 private void addProperties(final Document doc, final Map<String, String> extractedProperties) {
76 List<String> properties = new ArrayList<>(extractedProperties.keySet());
77 Collections.sort(properties);
78 for (String property : properties) {
79 List<Node> propertiesNode = getChildElementsByTagName(doc.getDocumentElement(), "properties");
80 Element propertyElement = doc.createElement(property);
81 propertyElement.setTextContent(extractedProperties.get(property));
82 propertiesNode.get(0).appendChild(propertyElement);
83 }
84 }
85
86 private void writeDocument(final Document doc, final File file) throws Exception {
87
88 TransformerFactory tFactory = TransformerFactory.newInstance();
89 Transformer transformer = tFactory.newTransformer();
90 DOMSource source = new DOMSource(doc);
91 StreamResult result = new StreamResult(file);
92 transformer.transform(source, result);
93 }
94
95 private Map<String, String> extractProperties(final NodeList pluginList, final String propertyPrefix) {
96 Map<String, String> extractedProperties = new HashMap<>();
97
98 for (int i = 0; i < pluginList.getLength(); i++) {
99
100 Node pluginNode = pluginList.item(i);
101
102 if (pluginNode.getNodeType() == Node.ELEMENT_NODE) {
103
104 Element pluginElement = (Element) pluginNode;
105
106
107
108
109 String artifactId = getTextContent(pluginElement, "artifactId");
110 String version = getTextContent(pluginElement, "version");
111 if (version != null && !version.startsWith("${")) {
112 String propertyName = propertyPrefix + artifactId + ".version";
113 setTextContent(pluginElement, "version", "${" + propertyName + "}");
114 extractedProperties.put(propertyName, version);
115 }
116
117 }
118 }
119 return extractedProperties;
120 }
121
122 private String getTextContent(final Element element, final String tagName) {
123 List<Node> elements = getChildElementsByTagName(element, tagName);
124 if (elements.size() > 1) {
125 throw new IllegalArgumentException(
126 "The element '" + tagName + "' was found more than ones (" + elements.size() + "). " + element.getTextContent());
127 }
128 if (elements.size() == 1) {
129 return elements.get(0).getTextContent();
130 }
131 return null;
132 }
133
134 private void setTextContent(final Element element, final String tagName, final String newValue) {
135 List<Node> elements = getChildElementsByTagName(element, tagName);
136 assertTrue(elements.size() == 1, () -> String.format("Expected one element for '%s' but got %s", tagName, elements.size()));
137 elements.get(0).setTextContent(newValue);
138 }
139
140 private List<Node> getChildElementsByTagName(final Element element, final String tagName) {
141 List<Node> childNodesByTagName = new ArrayList<>();
142 NodeList childNodes = element.getChildNodes();
143 for (int i = 0; i < childNodes.getLength(); i++) {
144
145 Node childNode = childNodes.item(i);
146 if (tagName.equals(childNode.getNodeName())) {
147 childNodesByTagName.add(childNode);
148 }
149 }
150
151 return childNodesByTagName;
152 }
153 }