有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java 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将像正常情况一样在每个依赖项中运行测试
  • 另一个参数将仅对指定的工件运行测试,而对未包含的依赖项跳过测试

这到底是可能的还是有其他的方法来实现这一点


共 (1) 个答案

  1. # 1 楼答案

    可以使用profiles生成两个不同的版本

    <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