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条回答
Summer. ? 凉城
2楼-- · 2019-01-05 09:05

Maven footer is pretty standard:

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.609s
[INFO] Finished at: Wed May 21 18:02:38 MSK 2014
[INFO] Final Memory: 17M/736M
[INFO] ------------------------------------------------------------------------

So you can use the following code:

> version=$(mvn help:evaluate -Dexpression=project.version | tail -8 | head -1)
> echo $version
查看更多
迷人小祖宗
3楼-- · 2019-01-05 09:11
python -c "import xml.etree.ElementTree as ET; \
  print(ET.parse(open('pom.xml')).getroot().find( \
  '{http://maven.apache.org/POM/4.0.0}version').text)"

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.

查看更多
祖国的老花朵
4楼-- · 2019-01-05 09:13

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 and
  • org.slf4j.simpleLogger.log.org.apache.maven.plugins.help=INFO

as documented at http://www.slf4j.org/api/org/slf4j/impl/SimpleLogger.html

MAVEN_OPTS="\
-Dorg.slf4j.simpleLogger.defaultLogLevel=WARN \
-Dorg.slf4j.simpleLogger.log.org.apache.maven.plugins.help=INFO" \
mvn help:evaluate -o -Dexpression=project.version

As a result, one can run simply tail -1 and get:

$ MAVEN_OPTS="\
-Dorg.slf4j.simpleLogger.defaultLogLevel=WARN \
-Dorg.slf4j.simpleLogger.log.org.apache.maven.plugins.help=INFO" \
mvn help:evaluate -o -Dexpression=project.version | tail -1

1.0.0-SNAPSHOT

Note that this is a one-liner. MAVEN_OPTS are being rewritten only for this particular mvn execution.

查看更多
贪生不怕死
5楼-- · 2019-01-05 09:14

This will avoid the need for grepping off log entries from the output:

mvn -Dexec.executable='echo' -Dexec.args='${project.version}' --non-recursive exec:exec -q
查看更多
Root(大扎)
6楼-- · 2019-01-05 09:14

I found right balance for me. After mvn package maven-archiver plugin creates target/maven-archiver/pom.properties with contents like this

version=0.0.1-SNAPSHOT
groupId=somegroup
artifactId=someArtifact

and I am using bash just to execute it

. ./target/maven-archiver/pom.properties

then

echo $version
0.0.1-SNAPSHOT

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.

查看更多
一纸荒年 Trace。
7楼-- · 2019-01-05 09:14

This worked for me, offline and without depending on mvn:

VERSION=$(grep --max-count=1 '<version>' <your_path>/pom.xml | awk -F '>' '{ print $2 }' | awk -F '<' '{ print $1 }')
echo $VERSION
查看更多
登录 后发表回答