Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 364
empire29 Avatar asked Feb 27 '12 20:02

empire29


People also ask

How does Jenkins pass parameters to Maven project?

Bookmark this question. Show activity on this post. Source Code Management → Git → Repositories → Repository URL: I wrote Git repo link, it was OK.

How do I add parameters in Jenkins?

Any Jenkins job or pipeline can be parameterized. All we need to do is check the box on the General settings tab, “This project is parameterized”: Then we click the Add Parameter button.

How use Jenkins Maven profile?

To enable it you'll have to check 'this build is parametrized' option and then define option. To be able to define list option you'll then have to install extended choice parameter plugin that extends the first one by adding list option (and other parameter types). You'll then be able to define a PROFILE list option.


1 Answers

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

like image 153
Andrzej Jozwik Avatar answered Oct 03 '22 19:10

Andrzej Jozwik