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