如何在 Maven/Jenkins 作业中替换 POM 中的 属性?
How to replace a property in a POM in a Maven/Jenkins job?
我有一个包含客户端和服务器组件的项目。两者都有自己的 Maven 多模块构建项目。
必须在各种前端模块中引用正确的服务器版本。为此,我在我的客户端父 POM 中设置了一个 属性,如下所示:
<properties>
<server.version>1.2.3</server.version>
</properties>
现在我想在 POM 中更新版本号 (即不只是使用 -D 从命令行注入不同的版本...)during/after 詹金斯构建工作。有办法吗?
你检查过maven版本插件了吗?
(网站已关闭,但插件页面在 google cache 中仍然可用)
mvn -DnewVersion=<version> versions:set
我找到了 com.google.code.maven-replacer-plugin:replacer
Maven 插件,它非常适合我的情况。它接受一个 xpath 和一个正则表达式来定义要在 XML 文件中替换的内容。
示例插件配置:
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
<version>1.5.3</version>
<configuration>
<file>${project.basedir}/pom.xml</file>
<xpath>/project/properties/server.version/text()</xpath>
<token>^.*$</token>
<value>${newServerVersion}</value>
</configuration>
</plugin>
对于每个受影响的 maven 模块,插件可以是 运行:
mvn --non-recursive replacer:replace -DnewServerVersion=xxxx
在 itembase 我们使用 Jenkins 管道插件。它带有一些有用的内置函数,例如 readMavenPom
和 writeMavenPom
。因此,在您的构建管道中,您可以执行以下操作:
def pom = readMavenPom file: 'pom.xml'
//Do some manipulation
writeMavenPom model: pom
我有一个包含客户端和服务器组件的项目。两者都有自己的 Maven 多模块构建项目。
必须在各种前端模块中引用正确的服务器版本。为此,我在我的客户端父 POM 中设置了一个 属性,如下所示:
<properties>
<server.version>1.2.3</server.version>
</properties>
现在我想在 POM 中更新版本号 (即不只是使用 -D 从命令行注入不同的版本...)during/after 詹金斯构建工作。有办法吗?
你检查过maven版本插件了吗? (网站已关闭,但插件页面在 google cache 中仍然可用)
mvn -DnewVersion=<version> versions:set
我找到了 com.google.code.maven-replacer-plugin:replacer
Maven 插件,它非常适合我的情况。它接受一个 xpath 和一个正则表达式来定义要在 XML 文件中替换的内容。
示例插件配置:
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
<version>1.5.3</version>
<configuration>
<file>${project.basedir}/pom.xml</file>
<xpath>/project/properties/server.version/text()</xpath>
<token>^.*$</token>
<value>${newServerVersion}</value>
</configuration>
</plugin>
对于每个受影响的 maven 模块,插件可以是 运行:
mvn --non-recursive replacer:replace -DnewServerVersion=xxxx
在 itembase 我们使用 Jenkins 管道插件。它带有一些有用的内置函数,例如 readMavenPom
和 writeMavenPom
。因此,在您的构建管道中,您可以执行以下操作:
def pom = readMavenPom file: 'pom.xml'
//Do some manipulation
writeMavenPom model: pom