|
16 | 16 |
|
17 | 17 | package org.springframework.boot.context.embedded;
|
18 | 18 |
|
| 19 | +import java.io.File; |
19 | 20 | import java.io.FileReader;
|
20 | 21 |
|
21 | 22 | import javax.xml.xpath.XPath;
|
22 |
| -import javax.xml.xpath.XPathExpression; |
23 | 23 | import javax.xml.xpath.XPathFactory;
|
24 | 24 |
|
25 | 25 | import org.xml.sax.InputSource;
|
26 | 26 |
|
| 27 | +import org.springframework.util.StringUtils; |
| 28 | + |
27 | 29 | /**
|
28 | 30 | * Provides access to dependency versions by querying the project's pom.
|
29 | 31 | *
|
30 | 32 | * @author Andy Wilkinson
|
31 | 33 | */
|
32 | 34 | final class Versions {
|
33 | 35 |
|
| 36 | + private static final String PROPERTIES = "/*[local-name()='project']/*[local-name()='properties']"; |
| 37 | + |
34 | 38 | private Versions() {
|
35 | 39 | }
|
36 | 40 |
|
37 | 41 | public static String getBootVersion() {
|
38 |
| - return evaluateExpression( |
39 |
| - "/*[local-name()='project']/*[local-name()='parent']/*[local-name()='version']" |
40 |
| - + "/text()"); |
| 42 | + String baseDir = StringUtils.cleanPath(new File(".").getAbsolutePath()); |
| 43 | + String mainBaseDir = evaluateExpression("pom.xml", |
| 44 | + PROPERTIES + "/*[local-name()='main.basedir']/text()"); |
| 45 | + mainBaseDir = mainBaseDir.replace("${basedir}", baseDir); |
| 46 | + return evaluateExpression(mainBaseDir + "/pom.xml", |
| 47 | + PROPERTIES + "/*[local-name()='revision']/text()"); |
41 | 48 | }
|
42 | 49 |
|
43 |
| - private static String evaluateExpression(String expression) { |
| 50 | + private static String evaluateExpression(String file, String expression) { |
44 | 51 | try {
|
45 |
| - XPathFactory xPathFactory = XPathFactory.newInstance(); |
46 |
| - XPath xpath = xPathFactory.newXPath(); |
47 |
| - XPathExpression expr = xpath.compile(expression); |
48 |
| - String version = expr.evaluate(new InputSource(new FileReader("pom.xml"))); |
49 |
| - return version; |
| 52 | + InputSource source = new InputSource(new FileReader(file)); |
| 53 | + XPath xpath = XPathFactory.newInstance().newXPath(); |
| 54 | + return xpath.compile(expression).evaluate(source); |
50 | 55 | }
|
51 | 56 | catch (Exception ex) {
|
52 | 57 | throw new IllegalStateException("Failed to evaluate expression", ex);
|
|
0 commit comments