- using native bash commands (
echo, cat, xmlint, sed
)
Ref: https://stackoverflow.com/a/16962213/1010947
This approach should work in most of the Linux base continer images. No need to instll additionl pkgs!
pom="source/pom.xml"
groupId=$(echo "cat //*[local-name()='project']/*[local-name()='groupId']" | xmllint --shell $pom | sed '/^\/ >/d' | sed 's/<[^>]*.//g')
artifactId=$(echo "cat //*[local-name()='project']/*[local-name()='artifactId']" | xmllint --shell $pom | sed '/^\/ >/d' | sed 's/<[^>]*.//g')
version=$(echo "cat //*[local-name()='project']/*[local-name()='version']" | xmllint --shell $pom | sed '/^\/ >/d' | sed 's/<[^>]*.//g')
echo "G:A:V = $groupId:$artifactId:$version"
- using XmlStarlet utility
Ref: https://stackoverflow.com/a/9030791/1010947
You need to install xmlstarlet in your Linux image. Pay attention to the -N
param that declare the Maven Pom namespace.
XMLStalert is strict to namespaces.
ver=$(xmlstarlet sel -N ns=http://maven.apache.org/POM/4.0.0 -t -m "/ns:project/ns:version" -v "text()" pom.xml)
- Using the Help Maven Plugin
Ref: https://blog.soebes.de/blog/2018/06/09/help-plugin/
mvn org.apache.maven.plugins:maven-help-plugin:3.1.0:evaluate -Dexpression=project.version -q -DforceStdout