java - Maven 肯定火 : Running tests in specified dependencies

标签 java maven maven-surefire-plugin

我有一个项目,其中包含多个其他 jar Artifact 作为依赖项。我使用 Surefire 插件的 dependencyToScan 属性在上述 Artifact 中运行测试,如下所示:

<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 将像平常一样在每个依赖项中运行测试
  • 另一个参数将运行指定的 Artifact 的测试,并跳过未包含的任何依赖项的测试

这是否可能,或者是否有其他方法可以解决这个问题?

最佳答案

您可以使用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 您还可以在每个配置文件中设置不同的属性,并拥有集中的可靠配置。

<小时/>

或者您可以使用属性:

<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

关于java - Maven 肯定火 : Running tests in specified dependencies,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30037847/

相关文章:

Java 11 与 Powermock - 它们兼容吗?

Java 8 : Found unsigned entry in resource

java - 从包含 txt 文件的 Maven 项目创建 JAR 文件

java - 从 Maven 插件执行 "Export Studio Documentation"

java - 通过命令行覆盖 Surefire 配置

java - 如何修复 Maven 和 Jacoco 的特定版本问题?

java - Java 中的词法、句法或语义错误差异

java - 如何在编译时访问嵌套依赖项?

java - Spring mvc-来自数据库的下拉框选项

java - 帮助设置 Java 构建环境