在Jenkins中设置Maven参数

15

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

我想要同一项目的不同构建具有特定的Maven参数和不同的目标。 (例如,频繁运行编译检查的作业,每小时将应用程序部署到测试服务器的另一个作业,以及发布到暂存区,然后再发布到生产环境的其他作业)

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

1个回答

18

我可以为你提供帮助,以下是我使用的一些选项:

在你的构建中创建配置文件。

<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 B at 0.0 KB/sec) 下载中: 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 at 0.0 KB/sec)
你可以对pom文件的默认部分进行参数化(直接设置默认值并通过执行属性进行覆盖)
最后,你可以使用环境变量 Parameterized BuildParameterized 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

或者在Jenkins中,在“此构建是参数化的”部分中设置testngVersion=6.0


2
需要注意的一件重要事情是,在自由形式的任务中,似乎 Jenkins 构建属性在 Maven 构建任务中被忽略了(Maven 2/3 任务正常工作)。 - mikołak

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接