给定一个包含模块dad
和son
的Maven项目,其中son
依赖于dad
,mvn -am -pl son test
也运行dad
的测试。有没有办法避免这种情况,只运行son
模块的测试?
注意,有几种方法可以实现这一点,尽管每种方法都有自己的注意事项,我不喜欢:
- 使用
-Dtest="**/son/*Test.java" -DfailIfNoTests=false
会覆盖模块中的maven-surefire-plugin
配置 - 与
-Dtest
类似,JUnit 5标签也可以被过滤,但这种方法也存在上述相同的缺点 - 可以先做
install -DskipTests=true
,然后做mvn -pl son test
,尽管这会用部分工作污染本地Maven存储库
在maven 3.3.9上,我运行了:
mvn test -pl :web-module
以运行web模块。在POM中,modules
标签内的<module>web-module</module>
应该是这样的。
测试该模块内的特定类:
mvn test -pl :web-module -Dtest="ClassUnderTest"
我在这个问题上坚持了一段时间,但最终,我得到了解决方案。
这是我的情况:
- 我有很多子模块:A、B和C。它们中的一些是独立的,还有一些是依赖的
- 我只想运行Module-C测试,但C依赖于A和B
mvn clean test -pl C
对我不起作用,因为2mvn clean test -pl C -am
对我有用,但它运行所有的模块测试
这是我的想法:
mvn clean test -pl C -am
之所以运行所有测试,是因为C对a和B有依赖性,所以模块C必须先编译a和B,然后编译C。
以下是决议:
- 使父Pom跳过所有模块测试
- 在模块C上制作一个配置文件,并启用测试
- 只运行
mvn clean test -DmoduleCTest=true
模块C pom是这样的,另一个模块也是这样,但有不同的配置文件id。
<profiles>
<profile>
<id>moduleCTest</id>
<activation>
<property>
<name>moduleCTest</name>
<value>true</value>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>false</skipTests>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>