小编典典

在jenkins中设置Maven参数

jenkins

我正在试验Jenkins,并且正在寻找一种方法来允许Jenkins为不同的项目构建设置参数。通常,所有这些属性都存储在settings.xml中(我目前为运行Jenkins的用户提供了settings.xml,其中包括默认属性和我的存储库)。

我想要同一项目的不同版本,这些版本特定于不同的Maven参数以及不同的目标。(有一项工作经常运行编译检查,另一项工作每小时将应用程序部署到测试服务器,另一项工作是发布到暂存然后进行生产)

在Jenkins中创建参数化构建的最佳方法是什么?


阅读 468

收藏
2020-07-25

共1个答案

小编典典

有一些适合您的选择。这是我正在使用的:

在构建中创建配置文件。

<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正常构建

从命令行将参数直接放入pom:

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。

看到

下载:http
:
//repo.maven.apache.org/maven2/org/testng/testng/6.3.1/testng-6.3.1.pom下载:
http
[//repo.maven.apache.org/maven2/org
/testng/testng/6.3.1/testng-6.3.1.pom(0](http://repo.maven.apache.org/maven2/org/testng/testng/6.3.1/testng-6.3.1.pom)
B,0.0 KB
/秒)下载:http
//repo.maven.apache.org/maven2/org/testng/testng/6.3.1
/testng-6.3.1.jar
下载:http
:
//repo.maven.apache.org/maven2/org/testng/testng/6.3.1/testng-6.3.1.jar
(0 B,速度为0.0 KB /秒)

您可以参数化pom的默认部分(直接设置默认值并通过执行属性覆盖它)

最后,您可以使用环境变量[Parameterized Build](http://wiki.jenkins-

ci.org/display/JENKINS/Parameterized+Build)或Parameterized Trigger
Plugin

更改上一个示例版本:

<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>${env.testngVersion}</version>
    <scope>test</scope>
</dependency>

在bash中,您可以调用:

export testngVersion=6.0
mvn clean install

或者在詹金斯中通过在This build is parameterized部分中设置testngVersion = 6.0

2020-07-25