Get the version of your Maven project on command line?

I bet you have been faced with the situation to get the version of your Maven project on command line? So the question is how to get it? I have seen many solutions via using Linux tools like grep, cat or awk etc. also seen many solutions like using exec-maven-plugin like this:

1VERSION=$(mvn -q \
2  -Dexec.executable="echo" \
3  -Dexec.args='${project.version}' \
4  --non-recursive \
5  org.codehaus.mojo:exec-maven-plugin:1.6.0:exec)

Unfortunately the above approach will not work on Windows.

Starting with Maven Help Plugin Release 3.1.0 this can be done more easier like the following:

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

This will exactly print out the version of your artifact on stdout and nothing else. Also no line feed is printed out.

So this can be used easily in scripts like this:

1VERSION=$(mvn org.apache.maven.plugins:maven-help-plugin:3.1.0:evaluate -Dexpression=project.version -q -DforceStdout)

If you have correctly pinned the version of your plugins in your pom file or in a parent pom of the project this can be simplified like this:

1VERSION=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout)

Apart from extracting the version you can also extract other information from the pom file like the following:

1GROUPID=$(mvn help:evaluate -Dexpression=project.groupId -q -DforceStdout)

More sophisticated expressions like the following are possible too:

1GROUPID=$(mvn help:evaluate \
2  -Dexpression=project.dependencyManagement.dependencies[0].groupId \
3  -q -DforceStdout \
4)