Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 406
David W. Avatar asked Aug 17 '16 13:08

David W.


People also ask

What is version in Maven POM xml?

version. It is the sub element of project. It specifies the version of the artifact under given group. File: pom.xml.

What is model version in POM xml?

model version is the version of project descriptor your POM conforms to. It needs to be included and is set. The value 4.0. 0 just indicated that it is compatible Maven 3.


1 Answers

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
like image 155
khmarbaise Avatar answered Sep 28 '22 07:09

khmarbaise