maven - 在 Jenkins 中设置 Maven 参数

我正在试验 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 用于正常构建

从命令行直接将参数放入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。

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

终于可以使用环境变量了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

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

https://stackoverflow.com/questions/9471460/

相关文章:

xcode - 如何在 Xcode 4 的运行脚本构建阶段中使用 sudo?

unit-testing - 在 Visual Studio 中进行单元测试时会重建不必要的项目

c++ - 尝试构建 boost 时更新 58 个目标失败,发生了什么?

maven - 在maven中构建模块时如何构建依赖项目

.net - NuGet Enterprise - 不同成熟度级别包的最佳实践

java - 目前是否可以通过 Maven 构建 Eclipse 插件并具有良好的 IDE 集成?

javascript - Bower 和 NPM 如何结合在一起?

build - 告诉 cmake 保持安静

ios - 无法运行 faSTLane 叹息。 "Couldn' t下载证书"

javascript - 为什么我要同时使用 TypeScript 和 Babel?