且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

maven:如何通过命令行选项跳过某些项目中的测试?

更新时间:2022-04-30 00:37:50

要打开和关闭整个项目的单元测试,请使用

To toggle unit tests on and off for an entire project use Maven Surefire Plugin's capability of skipping tests. There is a drawback with using skipTests from the command line. In a multi-module build scenario, this would disable all tests across all modules.

如果您需要对模块的测试子集进行更精细的控制,请使用

If you need more fine grain control of running a subset of tests for a module, look into using the Maven Surefire Plugin's test inclusion and exclusion capabilities.

要允许命令行替代,请在配置Surefire插件时使用POM属性.以下面的POM段为例:

To allow for command-line overrides, make use of POM properties when configuring the Surefire Plugin. Take for example the following POM segment:

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.9</version>
        <configuration>
          <excludes>
            <exclude>${someModule.test.excludes}</exclude>
          </excludes>
          <includes>
            <include>${someModule.test.includes}</include>
          </includes>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <properties>
    <someModule.skip.tests>false</someModule.skip.tests>
    <skipTests>${someModule.skip.tests}</skipTests>
    <someModule.test.includes>**/*Test.java</someModule.test.includes>
    <someModule.test.excludes>**/*Test.java.bogus</someModule.test.excludes>
  </properties>

使用上述POM,您可以通过多种方式执行测试.

With a POM like the above you can execute tests in a variety of ways.

  1. 运行所有测试(以上配置包括所有**/* Test.java测试源文件)

mvn test

  1. 跳过所有模块中的所有测试

mvn -DskipTests=true test

  1. 跳过所有针对特定模块的测试

mvn -DsomeModule.skip.tests=true test

  1. 仅对特定模块运行某些测试(此示例包括所有**/* IncludeTest.java测试源文件)

mvn -DsomeModule.test.includes="**/*IncludeTest.java" test

  1. 排除特定模块的某些测试(此示例排除了所有**/* ExcludeTest.java源文件)

mvn -DsomeModule.test.excludes="**/*ExcludeTest.java" test