Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
331 views
in Technique[技术] by (71.8m points)

parameters - Setting Maven params in Jenkins

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).

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)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

Create profiles in your build.

<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>

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

Put parameters directly to pom from command line:

Maven call:

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>

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.

See

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)

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

Finally you can use environment variables Parameterized Build or Parameterized Trigger Plugin

Change in last example version:

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

In bash you can call:

export testngVersion=6.0
mvn clean install

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...