如何在Maven构建中永久排除一个测试类

时间:2014-08-12 20:05:51

标签: maven maven-surefire-plugin maven-compiler-plugin

我试图从我的maven构建中排除单个测试(我不想要编译或执行测试)。以下不起作用:

<project ...>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <excludes>
            <exclude>**/MyTest.java</exclude>
          </excludes>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

实现目标的正确方法是什么?我知道我可以使用命令行选项-Dmaven.test.skip=true,但我希望这可以成为pom.xml的一部分。

3 个答案:

答案 0 :(得分:6)

跳过测试

docs,如果您想跳过测试,可以使用:

<project>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.17</version>
        <configuration>
          <excludes>
            <exclude>**/MyTest.java</exclude>
          </excludes>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

在您的示例中,请参阅差异,使用<artifactId>maven-compiler-plugin</artifactId>,文档说您应该使用<artifactId>maven-surefire-plugin</artifactId>插件。

并且,如果要禁用所有测试,可以使用:

    <configuration>
      <skipTests>true</skipTests>
    </configuration>

此外,如果您使用的是JUnit,则可以使用@Ignore并添加消息。

从编译中排除测试

this回答,您可以使用。诀窍是截取<id>default-testCompile</id> <phase>test-compile</phase>(默认测试编译阶段)并排除类:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <executions>
    <execution>
      <id>default-testCompile</id>
      <phase>test-compile</phase>
      <configuration>
        <testExcludes>
          <exclude>**/MyTest.java</exclude>
        </testExcludes>
      </configuration> 
      <goals>
        <goal>testCompile</goal>
      </goals>
    </execution>                  
  </executions>
</plugin>

答案 1 :(得分:2)

默认情况下,在Maven中跳过编译和执行测试的最简单方法是在pom.xml中添加以下属性:

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

您仍然可以通过从命令行覆盖属性来更改行为:

-Dmaven.test.skip=false

或者通过激活个人资料:

<profiles>
    <profile>
        <id>testing-enabled</id>
        <properties>
           <maven.test.skip>false</maven.test.skip>
        </properties>
    </profile>
</profiles> 

答案 2 :(得分:0)

使用解释标记(!)排除一个测试类

mvn test -Dtest=!LegacyTest

排除一种测试方法

mvn verify -Dtest=!LegacyTest#testFoo

排除两种测试方法

mvn verify -Dtest=!LegacyTest#testFoo+testBar

排除带有通配符(*)的软件包

mvn test -Dtest=!com.mycompany.app.Legacy*

这来自:https://blog.jdriven.com/2017/10/run-one-or-exclude-one-test-with-maven/