Skip to content

Instantly share code, notes, and snippets.

@rafaeltuelho
Last active February 2, 2021 13:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rafaeltuelho/d73ee5fe63cc142e4df1360e327bd342 to your computer and use it in GitHub Desktop.
Save rafaeltuelho/d73ee5fe63cc142e4df1360e327bd342 to your computer and use it in GitHub Desktop.
Ways to get GAV from your POM using bash script
  1. 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"
  1. 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)
  1. 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment