在 Jenkins 中设置 Maven 参数 [英] Setting Maven params in Jenkins

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

问题描述

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

I am experimenting the Jenkins, and am looking for a way to allow Jenkins to set parameters for different project builds. Normally all these attributes are stored in the settings.xml (I currently have a settings.xml for the user running Jenkins which includes default properties and my repositories).

我希望对同一项目进行不同的构建,以指定不同的 Maven 参数和不同的目标.(有一个经常运行编译检查的工作,另一个每小时将应用程序部署到测试服务器,另一个用于发布到暂存然后生产)

I want to have different builds of the same project that specific different Maven parameters and also different goals. (have a job that runs compile checks frequently, another that deploys the app to the test server every hour, another for releasing to staging and then prod)

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

What is the best way for creating parameterized builds in Jenkins?

推荐答案

有一些选项供您选择.这是我正在使用的:

There are some options for you. Here's what I'm using:

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

The you can define in jenkins clean install -P package - for package task or clean install for normal build

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.

If you run it normally testng version 6.4 will be used. But if you run it like: mvn clean install -Dtestng.version=6.3.1 testng version 6.3.1 will be used.

下载: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 为 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/秒)

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 的默认部分(直接设置默认值并通过执行属性覆盖它)

You can parametrize default part of pom (setting default values directly and overriding it by execution properties)

上一个示例版本的更改:

Change in last example version:

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

在 bash 中,您可以调用:

In bash you can call:

export testngVersion=6.0
mvn clean install

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

Or in jenkins by setting testngVersion=6.0 in This build is parameterized section

这篇关于在 Jenkins 中设置 Maven 参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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