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 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 opinnion 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? Thanks in advance.
Solution
Thank you for the help. 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 simplier 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)
Hope this helps someone.
The easy to understand all-in-one solution that outputs the maven project version, and suppresses extraneous output from
[INFO]
andDownload
messages:Same thing, but split onto two lines:
Outputs:
4.3-SNAPSHOT
So, using your
project.version
in a simple bash script:Other solutions on this page didn't seem to combine all the tricks into one.
The Maven Help Plugin is somehow already proposing something for this:
Here is how you would invoke it on the command line to get the
${project.version}
:I've recently developed the Release Candidate Maven plugin that solves this exact problem so that you don't have to resort to any hacky shell scripts and parsing the output of the
maven-help-plugin
.For example, to print the version of your Maven project to a terminal, run:
which gives output similar to
maven-help-plugin
:However, you can also specify an arbitrary output format (so that the version could be picked up from the log by a CI server such as TeamCity):
Which results in:
To save the output to a file (so that a CI server such as Jenkins could use it):
The resulting
version.properties
file will look as follows:On top of all the above, Release Candidate also allows you to set the version of your project (which is something you'd probably do on your CI server) based on the API version you've defined in your POM.
If you'd like to see an example of Release Candidate being used as part of the Maven lifecycle, have a look at the
pom.xml
of my other open-source project - Build Monitor for Jenkins.Exec plugin works without any output parsing because output can be redirected into file and injected back into the job environment via EnvInject plugin:
A simple maven only solution
And for bonus points parsed part of a version
Should be easier since this bug is fixed in maven-help-plugin 3.0.0: MPH-99 Evaluate has no output in quiet mode.