在 Jenkins 中从 Maven POM 获取项目版本 [英] Getting Project Version from Maven POM in Jenkins

查看:109
本文介绍了在 Jenkins 中从 Maven POM 获取项目版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在处理 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.

  1. 安装 Groovy 插件
  2. Post Step 添加到 Execute **system** Groovy script
  3. 类型的 Maven 构建中
  4. 粘贴以下 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 获取项目版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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