【问题标题】:How to run two profiles in one maven command?如何在一个 Maven 命令中运行两个配置文件?
【发布时间】:2014-11-13 09:48:14
【问题描述】:

我有一个 pom.xml 文件,其中包含 surefire:test 插件,并且有两个配置文件来运行不同的测试模块/测试。我试图通过命令“mvn surefire:test -PfirstProfile,secondProfile”运行这些。 但是这里只有用 pom.xml 编写的第二个配置文件被执行。这个命令是 apache maven 网站推荐的。这是我的 pom.xml 文件:

 <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>${maven.plugin.surefire.version}</version>
    <configuration>
      <forkMode>always</forkMode>
      <!-- <testFailureIgnore>true</testFailureIgnore> -->
      <failIfNoTests>false</failIfNoTests>
      <skipTests>${skipTests}</skipTests>
      <runOrder>${testRunOrder}</runOrder>
    </configuration>
    </execution>
    </executions>
  </plugin>
  <profile>
  <id>firstProfile</id>
  <properties>
        <skipTests>false</skipTests>
  </properties>
  <build>
      <plugins>
          <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-surefire-plugin</artifactId>
              <version>${maven.plugin.surefire.version}</version>
              <configuration>
                  <includes>
                    <include>**/AbcTest.java</include>
                  </includes>
              </configuration>
          </plugin>
      </plugins>

  </build>
</profile>
<profile>
  <id>SecondProfile</id>
  <properties>
        <skipTests>false</skipTests>
  </properties>
  <build>
      <plugins>
          <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-surefire-plugin</artifactId>
              <version>${maven.plugin.surefire.version}</version>

              <configuration>
                  <includes>
                      <include>**/xyzTest.java</include>                      
                  </includes>
              </configuration>
          </plugin>
      </plugins>

  </build>
</profile>

那么我如何同时运行这两个配置文件?我也尝试了执行 ID。但它不起作用。

【问题讨论】:

    标签: maven maven-plugin maven-profiles


    【解决方案1】:

    尝试在两个配置文件中更改 maven-surefire-plugin 的配置,如下所示:

       <build>
          <plugins>
              <plugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-surefire-plugin</artifactId>
                  <version>${maven.plugin.surefire.version}</version>
                  <configuration>
                      <includes combine.children="append">
                        <include>**/AbcTest.java</include>
                      </includes>
                  </configuration>
              </plugin>
          </plugins>
    
      </build>
    

    除此之外,按配置文件选择单元测试并不是一个好主意。如果这些测试是集成测试,您应该改用 maven-failsafe-plugin。

    【讨论】:

    • @khkhmarbaise 基本上,我的第一个配置文件适用于所有单元测试,其中我包括 **/*Test.java 并排除 **/*IntegrationTest.java。第二个配置文件适用于我排除 **/*Test.java 并包含 **/*IntegrationTest.java 的所有集成测试。当开发人员分别指定 -u 或 -i 选项时,我在 test_runner.sh 脚本中使用这两个配置文件来运行单元测试和集成测试。但是当开发人员同时指定 -u 和 -i 选项时,我也想同时运行单元测试和集成测试。这就是为什么我试图在一个 Maven 命令中运行这两个配置文件。
    • 继续我上面的评论。这导致只运行一个配置文件,它是 pom.xml 文件中的最后一个。如果您提到的 maven-failsafe-plugin 解决了这个问题,请告诉我。或者,如果您有其他解决方案,请指定。你提到的 combine.children="append" 解决方案对我不起作用。感谢您的快速回复
    • 如果您正在使用由 maven-surefire-plugin 自动处理的命名约定 /*Test.java 或 /*TestCase.java 或 /Test*.java 进行单元测试。如果您喜欢进行集成测试,您应该遵循命名约定并使用 maven-failsafe-plugin 开箱即用的 IT*.java*IT.java*ITCase.java。如果您遵循约定,则不需要配置(Convention over Configuration 范例)。
    猜你喜欢
    • 2016-07-18
    • 2013-05-23
    • 2016-12-12
    • 1970-01-01
    • 2012-04-10
    • 2018-06-29
    • 1970-01-01
    • 2015-09-04
    相关资源
    最近更新 更多