How to get Maven project version to the bash comma

2019-01-05 08:33发布

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.

22条回答
爷的心禁止访问
2楼-- · 2019-01-05 09:17

Either you have mvn give you the answer (as most answers suggest), or you extract the answer from the pom.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.
  • Starting mvn is very slow compared to reading the pom.xml.
  • I always use literal values in <version/>.

mvn-version is a zsh shell script that uses xmlstarlet to read the pom.xml and print the version of the project (if it exists) or the version of the parent project (if it exists):

$ mvn-version .
1.0.0-SNAPSHOT

The advantage is that it's way quicker than running mvn:

$ time mvn-version .
1.1.0-SNAPSHOT
mvn-version .  0.01s user 0.01s system 75% cpu 0.019 total

$ time mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate \
> -Dexpression=project.version
mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate   4.17s user 0.21s system 240% cpu 1.823 total

The difference on my machine is greater than two orders of magnitude.

查看更多
甜甜的少女心
3楼-- · 2019-01-05 09:18
VERSION=$(head -50 pom.xml | awk -F'>' '/SNAPSHOT/ {print $2}' | awk -F'<' '{print $1}')

This is what I used to get the version number, thought there would have been a better maven way to do so

查看更多
我命由我不由天
4楼-- · 2019-01-05 09:19

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.

#!/usr/bin/env sh

MVN_VERSION=""
VERSION_FILE=$( mktemp mvn_project_version_XXXXX )
trap "rm -f -- \"$VERSION_FILE\"" INT EXIT

mvn -Dexec.executable="echo" \
    -Dexec.args='${project.version}' \
    -Dexec.outputFile="$VERSION_FILE" \
    --non-recursive \
    --batch-mode \
    org.codehaus.mojo:exec-maven-plugin:1.3.1:exec > /dev/null 2>&1 ||
    { echo "Maven invocation failed!" 1>&2; exit 1; }

# if you just care about the first line of the version, which will be
# sufficent for pretty much every use case I can imagine, you can use
# the read builtin
[ -s "$VERSION_FILE" ] && read -r MVN_VERSION < "$VERSION_FILE"

# Otherwise, you could use cat.
# Note that this still has issues when there are leading whitespaces
# in the multiline version string
#MVN_VERSION=$( cat "$VERSION_FILE" )

printf "Maven project version: %s\n" "$MVN_VERSION"
查看更多
做自己的国王
5楼-- · 2019-01-05 09:21

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.

xmllint --xpath "//*[local-name()='project']/*[local-name()='version']/text()" pom.xml
查看更多
forever°为你锁心
6楼-- · 2019-01-05 09:22
mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate -Dexpression=project.version | grep -v '\['
查看更多
女痞
7楼-- · 2019-01-05 09:25

I have been doing some research and found the following:

  1. 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)

  2. 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:

    mvn help:evaluate -Dexpression=project.version -q
    

    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)

  3. Karl Heinz Marbaise (https://github.com/khmarbaise) fixed it by adding an optional parameter that allows you to call it in the following way:

    mvn help:evaluate -Dexpression=project.version -q -DforceStdout
    

    The commit description is available at: (https://github.com/apache/maven-help-plugin/commit/316656983d780c04031bbadd97d4ab245c84d014)

查看更多
登录 后发表回答