views:

53

answers:

1

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.

+5  A: 

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
Pascal Thivent
+1 perfect solution
seanizer
Great, thank you! Now one last issue for me is how should I get the version of a certain child project? I tried setting the `artifact` property, but it seems to give me `LATEST`.
mkko
@mkko: Do you mean, how to get the version of a specific child when running maven from an aggregating parent?
Pascal Thivent
@pascal Yes. I understand I could just `cd` and run maven there but changing directories in a script is not nice, at least in this case.
mkko
@mkko: Yes, I understand too :) I just wanted to get confirmation. But I have to think a bit (and I'm not sure I'll find a nice solution).
Pascal Thivent
Okay thanks. I think this is the closest I'll get to a clean solution. Any ideas how could I surpress the maven output and filter out those `[INFO]` messages? I didn't find a switch for maven. Otherwise I'll just add some command line scripting to parse the version number.
mkko
@mkko: I didn't find a solution to "query" the child modules from the parent. And I don't know how to skip the `[INFO]` stuff so I'm afraid you'll have to grep what you want from the script.
Pascal Thivent
Thank you Pascal. I can live with that since the most critical part, writing and reading the version is not command line magic.
mkko