Is there any way a Jenkins build can be aware of the Maven version number of a project after processing the POM?
I\'ve got some projects where versioning is controll
Solution:
POM_VERSION=$( \
xmlstarlet sel \
-N x='http://maven.apache.org/POM/4.0.0' \
-t \
-v '//x:project/x:version/text()' \
pom.xml \
)
Explanation:
You can do this in a one-liner using a command-line XPath tool, such as those mentioned at "How to execute XPath one-liners from shell?". I chose XMLStarlet, but they all have similar syntax.
When parsing a POM, you have to account for namespaces. The docs here helped me figure this out.
In order to get the text for an element in XPath, you use the text() function as explained at XPath: select text node.
My POM looks like this:
4.0.0
com.foo.bar
foobar
1.0.6-SNAPSHOT
jar
The downside here is that if the namespace changes, you have to change the command.