Skip to content

Instantly share code, notes, and snippets.

@wytten
Created September 5, 2017 15:25
Show Gist options
  • Save wytten/56c44d494b6a33868ee4dff1f4cfe665 to your computer and use it in GitHub Desktop.
Save wytten/56c44d494b6a33868ee4dff1f4cfe665 to your computer and use it in GitHub Desktop.
xpath to report maven project version as parsed from pom.xml
public static void main(String[] args) throws Exception {
String uri = "pom.xml";
if (args.length > 0) {
uri = args[0];
}
String path = "/project/version/text()";
if (args.length > 1) {
path = args[1];
}
if (!path.startsWith("/")) {
// grody
// https://stackoverflow.com/questions/39632924/git-bash-is-adding-its-current-path-to-one-of-the-parameter
path = "/" + path;
}
// System.err.println(String.format("%s %s", uri, path));
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(uri);
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr = xpath.compile(path);
String result = expr.evaluate(doc);
System.out.println(result);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment