尽管用于依赖项的聚合报告,但在我的情况下它不起作用 - 我使用如下项目结构:jacoco:report-aggregate
project
└─ module-api ⇦ API definition
└─ module-spec ⇦ module with black box tests through the API
└─ module-impl1 ⇦ implementation 1 of the API
└─ module-impl2 ⇦ another implementation of the API
└─ module-test1 ⇦ some dependencies + impl1, apply the tests from spec
└─ module-test2 ⇦ some dependencies + impl2, apply the tests from spec
在本例中,生成了一个空报告。module-test1 和 module-test2 项目是使用 POM 打包类型,这也不适合在它们上运行,因为它需要 target/classes 目录中的类。report-aggregate
report
我的解决方案
我配置了jacoco插件,通过添加以下内容,将其在项目的目标/类目录中的类转储:<classDumpDir>${project.build.outputDirectory}</classDumpDir>
这使得常规目标得以实现。完整的代码是:report
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.5</version>
<configuration>
<destFile>${project.build.directory}/coverage-reports/code-coverage.exec</destFile>
<dataFile>${project.build.directory}/coverage-reports/code-coverage.exec</dataFile>
<outputDirectory>${project.reporting.outputDirectory}/code-coverage</outputDirectory>
<propertyName>jacocoArgLine</propertyName>
<classDumpDir>${project.build.outputDirectory}</classDumpDir>
</configuration>
<executions>
<execution>
<id>prepare-jacoco-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>post-integration-test</id>
<phase>post-integration-test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
请注意,检测的类来自所有依赖项,因此需要进行一些筛选以缩小报告范围。请参见: https://www.eclemma.org/jacoco/trunk/doc/prepare-agent-mojo.html