[转] Maven 从命令行获取项目的版本号

【From】https://blog.soebes.de/blog/2018/06/09/help-plugin/

 

I bet you have been faced with the situation to get the version of your Maven project on command line? So the question is how to get it? I have seen many solutions via using Linux tools like grepcat or awk etc. also seen many solutions like using exec-maven-plugin like this:

1
2
3
4
5
VERSION=$(mvn -q \
  -Dexec.executable="echo" \
  -Dexec.args='${project.version}' \
  --non-recursive \
 org.codehaus.mojo:exec-maven-plugin:1.6.0:exec)

Unfortunately the above approach will not work on Windows.

Starting with Maven Help Plugin Release 3.1.0 this can be done more easier like the following:

1
mvn org.apache.maven.plugins:maven-help-plugin:3.1.0:evaluate -Dexpression=project.version -q -DforceStdout

This will exactly print out the version of your artifact on stdout and nothing else. Also no line feed is printed out.

So this can be used easily in scripts like this:

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

If you have correctly pinned the version of your plugins in your pom file or in a parent pom of the project this can be simplified like this:

1
VERSION=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout)

Apart from extracting the version you can also extract other information from the pom file like the following:

1
GROUPID=$(mvn help:evaluate -Dexpression=project.groupId -q -DforceStdout)

More sophisticated expressions like the following are possible too:

1
2
3
4
GROUPID=$(mvn help:evaluate \
  -Dexpression=project.dependencyManagement.dependencies[0].groupId \
  -q -DforceStdout \
)

 

转载于:https://www.cnblogs.com/pekkle/p/10935991.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值