Maven Surefire:在指定的依赖项中运行测试



我有一个项目,其中包括其他多个JAR伪像作为依赖项。我正在使用SureFire插件的dependenciesToScan属性在上述文物中运行测试,如下所示:

<build>
  <plugins>
    <plugin>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>2.18.1</version>
      <configuration>
        <dependenciesToScan>
          <dependency>com.example.tests:project-a</dependency>
          <dependency>com.example.tests:project-b</dependency>
        </dependenciesToScan>
      </configuration>
    </plugin>
  </plugins>
</build>
<dependencies>
  <dependency>
    <groupId>com.example.tests</groupId>
    <artifactId>project-a</artifactId>
  </dependency>
  <dependency>
    <groupId>com.example.tests</groupId>
    <artifactId>project-b</artifactId>
  </dependency>
</dependencies>

理想情况下,我希望能够做以下操作:

  • mvn test将在每个依赖项中运行测试,例如普通
  • 另一个参数将仅运行 指定了任何工件的测试,并跳过不包括任何依赖性的测试

这是完全可能的,还是有另一种方法可以接近?

您可以使用配置文件具有两个不同的构建。

<profiles>
  <profile>
    <id>project-a</id>
    <build>
      <plugins>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.18.1</version>
          <configuration>
            <dependenciesToScan>
              <dependency>com.example.tests:project-a</dependency>
            </dependenciesToScan>
          </configuration>
        </plugin>
     </plugins>
   </build>
  </profile>
  <profile>
    <id>project-b</id>
    <build>
      <plugins>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.18.1</version>
          <configuration>
            <dependenciesToScan>
              <dependency>com.example.tests:project-b</dependency>
            </dependenciesToScan>
          </configuration>
        </plugin>
     </plugins>
   </build>
  </profile>
</profiles>

使用mvn clean test -P project-amvn clean test -P project-b您还可以在每个配置文件中设置不同的属性,并具有集中的SureFire配置。


,或者您可以使用属性:

<build>
  <plugins>
    <plugin>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>2.18.1</version>
      <configuration>
        <dependenciesToScan>
          <dependency>${someProperty}</dependency>
        </dependenciesToScan>
      </configuration>
    </plugin>
  </plugins>
</build>

使用mvn clean test -DsomeProperty=project-a

最新更新