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

Java Maven Surefire:在指定的依赖项中运行测试,java,maven,maven-surefire-plugin,Java,Maven,Maven Surefire Plugin,我有一个项目,其中包含多个其他jar工件作为依赖项。我使用surefire插件的dependenciesToScan属性在上述工件中运行测试,如下所示: <build> <plugins> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.18.1</version> <con

我有一个项目,其中包含多个其他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>

maven surefire插件
2.18.1
com.example.tests:project-a
com.example.tests:project-b
com.example.tests
项目a
com.example.tests
b项目
理想情况下,我希望能够做到以下几点:

  • 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清洁测试-DsomeProperty=project-a

感谢您的快速回复!当存在多个依赖项时会怎么样?假设有许多测试jar…是否有任何方法可以避免必须为每个添加的jar创建配置文件?如果“project-x”太多,则始终可以使用属性指定“”为什么不直接在适当的模块中运行单元测试呢?这是在一个“聚合”项目中,用于同时运行所有不同的工件,例如在Jenkins作业中。你建议把它们分开吗?
<build>
  <plugins>
    <plugin>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>2.18.1</version>
      <configuration>
        <dependenciesToScan>
          <dependency>${someProperty}</dependency>
        </dependenciesToScan>
      </configuration>
    </plugin>
  </plugins>
</build>