gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 17:55:11 25 4
gpt4 key购买 nike

关注 this问题:
在它们不冲突的情况下,我找不到有关如何构建多个(事件)maven 配置文件的联合的信息。

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

  • env_jenkins用于在 jenkins 上运行
  • env_local用于在本地运行
  • testset_A用于运行一些测试
  • testset_B用于运行其他一些测试

  • 请注意,对于所有这些配置文件,我需要为 maven-surefire-plugin 定义插件配置。
    (具体的xml配置见下文)

    然后我想结合配置文件运行 - 例如 - jenkins 上的测试集 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>

    最佳答案

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

  • POM 中配置文件声明的顺序很重要。插件配置继承了在 POM 中出现在它之前的事件配置文件中声明的相同插件配置。
  • 属性 combine.selfcombine.children可用于控制合并。


  • 默认设置示例(不使用 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>

    关于maven - 如果激活多个配置文件,如何解决非冲突配置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40760066/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com