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.
Maven footer is pretty standard:
So you can use the following code:
As long as you have python 2.5 or greater, this should work. If you have a lower version than that, install
python-lxml
and change the import to lxml.etree. This method is quick and doesn't require downloading any extra plugins. It also works on malformed pom.xml files that don't validate with xmllint, like the ones I need to parse. Tested on Mac and Linux.Just for the record, it's possible to configure Maven's Simple SLF4J logging directly in the command line to output only what we need by configuring:
org.slf4j.simpleLogger.defaultLogLevel=WARN
andorg.slf4j.simpleLogger.log.org.apache.maven.plugins.help=INFO
as documented at http://www.slf4j.org/api/org/slf4j/impl/SimpleLogger.html
As a result, one can run simply
tail -1
and get:Note that this is a one-liner.
MAVEN_OPTS
are being rewritten only for this particularmvn
execution.This will avoid the need for grepping off log entries from the output:
I found right balance for me. After
mvn package
maven-archiver plugin createstarget/maven-archiver/pom.properties
with contents like thisand I am using bash just to execute it
then
Of course this is not safe at all to execute this file, but execution can easily be converted into perl or bash script to read and set environment variable from that file.
This worked for me, offline and without depending on mvn: