Creating multiple Maven profiles for a single Jenkins job

I am trying to create multiple Maven profiles in a single Jenkins task. Each profile changes some code and then creates a jar by executing mvn -Pdev install and then mvn -Pprod install on the command line (according to Maven using mvn -Pdev,prod install should work, but it does not work for me) . Here are two profiles in my pom.xml project:

 <profiles> <!-- prod profile --> <profile> <id>prod</id> <build> <plugins> <plugin> <groupId>com.google.code.maven-replacer-plugin</groupId> <artifactId>replacer</artifactId> <version>1.5.2</version> <executions> <execution> <phase>process-resources</phase> <goals> <goal>replace</goal> </goals> </execution> </executions> <configuration> <file>src/main/java/com/IQzone/android/configuration/AbstractHoldingRefreshable.java</file> <replacements> <replacement> <token>TrUe</token> <value>TOAST_SWITCH</value> </replacement> </replacements> </configuration> </plugin> <plugin> <artifactId>maven-jar-plugin</artifactId> <executions> <execution> <phase>package</phase> <goals> <goal>jar</goal> </goals> <configuration> <classifier>prod</classifier> </configuration> </execution> </executions> </plugin> </plugins> </build> </profile> <!-- dev profile --> <profile> <id>dev</id> <build> <plugins> <plugin> <groupId>com.google.code.maven-replacer-plugin</groupId> <artifactId>replacer</artifactId> <version>1.5.2</version> <executions> <execution> <phase>process-resources</phase> <goals> <goal>replace</goal> </goals> </execution> </executions> <configuration> <file>src/main/java/com/IQzone/android/configuration/AbstractHoldingRefreshable.java</file> <replacements> <replacement> <token>TOAST_SWITCH</token> <value>TrUe</value> </replacement> </replacements> </configuration> </plugin> <!-- build project with JAVA 1.6 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <configuration> <skip>true</skip> </configuration> </plugin> <plugin> <artifactId>maven-jar-plugin</artifactId> <executions> <execution> <phase>package</phase> <goals> <goal>jar</goal> </goals> <configuration> <classifier>dev</classifier> </configuration> </execution> </executions> </plugin> </plugins> </build> </profile> </profiles> 

How do I configure Jenkins to automatically create both of these profiles for the same Jenkins job whenever a job strikes to build? And put both of these jars in Artifactory? I have very little Jenkins knowledge, and there is little information on the Internet about this.

+7
maven build jenkins artifactory
source share
4 answers

You can create a Jenkins matrix job. The matrix task allows you to perform the same task with changing settings (in your case: a line).

Each changing setting is called an axis. In your case, you will create a line axis containing two values: dev and prod.

This way your work will be done twice, with both values.

However: using profiles is dangerous. Since the profile used to start the build is not encoded into your artifact, your break β€œone source should always result in the exact same target artifact” of the Maven contract (see http://www.blackbuild.com/how-to-really -use-maven-profiles-without-endangering-your-karma / for a more detailed explanation)

Consider creating two different artifacts using the classifier (-dev and -prod) or even better: create two separate modules of your assembly, each of which creates only one of your target artifacts.

+13
source share

In Maven, if you use mvn -Pdev,prod , you simultaneously activate both profiles in the same command.

It seems that you want to execute 2 different executions of the command, that is, something that you would achieve on the command line by doing 2 lines:

 mvn -Pdev install; mvn -Pprod install 

In jenkins you can achieve this with

  • one free style project job (with 2 shell collectors performing mvn -P$PROFILE install tasks)
  • 2 tasks of the maven type (which you can link one by one using "build after creating other projects").
+9
source share

In addition to the Matrix task and multiple maven calls in the free style task, there is another way: Run top-level Maven targets as a pre-build step and run another command through the maven jenkins plugin.

Make sure that the pre-build step uses the same maven repo as the other command by supplying -Dmaven.repo.local=/${whatever-it-is}/${EXECUTOR_NUMBER} .

For more information on working with the matrix, see other answers.

+1
source share

You can do this by setting different execution identifiers for each execution, and then run the command

mvn -Pdev,prod clean package install

0
source share

All Articles