我正在尝试将 jacoco 与 Maven 一起使用。当我运行 mvn clean test 时,我希望覆盖率输出写入 target/coverage-reports,但是当我在构建后打开 index.html 时,它是空的。
我检查了以下内容, - jacoco.exec 文件存在并且其中有一堆类名 - 在覆盖 html 中,有一个链接“Sessions”,当我点击它时,如果我的类似乎已被执行,我会看到一堆 - 运行 Maven 命令时没有看到任何错误或警告
我很困惑为什么报告是空的。从我看到的所有例子来看,这似乎应该可行。我错过了什么?
我正在为 Maven 插件使用以下 jacoco 配置
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<configuration>
<append>true</append>
<skip>false</skip>
<excludes/>
<outputDirectory>${project.build.directory}/coverage-reports/</outputDirectory>
<dataFile>${project.build.directory}/jacoco.exec</dataFile>
<includes>
<include>mypackage.*</include>
</includes>
<check>false</check>
</configuration>
<executions>
<execution>
<id>pre-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>post-test</id>
<goals>
<goal>report</goal>
</goals>
<phase>test</phase>
</execution>
</executions>
</plugin>
更新
显然这与我提供的包含模式有关。当我删除包含内容时,它确实显示了覆盖范围,但超出了我的需要。我仍在尝试找出用于执行正确包含的模式。
最佳答案
你还需要将它挂接到surefire,将其更改为
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.6.3.201306030806</version>
<executions>
<!-- pre-unit-test execution helps setting up some maven property,
which will be used later by JaCoCo -->
<execution>
<id>pre-unit-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</destFile>
<!-- passing property which will contains settings for JaCoCo agent.
If not specified, then "argLine" would be used for "jar" packaging -->
<propertyName>surefireArgLine</propertyName>
</configuration>
</execution>
<!-- report phase setup -->
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<!-- output file with report data. -->
<dataFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</dataFile>
<!-- output directory for the reports. -->
<outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.14</version>
<executions>
<execution>
<id>default-test</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<argLine>${surefireArgLine}</argLine>
</configuration>
</execution>
</executions>
<configuration>
<argLine>-XX:MaxPermSize=512m</argLine>
</configuration>
</plugin>
</plugins>
</build>
关于java - jacoco maven 插件不报告测试覆盖率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25111004/