Search code examples
mavenxpathpom.xmlxmllint

Pulling out the version in the pom.xml


I want to append the Jenkins build number in my pom.xml file. I can do this via the Maven Version plugin, but first I need the original version from the pom.xml file.

In order to do this, I am using the command xmllint --xpath '/project/version/text()' pom.xml command. However, I keep getting XPath set is empty

Here's the sample pom.xml file:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <artifactId>das-artifact</artifactId>
  <name>das-name</name>
  <version>4.2</version>
</project>

And here's my output:

$ xmllint --nsclean --xpath '/project/version/text()' pom.xml
XPath set is empty
$

However, if I change the xmlns element to fooxmlns, it works:

Here's the sample pom.xml file:

<project fooxmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <artifactId>das-artifact</artifactId>
  <name>das-name</name>
  <version>4.2</version>
</project>

And here's my output:

$ xmllint --nsclean --xpath '/project/version/text()' pom.xml
4.2$

I'm not too familiar with all the stuff xmllint can do. Can I get it to ignore the xmlns namespace attribute?


Solution

  • There in general two solutions either you go the path which @A_Di-Matteo has suggested (but there I would suggest to use at least Maven 3.3.9 or 3.4.0 if released) or you can go via a build-helper-maven-plugin like the following:

     mvn build-helper:parse-version versions:set \
         -DnewVersion=\${parsedVersion.majorVersion}.\${parsedVersion.minorVersion}.\${parsedVersion.incrementalVersion}-WhatEverYouLikeToAdd \
         versions:commit