2

我试图从我的 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.

4

3 回答 3

10

跳过测试

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>plugin 代替。

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

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

此外,如果您正在使用JUnit,您可以使用@Ignore并添加一条消息。

从编译中排除测试

这个答案,你可以使用。诀窍是拦截<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>
于 2014-08-12T23:48:27.100 回答
2

在 Maven 中默认跳过 test 的编译和执行的最简单方法是在你的 中添加以下属性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> 
于 2014-08-12T22:16:55.300 回答
2

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

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/

于 2019-11-29T12:14:19.260 回答