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.
I kept running into side cases when using some of the other answers here, so here's yet another alternative.
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:
I noticed some spurious
Downloaded:
lines coming in the output that were breaking my original assignment. Here's the filter I've settled on; hope it helps!EDIT
Not 100% sure why, but when running this through a post-build script in Jenkins, the output was coming out as
[INFO]version
, e.g.[INFO]0.3.2
.I dumped the output to a file and ran it through my first filter directly from BASH, it works fine.., so again, unsure what's going on in Jenkins land.
To get it 100% in Jenkins, I've added a follow-up
sed
filter; here's my latestEDIT
One last note here.. I found out
tr
was still resulting in things like/r/n0.3.2
(again only when running via Jenkins). Switched toawk
and the problem has gone away! My final working resultI'm just adding small
sed
filter improvement I have recently implemented to extractproject.version
from maven output.