问题描述
在处理POM之后,Jenkins构建有什么方法可以知道项目的Maven版本号吗?
Is there any way a Jenkins build can be aware of the Maven version number of a project after processing the POM?
我有一些项目由Maven控制版本控制,在构建后的工作中,我们想创建一个Debian软件包并调用一些shell脚本.我需要的是Maven过去可以作为Jenkins环境变量使用的版本号,这样我就可以将其传递给构建后操作.
I've got some projects where versioning is controlled by Maven, and in a post-build job we'd like to create a Debian package and call some shell scripts. What I need is for the version number that Maven used to be available as a Jenkins environment variable so I can pass it to post-build actions.
需要明确的是,我不是要知道如何让Jenkins将版本号传递给Maven.相反,我希望Maven将版本号传递给Jenkins!
To be clear, I'm not needing to know how to get Jenkins to pass a version number to Maven; instead I want Maven to pass a version number to Jenkins!
推荐答案
经过大量的挖掘(我从未意识到Jenkins的文档记录很差!),我找到了一个非常简单的解决方案.
After a lot of digging around (I never realised how poorly-documented Jenkins is!) I found a quite trivial solution.
- 安装Groovy插件
- 将
Post Step
添加到类型为Execute **system** Groovy script
的Maven版本中 - 粘贴以下Groovy片段:
- Install the Groovy plugin
- Add a
Post Step
to your Maven build of typeExecute **system** Groovy script
- Paste in the following snippet of Groovy:
脚本:
import hudson.model.*;
import hudson.util.*;
def thr = Thread.currentThread();
def currentBuild = thr?.executable;
def mavenVer = currentBuild.getParent().getModules().toArray()[0].getVersion();
def newParamAction = new hudson.model.ParametersAction(new hudson.model.StringParameterValue("MAVEN_VERSION", mavenVer));
currentBuild.addAction(newParamAction);
现在可以使用通常的方式(${MAVEN_VERSION}
)将名为MAVEN_VERSION
的构建环境变量替换为其他构建后步骤.我正在将它用于Git标记等.
The build environment variable called MAVEN_VERSION
will now be available for substitution into other post-build steps in the usual manner (${MAVEN_VERSION}
). I'm using it for Git tagging amongst other things.
这篇关于从Jenkins的Maven POM获取项目版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!