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.
Either you have
mvn
give you the answer (as most answers suggest), or you extract the answer from thepom.xml
. The only drawback of the second approach is that you can very easily extract the value of the<version/>
tag, but it will be meaningful only if it's literal, that is, not a Maven property. I chose this approach anyway because:mvn
is way to verbose and I simply don't like filtering its output.mvn
is very slow compared to reading thepom.xml
.<version/>
.mvn-version
is azsh
shell script that usesxmlstarlet
to read thepom.xml
and print the version of the project (if it exists) or the version of the parent project (if it exists):The advantage is that it's way quicker than running
mvn
:The difference on my machine is greater than two orders of magnitude.
This is what I used to get the version number, thought there would have been a better maven way to do so
If you don't mind to write the version into a temporary file, there is another solution (without grep/sed) that works well for me. (EDIT: see rjrjr's answer for a much simpler solution without any temporary file hassle)
I use the Exec Maven Plugin along with the
echo
binary. In contrast to the Maven Help Plugin, the Exec Plugin allows output redirection into a file, which can be used to bypass grep/sed, and makes it even possible to parse strange things like multiline version strings (with CDATA block in version tag), at least to a certain extent.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.
I have been doing some research and found the following:
Maven has been blamed for not being easily integrable within system operation scripts since it does not follow some good practices regarding CLI tools. (ref: https://youtu.be/1ILEw6Qca3U?t=372)
Inspired on the previous assertion, I decided to give a look on maven's source code as well as on maven-help-plugin. It seems that they have fixed a little bit the maven's -q switch (I am using version 3.5.3), 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:
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 is 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:
The commit description is available at: (https://github.com/apache/maven-help-plugin/commit/316656983d780c04031bbadd97d4ab245c84d014)