Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Maven project version to the bash command line

People also ask

How do I know what version of POM I have?

Check the following goals: versions:display-dependency-updates scans a project's dependencies and produces a report of those dependencies which have newer versions available. versions:display-plugin-updates scans a project's plugins and produces a report of those plugins which have newer versions available.


The Maven Help Plugin is somehow already proposing something for this:

  • help:evaluate evaluates Maven expressions given by the user in an interactive mode.

Here is how you would invoke it on the command line to get the ${project.version}:

mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate \
    -Dexpression=project.version

Tom's solution with the Exec Maven Plugin is much better, but still more complicated than it needs to be. For me it's as simple as:

MVN_VERSION=$(mvn -q \
    -Dexec.executable=echo \
    -Dexec.args='${project.version}' \
    --non-recursive \
    exec:exec)

After doing some research I found the following:

  1. Maven has been blamed because integration with DevOps tools is not easy due to the fact that it does not follow some good practices regarding CLI tools, i.e:

http://www.faqs.org/docs/artu/ch01s06.html (not available anymore) "The Unix Way" (ii) Expect the output of every program to become the input of another, as yet unknown, program. Don't clutter output with extraneous information. Avoid stringently columnar or binary input formats. Don't insist on interactive input. What does it actually mean? Your output should be:

  • "grepable": (One "record" per line)
  • "cutable": (Delimited "fields")
  • exit codes: 0 for success, nonzero for failure.
  • messaging (stderr) vs. output (stdout)

(ref: Make awesome command line apps with ruby by Dave Copeland Jan 18, 2012 https://youtu.be/1ILEw6Qca3U?t=372)

  1. Frankly Dave Copeland is right when he said that maven does't play fairly with others. So I decided to give a look to maven's source code as well as to maven-help-plugin's source code as well. It seems that they have fixed a little bit the maven's -q switch (I was using version 3.5.3 at that time), so now if you pass it, you won't get all the annoying non-sense logging stuff that prevents maven from being used within automated scripts. So you should be able to use something like this:

    mvn help:evaluate -Dexpression=project.version -q
    

The problem is that this command prints nothing because by default the help plugin outputs through the logger which has been silenced by the -q switch. (latest available version of the plugin at that time was 3.1.0 released on June, 3rd 2018)

  1. Karl Heinz Marbaise (https://github.com/khmarbaise) fixed it by adding an optional parameter that allows you to call it in the following way:

    mvn help:evaluate -Dexpression=project.version -q -DforceStdout
    

The commit description is available at: (https://github.com/apache/maven-help-plugin/commit/316656983d780c04031bbadd97d4ab245c84d014)

Again, you should always verify the exit code of the command and redirect all stderr to /dev/null on unix.


mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate -Dexpression=project.version | grep -v '\['

The top answer is pretty garbage in my opinion, you have to use a bunch of grep to hack out the maven console output. Why not use the right tool for the job? Using xpath syntax is the best approach to retrieving the version number, since it is the intended method of accessing a XML data structure. The expression below is traversing the pom using the "local name" of the elements, in other words ignoring namespace declarations which may or may not be present in the xml.

xmllint --xpath "//*[local-name()='project']/*[local-name()='version']/text()" pom.xml

This is the cleanest solution there is:

mvn org.apache.maven.plugins:maven-help-plugin:3.2.0:evaluate \
-Dexpression=project.version -q -DforceStdout

Advantages:

  • This works fine on all operating systems and all shells.
  • No need for any external tools!
  • [important] This works even if project version is inherited from parent pom.xml

Note:

  • maven-help-plugin version 3.2.0 (and above) has forceStdout option. You may replace 3.2.0 in above command with a newer version from the list of available versions of mvn-help-plugin from artifactory, if available.
  • Option -q suppresses verbose messages ([INFO], [WARN] etc.)

Alternatively, you can add this entry in your pom.xml, under plugins section:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-help-plugin</artifactId>
    <version>3.2.0</version>
</plugin>

and then run above command compactly as follows:

mvn help:evaluate -Dexpression=project.groupId -q -DforceStdout

If you want to fetch groupId and artifactId as well, check this answer.