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

时间:2015-05-04 18:57:08

标签: java maven maven-surefire-plugin

我有一个项目,其中包含多个其他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将在每个依赖项中运行测试,如正常
  • 另一个参数只运行 测试指定的任何工件,并跳过测试以确定是否包含任何依赖

这是否可行或有其他方法可以解决这个问题吗?

1 个答案:

答案 0 :(得分:2)

You can use profiles to have two distinct builds.

<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>

The use mvn clean test -P project-a or mvn clean test -P project-b You could also set different properties in each profiles and have a centralized surefire config.


Or you could use a property:

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

The use mvn clean test -DsomeProperty=project-a