11

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.

haihui
  • 1,075
  • 1
  • 18
  • 25
user2639215
  • 111
  • 1
  • 1
  • 5

4 Answers4

16

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.

blackbuild
  • 5,026
  • 1
  • 23
  • 35
  • I have a problem which is little similar to this. I have placed in this link, can u please have a look into it and let me know if you have any solution. https://stackoverflow.com/questions/48002535/one-profile-properties-are-overriding-with-another-profile-properties-in-maven – Nallamachu Jan 04 '18 at 12:34
11

In Maven, if you use mvn -Pdev,prod, then you are activating both profiles simultaneously in one command.

It seems you want 2 distinct run of the command, i.e. something you would achieve on the command line by doing 2 builds:

mvn -Pdev install; mvn -Pprod install

In jenkins you can achieve this with either

  • one free style project job (with 2 shell builders running the mvn -P$PROFILE install tasks)
  • 2 maven type jobs (that you can chain one after the other using "build after other projects are built").
Greg Dubicki
  • 5,983
  • 3
  • 55
  • 68
coffeebreaks
  • 3,787
  • 1
  • 26
  • 25
2

In addition to Matrix job and multiple maven invocations in a free-style job, there's another way: Run top-level Maven targets as a pre-build step and run the other command via 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}.

Please refer to other answers for details on matrix job, etc.

phanin
  • 5,327
  • 5
  • 32
  • 50
0

You can do it by setting different execution ids to each execution and then trigger the command

mvn -Pdev,prod clean package install