之前我发布了一个关于如何从命令行更改Maven项目版本的问题,这导致我遇到了一个新问题。

以前我能够获得版本号,因为版本存储为一个属性,很容易从命令行(bash)进行grep和解析。现在pom.xml <version>元素用于此,它不再是唯一的,因为所有依赖项(也许其他一些依赖项也使用它)。我认为,如果没有用于解析XML的外部工具或一些非常能感知上下文的sed命令,就无法用bash脚本获得当前版本号。

在我看来,最干净的解决方案是Maven发布这个版本信息。我想写一个自定义的maven插件检索不同的属性,但我想我应该先问这里。

那么,是否有任何简单的方法来获取${project的值。版本}到命令行?

解决方案

我必须手动cd到该目录,但这可以很容易地做到。在我的bash脚本我有:

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可能更简单,但我认为我想尽可能健壮,所以我对以数字开头的第一行感到满意,并尝试将其作为版本号处理。

# 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)

当前回答

mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate -Dexpression=project.version | grep -v '\['

其他回答

一种替代方法是使用yq (https://github.com/kislyuk/yq)进行解析,如下所示:

cat pom.xml | xq -r '.project.version'

注意,可执行文件是xq而不是yq

为了获得xq,像pip安装yq那样安装yq

在做了一些研究之后,我发现了以下几点:

Maven一直被指责,因为与DevOps工具的集成并不容易,因为它没有遵循一些关于CLI工具的良好实践,例如:

http://www.faqs.org/docs/artu/ch01s06.html (not available anymore) "The Unix Way" (ii) Expect the output of every program to become the input of another, as yet unknown, program. Don't clutter output with extraneous information. Avoid stringently columnar or binary input formats. Don't insist on interactive input. What does it actually mean? Your output should be: "grepable": (One "record" per line) "cutable": (Delimited "fields") exit codes: 0 for success, nonzero for failure. messaging (stderr) vs. output (stdout)

(参考:Dave Copeland于2012年1月18日用ruby制作很棒的命令行应用程序https://youtu.be/1ILEw6Qca3U?t=372)

Honesty I think Dave Copeland was right when he said that maven does't play fairly with others. So I decided to give a look to maven's source code as well as to maven-help-plugin's source code as well. It seems that they have fixed a little bit the maven's -q switch (I was using version 3.5.3 at that time), 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

问题是这个命令不会输出任何东西,因为默认情况下,帮助插件通过记录器输出,而记录器已被-q开关静音。(当时插件的最新可用版本是2018年6月3日发布的3.1.0)

Karl Heinz Marbaise (https://github.com/khmarbaise)通过添加一个可选参数来修复它,允许你以以下方式调用它: mvn help:evaluate -Dexpression=project。version -q -DforceStdout

提交描述可在以下网站获得:(https://github.com/apache/maven-help-plugin/commit/316656983d780c04031bbadd97d4ab245c84d014)

同样,您应该始终验证命令的退出码,并在unix上将所有stderr重定向到/dev/null。

基于这个问题,我使用下面的脚本自动增加所有maven父/子模块中的版本号:

#!/usr/bin/env bash  

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

version=$(mvn org.apache.maven.plugins:maven-help-plugin:3.2.0:evaluate -Dexpression=project.version -q -DforceStdout)  

new\_version=$(advance\_version $version)  

mvn versions:set -DnewVersion=${new\_version} -DprocessAllModules -DgenerateBackupPoms=false

我最近开发了发布候选Maven插件来解决这个确切的问题,这样您就不必求助于任何hack shell脚本并解析Maven -help-插件的输出。

例如,要将Maven项目的版本打印到终端,运行:

mvn com.smartcodeltd:release-candidate-maven-plugin:LATEST:version

输出类似于maven-help-plugin:

[INFO] Detected version: '1.0.0-SNAPSHOT'
1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

但是,您也可以指定任意的输出格式(这样CI服务器(如TeamCity)就可以从日志中获取版本):

mvn com.smartcodeltd:release-candidate-maven-plugin:LATEST:version \
   -DoutputTemplate="##teamcity[setParameter name='env.PROJECT_VERSION' value='{{ version }}']"

结果是:

[INFO] Detected version: '1.0.0-SNAPSHOT'
##teamcity[setParameter name='env.PROJECT_VERSION' value='1.0.0-SNAPSHOT']
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

将输出保存到一个文件中(这样CI服务器(如Jenkins)就可以使用它):

mvn com.smartcodeltd:release-candidate-maven-plugin:LATEST:version \
   -DoutputTemplate="PROJECT_VERSION={{ version }}" \
   -DoutputUri="file://\${project.basedir}/version.properties"

最终的版本。属性文件将如下所示:

PROJECT_VERSION=1.0.0-SNAPSHOT

在上述所有功能之上,Release Candidate还允许您基于在POM中定义的API版本设置项目的版本(这可能是您在CI服务器上所做的事情)。

如果您希望看到一个Release Candidate作为Maven生命周期一部分使用的示例,请查看我的另一个开源项目——Build Monitor for Jenkins的pom.xml。

我在用其他答案的时候遇到了一些特例,所以这是另一种选择。

version=$(printf 'VER\t${project.version}' | mvn help:evaluate | grep '^VER' | cut -f2)