我正在试验 Jenkins,并且正在寻找一种方法来允许 Jenkins 为不同的项目build设置参数。通常所有这些属性都存储在 settings.xml 中(我目前有一个用于运行 Jenkins 的用户的 settings.xml,其中包括默认属性和我的存储库)。
我希望对同一个项目有不同的构建,这些构建具有不同的 Maven 参数和不同的目标。 (有一个经常运行编译检查的工作,另一个每小时将应用程序部署到测试服务器,另一个用于发布到登台然后生产)
在 Jenkins 中创建参数化构建的最佳方式是什么?
最佳答案
您有一些选择。这是我正在使用的:
<profile>
<activation>
<file>
<exists>findbugs-exclude.xml</exists>
</file>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<configuration>
<excludeFilterFile>findbugs-exclude.xml</excludeFilterFile>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>package</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
<includeScope>runtime</includeScope>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
您可以在 jenkins 中定义 clean install -P package
- 用于打包任务或 clean install
用于正常构建
Maven 调用:
mvn clean install -Dparameter.one=ONE -Dparameter.two=TWO
POM:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>test</artifactId>
<version>1.0.0</version>
<name>test</name>
<properties>
<testng.version>6.4</testng.version>
</properties>
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>${testng.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
如果您正常运行,将使用 testng 6.4 版。但是如果你像这样运行它:
mvn clean install -Dtestng.version=6.3.1
将使用 testng 版本 6.3.1。
见
Downloading: http://repo.maven.apache.org/maven2/org/testng/testng/6.3.1/testng-6.3.1.pom Downloaded: http://repo.maven.apache.org/maven2/org/testng/testng/6.3.1/testng-6.3.1.pom (0 B at 0.0 KB/sec) Downloading: http://repo.maven.apache.org/maven2/org/testng/testng/6.3.1/testng-6.3.1.jar Downloaded: http://repo.maven.apache.org/maven2/org/testng/testng/6.3.1/testng-6.3.1.jar (0 B at 0.0 KB/sec)
您可以参数化 pom 的默认部分(直接设置默认值并通过执行属性覆盖它)
上一个示例版本的更改:
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>${env.testngVersion}</version>
<scope>test</scope>
</dependency>
在 bash 中你可以调用:
export testngVersion=6.0
mvn clean install
或者在 jenkins 中通过在 This build is parameterized
部分设置 testngVersion=6.0
https://stackoverflow.com/questions/9471460/