基本配置
<plugins>
..............
..............
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
<includes>
<include>**/TestServiceTest.java</include>
</includes>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.1</version>
<configuration>
<includes>
<include>com/jd/feima/TestService*</include>
</includes>
<destFile>${project.build.directory}/coverage-reports/jacoco-unit.exec</destFile>
<dataFile>${project.build.directory}/coverage-reports/jacoco-unit.exec</dataFile>
</configuration>
<executions>
<execution>
<id>jacoco-initialize</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>jacoco-site</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
............
............
</plugins>
其中:
surefire指定跑 ‘**/TestServiceTest.java’
surefire插件的路径与文件名匹配参考:
surefire插件匹配模式参考
jacoco计算 相对 ‘target/classes’下面的 ‘com/jd/feima/TestService*’ 类的测试覆盖率。
而且jacoco绑定到mvn的test goal。
包含子项目的配置
最近的一个项目是一个父工程下面包含多个子工程的项目结构。
-|-com-product-project
-|- common
-|- service
-|- manager
-|- web
我们知道子工程会自动继承父工程POM里面的dependency与plugin设置。
该工程中作为共通插件,surefire与jacoco与compiler一样是配置在父工程中的。
那么子工程如何既继承父工程的plugin又个性化路径,测试类之类的配置?
下面是我配置的例子:
<plugins>
.........
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
<!-- 只运行以NewTest结尾的测试 -->
<includes>
<include>**/*NewTest.java</include>
</includes>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.1</version>
<configuration>
<destFile>${project.build.directory}/coverage-reports/jacoco-unit.exec</destFile>
<dataFile>${project.build.directory}/coverage-reports/jacoco-unit.exec</dataFile>
</configuration>
<executions>
<execution>
<id>jacoco-initialize</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>jacoco-site</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
.......
</plugins>
surefire插件:
**/*NewTest.java : 将要执行的测试类限定为NewTest结尾的类。
jacoco
${project.build.directory} : 每个项目的target/ 目录下放jacoco的覆盖率配置文件以及测试报告
${project.build.directory} 是maven自带的环境变量,我们无需配置