为单个 Jenkins 作业构建多个 Maven 配置文件 [英] Building multiple Maven profiles for a single Jenkins job

查看:44
本文介绍了为单个 Jenkins 作业构建多个 Maven 配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在单个 Jenkins 作业中构建多个 Maven 配置文件.每个配置文件更改一些代码,然后通过在命令行中执行 mvn -Pdev install 然后 mvn -Pprod install 创建一个 jar(根据 Maven 使用 mvn -Pdev,prod install 应该可以工作,但对我不起作用).这是我项目的 pom.xml 中的两个配置文件:

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>

我将如何设置 Jenkins 以在每次构建作业时自动为单个 Jenkins 作业构建这两个配置文件?并将这两个罐子都放在 Artifactory 中?我对 Jenkins 的了解很少,网上也没有太多相关信息.

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.

推荐答案

您可以创建一个 Jenkins 矩阵作业.矩阵作业允许在更改设置的情况下运行相同的作业(在您的情况下:一个字符串).

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

每个更改设置称为一个轴.在您的情况下,您将创建一个包含两个值的字符串轴:dev 和 prod.

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.

但是:您对配置文件的使用是危险的.由于用于运行构建的配置文件未编入您的工件中,因此您违反了 Maven 的一个源版本应始终导致完全相同的目标工件"合同(请参阅:http://www.blackbuild.com/how-to-really-use-maven-profiles-without-endangering-your-karma/ 以获得更详细的解释)

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)

考虑使用分类器(-dev 和 -prod)创建两个不同的工件,或者甚至更好:创建两个单独的构建模块,每个模块仅创建一个目标工件.

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.

这篇关于为单个 Jenkins 作业构建多个 Maven 配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆