Building multiple Maven profiles for a single Jenkins job

user2639215 picture user2639215 · Aug 9, 2013 · Viewed 37.2k times · Source

I am trying to build multiple Maven profiles in a single Jenkins job. Each profile changes some code and then creates a jar by executing mvn -Pdev install then mvn -Pprod install in the command line (According to Maven using mvn -Pdev,prod install is supposed to work but it isn't working for me). Here are the two profiles in my project's pom.xml:

<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 would I setup Jenkins to automatically build both of these profiles for a single Jenkins job whenever the job is hit for a build? And put both of these jars in the Artifactory? I have very little Jenkins knowledge and there isn't much information on this on the web.

Answer

blackbuild picture blackbuild · Mar 2, 2014

You could create a Jenkins matrix job. A matrix job allows the same job to run with changing settings (in your case: a string).

Each changing setting is called an axis. In your case you would create a string axis containing the two values: dev and prod.

That way your job would run twice, with both values.

However: your usage of profiles is dangerous. Since the profile used to run the build is not codified into your artifact, your break the "one source revision should always lead to exactly the same target artifact" contract of Maven (see: http://www.blackbuild.com/how-to-really-use-maven-profiles-without-endangering-your-karma/ for a more detailed explanation)

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