【问题标题】:How do I set in the pom to not compile tests?如何在 pom 中设置不编译测试?
【发布时间】:2013-03-27 03:29:56
【问题描述】:

如何在 pom 中设置为不在 Maven 中编译测试?我试过了:

<properties>
  <skipTests>true</skipTests>
</properties>

但在这种情况下,Maven 编译测试但不运行它们。我需要 Maven 不要编译我的测试。

【问题讨论】:

  • 也许您可以拥有一个指向测试源目录的配置文件。否则,它指向一个空的。
  • 我不需要编译它们,因为这些测试调用 web 服务,解析它的类等等。由于项目使用 Hudson 进行持续集成,因此只有在我们明确设置它们执行此操作时,我们才需要编译和运行这些测试。
  • @Rafael。我认为这样做会破坏构建的可移植性。

标签: java maven testing pom.xml


【解决方案1】:

如果您使用surefire-plugin 执行测试,您可以将其配置为根据命名模式跳过它们:

<project>
  [...]
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.14</version>
        <configuration>
          <includes>
            <include>%regex[.*[Cat|Dog].*Test.*]</include>
          </includes>
        </configuration>
      </plugin>
    </plugins>
  </build>
  [...]
</project>

然而,这要求测试文件名符合所需的模式。在工作中,我们正在使用这种方法,并让我们的测试以..UnitTest..IntegrationTest 结尾,这样我们就可以通过修改相应构建配置文件中的正则表达式来轻松关闭它们。

看看surefire插件上的Apache's documentation。您可能会发现更有用或更适合您的情况的东西。

【讨论】:

    【解决方案2】:

    您必须将 maven.test.skip 定义为 true。

    <properties>
        <maven.test.skip>true</maven.test.skip>
    </properties>
    

    http://maven.apache.org/surefire/maven-surefire-plugin/examples/skipping-test.html

    【讨论】:

    • 这会跳过运行它们。 OP 想跳过编译
    • @user2863942 引用我的链接:“如果你绝对必须,你也可以使用 maven.test.skip 属性来跳过编译测试。maven.test.skip 受到 Surefire 的尊重、故障安全和编译器插件。”
    • -Dmaven.test.skip=true 只是为了方便复制/粘贴
    • 你甚至不需要“=true”。通过设置它是真的。
    【解决方案3】:

    配置 maven-compiler-plugin 以跳过编译。 再一次,我不推荐它。

    <project>
      <properties>
        <maven.test.skip>true</maven.test.skip>
      </properties>
      [...]
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.0</version>
            <configuration>
            <executions>
              <execution>
                <id>default-testCompile</id>
                <phase>test-compile</phase>
                <goals>
                  <goal>testCompile</goal>
                </goals>
                <configuration>
                  <skip>${maven.test.skip}</skip>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
      [...]
    </project>
    

    【讨论】:

    • 如果我以后需要激活测试编译和运行, mvn clean install -Dmaven.test.skip=false 可以吗?或者我需要另一个参数?
    • @Rafael。我想,你可以让maven.test.skip 拉双重职责
    【解决方案4】:

    在我的例子中,一个解决方案是将测试放在配置文件中(例如 runTests),所以当我想运行这些测试时,我添加了参数-PrunTests。感谢您的回复。

    【讨论】:

      猜你喜欢
      • 2014-11-09
      • 1970-01-01
      • 1970-01-01
      • 2023-01-03
      • 2012-03-29
      • 2011-06-13
      • 2017-03-18
      • 1970-01-01
      • 2012-08-27
      相关资源
      最近更新 更多