配置
配置jacoco 插件时,主pom中配置如下
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.2</version>
<executions>
<execution>
<id>jacoco-initialize</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<!--这个report:对代码进行检测,然后生成index.html在target/jacoco-ut/index.html中可以查看检测的详细结果-->
<execution>
<id>jacoco-site</id>
<phase>test</phase>
<!--<phase>package</phase>写上test的时候会自动出现jacoco-ut文件夹,而不需执行下面的jacoco:report步骤,推荐-->
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>target/jacoco.exec</dataFile>
<outputDirectory>target/jacoco-ut</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
在每个子pom中配置
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
</plugin>
运行mvn test 或在 maven Projects -> Lifecycle中双击test后,没有生成覆盖率报告。
问题
查看运行结果,最开始发现是因为没有生成执行文件,所以查了很多博文,
jacoco没有生成执行文件,发现都没办法解决
[INFO] --- jacoco-maven-plugin:0.8.2:report (jacoco-site) @ manager ---
[INFO] Skipping JaCoCo execution due to missing execution data file.
解决
最后发现还是使用 Surefire 配置破坏 JaCoCo 代理,但是不是以上原因,查看了控制台输出,发现以下提示
[INFO] --- maven-surefire-plugin:2.21.0:test (default-test) @ manager ---
[WARNING] The parameter forkMode is deprecated since version 2.14. Use forkCount and reuseForks instead.
最后在主pom和每个子pom都添加了以下配置
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.21.0</version>
<configuration>
<useSystemClassLoader>false</useSystemClassLoader>
<forkMode>once</forkMode>
<reuseForks>true</reuseForks>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
重新执行,终于有覆盖率报告了!!!!!
(解决后才发现原来已有博主有提到这种原因啦,贴个链接
surefire破坏jacoco配置)