如何获取Maven项目版本到bash命令行 [英] How to get Maven project version to the bash command line

查看:531
本文介绍了如何获取Maven项目版本到bash命令行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上一页我在如何从命令行更改Maven项目版本

Previous I issued a question on how to change Maven project vesion from command line which lead me to a new issue.

以前,我可以获取版本号,因为版本存储为容易grep的属性,并且从命令行(bash)解析。现在pom.xml元素用于这个,它不再是唯一的,因为所有的依赖和可能一些其他人也使用这个。我认为没有办法得到当前版本号与一个bash脚本没有外部工具解析xml或一些非常上下文感知的sed命令。

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.

在我的意见中最干净的解决方案将是Maven提供这个版本信息。我在想一个自定义的maven插件来检索不同的属性,但我想我会先问这里。

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.

所以,有什么简单的方法来获得值 $ {project.version} 到命令行?提前感谢。

So, is there any easy way to get the value of ${project.version} to the command line? Thanks in advance.

解决方案

感谢您的帮助。我必须手动 cd 到目录,但这可以很容易做到。在我的bash脚本中,我有

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 } }'`

这给了我可以提前的当前版本。 grepping可能更简单,但我认为我想尽可能强大,所以我对第一行以一个数字开头,并尝试处理这个版本号令人满意。

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/"
}

我用这个简单调用

new_version=$(advance_version $version)

希望这有助于某人。

推荐答案

Maven帮助插件在某种程度上已经为此提出了一些建议:

The Maven Help Plugin is somehow already proposing something for this:


在命令行上获取 $ {project.version}

mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate \
    -Dexpression=project.version

这篇关于如何获取Maven项目版本到bash命令行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆