如果激活多个配置文件,如何解决非冲突配置?

时间:2016-11-23 09:07:35

标签: maven

跟进this问题: 在冲突的情况下,我无法找到有关如何构建多个(活动)maven配置文件的联合信息。

考虑以下示例: 我想控制我的测试用例如何使用maven-surefire-plugin运行。首先,我需要配置两个不同的环境(jenkins-CI-server)和本地环境。其次,我想启用运行不同的测试集。 我会有四个档案:

  1. env_jenkins在jenkins上运行
  2. env_local在本地
  3. 上运行
  4. testset_A用于运行某些测试
  5. testset_B用于运行其他一些测试
  6. 请注意,对于所有这些配置文件,我需要为maven-surefire-plugin定义插件配置。 (具体的xml配置见下文)

    然后我想组合配置文件来运行 - 例如 - jenkins上的testset A.

    我的问题:

    • maven是否支持这种行为?
    • the related question中提及的覆盖在哪个阶段发生?是否覆盖了整个插件配置( - >我的示例不起作用)。或者只是真正有冲突的部分( - >我的例子会起作用)?
    • 覆盖行为是否取决于插件,还是所有maven插件都一致?

    示例pom:

    <profiles>
        <!-- handle system configurations (e.g. one for jenkins environment, one for local) -->
        <profile>
            <id>env_jenkins</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <configuration>
                            <systemPropertyVariables>
                                <arquillian.launch>jbossas-managed</arquillian.launch>
                                <jbossHttpPortOverride>8080</jbossHttpPortOverride>
                            </systemPropertyVariables>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <id>env_local</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <configuration>
                            <systemPropertyVariables>
                                    <arquillian.launch>jbossas-managed-jenkins</arquillian.launch>
                                    <jbossHttpPortOverride>${jboss.http.port}</jbossHttpPortOverride>
                            </systemPropertyVariables>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <!-- define different test sets -->
        <profile>
            <id>testset_A</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <configuration>
                            <includes>
                                <include>TestA1.java</include>
                                <include>TestA2.java</include>
                                <include>TestA3.java</include>
                            </includes>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <id>testset_B</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <configuration>
                            <includes>
                                <include>TestB1.java</include>
                            </includes>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
    

1 个答案:

答案 0 :(得分:1)

它使用在父POM和子POM之间工作的相同inheritance/merging process。使用Maven帮助插件可以确认这一点(使用Maven 3.3.9):

  1. POM中的个人资料声明顺序很重要。插件配置继承了在POM中出现的活动配置文件中声明的相同插件配置。

  2. 属性combine.selfcombine.children可用于控制合并。

  3. 使用默认设置的示例(不使用combine.selfcombine.children,即隐式使用combine.children="merge"):

    mvn help:effective-pom -Penv_local,testset_A
    

    有效POM显示合并配置:

    ...
    <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.12.4</version>
        <executions>
          <execution>
            <id>default-test</id>
            <phase>test</phase>
            <goals>
              <goal>test</goal>
            </goals>
            <configuration>
              <includes>
                <include>TestA1.java</include>
                <include>TestA2.java</include>
                <include>TestA3.java</include>
              </includes>
              <systemPropertyVariables>
                <arquillian.launch>jbossas-managed-jenkins</arquillian.launch>
                <jbossHttpPortOverride>${jboss.http.port}</jbossHttpPortOverride>
              </systemPropertyVariables>
            </configuration>
          </execution>
        </executions>
        <configuration>
          <includes>
            <include>TestA1.java</include>
            <include>TestA2.java</include>
            <include>TestA3.java</include>
          </includes>
          <systemPropertyVariables>
            <arquillian.launch>jbossas-managed-jenkins</arquillian.launch>
            <jbossHttpPortOverride>${jboss.http.port}</jbossHttpPortOverride>
          </systemPropertyVariables>
        </configuration>
      </plugin>
      ...
    

    combine.self="override"的示例:

    <profile>
        <id>testset_A</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <configuration combine.self="override">
                        <includes>
                            <include>TestA1.java</include>
                            <include>TestA2.java</include>
                            <include>TestA3.java</include>
                        </includes>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
    
    mvn help:effective-pom -Penv_local,testset_A
    

    有效POM仅显示testset_A的配置:

    <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.12.4</version>
        <executions>
          <execution>
            <id>default-test</id>
            <phase>test</phase>
            <goals>
              <goal>test</goal>
            </goals>
            <configuration combine.self="override">
              <includes>
                <include>TestA1.java</include>
                <include>TestA2.java</include>
                <include>TestA3.java</include>
              </includes>
            </configuration>
          </execution>
        </executions>
        <configuration combine.self="override">
          <includes>
            <include>TestA1.java</include>
            <include>TestA2.java</include>
            <include>TestA3.java</include>
          </includes>
        </configuration>
      </plugin>
    

    combine.children="append"的示例:

    <profile>
        <id>testset_A</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <configuration combine.children="append">
                        <includes>
                            <include>TestA1.java</include>
                            <include>TestA2.java</include>
                            <include>TestA3.java</include>
                        </includes>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
    
    mvn help:effective-pom -Penv_local,testset_A
    

    有效POM显示合并配置。在这种情况下,它恰好与第一个示例相同。但是,如果testset_A引入了env_local中已存在的配置XML元素,则会附加它们:

    <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.12.4</version>
        <executions>
          <execution>
            <id>default-test</id>
            <phase>test</phase>
            <goals>
              <goal>test</goal>
            </goals>
            <configuration combine.children="append">
              <systemPropertyVariables>
                <arquillian.launch>jbossas-managed-jenkins</arquillian.launch>
                <jbossHttpPortOverride>${jboss.http.port}</jbossHttpPortOverride>
              </systemPropertyVariables>
              <includes>
                <include>TestA1.java</include>
                <include>TestA2.java</include>
                <include>TestA3.java</include>
              </includes>
            </configuration>
          </execution>
        </executions>
        <configuration combine.children="append">
          <systemPropertyVariables>
            <arquillian.launch>jbossas-managed-jenkins</arquillian.launch>
            <jbossHttpPortOverride>${jboss.http.port}</jbossHttpPortOverride>
          </systemPropertyVariables>
          <includes>
            <include>TestA1.java</include>
            <include>TestA2.java</include>
            <include>TestA3.java</include>
          </includes>
        </configuration>
      </plugin>