How to get Maven project version to the bash command line
Previous I issued a question on how to change Maven project vesion from command line which lead me to a new issue.
Previously I was able to get the version number since the version was stored as a property that was easy to grep and parse from the command line (bash). Now that the pom.xml <version>
element is used for this, it no longer is unique since all the dependencies and maybe some others too use this. I think there is no way to get the current version number with a bash script without external tools for parsing XML or some very context-aware sed
command.
The most clean solution in my opinion would be for Maven to hand out this version information. I was thinking of writing a custom maven plugin for retrieving different properties but I thought I'd ask here first.
So, is there any easy way to get the value of ${project.version}
to the command line?
Solution
I had to cd
to the directory manually but that can be done easily. In my bash script I have:
version=`cd $project_loc && mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate -Dexpression=project.version | sed -n -e '/^\[.*\]/ !{ /^[0-9]/ { p; q } }'`
Which gives me the current version that I can then advance. Grepping might be simpler but I thought I'd like as robust as possible, so I'm satisfied with the first line that starts with a number and try to handle this as a version number.
# Advances the last number of the given version string by one.
function advance_version () {
local v=$1
# Get the last number. First remove any suffixes (such as '-SNAPSHOT').
local cleaned=`echo $v | sed -e 's/[^0-9][^0-9]*$//'`
local last_num=`echo $cleaned | sed -e 's/[0-9]*\.//g'`
local next_num=$(($last_num+1))
# Finally replace the last number in version string with the new one.
echo $v | sed -e "s/[0-9][0-9]*\([^0-9]*\)$/$next_num/"
}
And I use this by simply calling:
new_version=$(advance_version $version)
Solution 1:
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
Solution 2:
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)
Solution 3:
After doing some research I found the following:
- 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)
-
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)
-
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.
Solution 4:
mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate -Dexpression=project.version | grep -v '\['
Solution 5:
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