我的项目具有以下结构,如下所示。我的集成测试在模块 A 中,它使用其他模块中的类。我希望 JaCoCo 包含模块 A 执行的其他模块的代码覆盖率。
我知道该插件可以合并其他模块的报告,但这是一个不同的场景。我正在模块 A 上执行集成测试
pom.xml
|
|
-----sub module A - application layer - executable integration-test with submodule B & C
|
|
-----sub module B - business logic - has no integration test
|
|
-----sub module C - business logic - has no integration test
我遇到了同样的问题并使用了 jacoco-aggragete 执行。我想,有很多解决方案,但我会尽力告诉您最适合您的问题。您应该将 jacoco 插件添加到 pom.xml 到根目录。
这个插件应该有 2 个不同的指定执行:第一个 jacoco-initialize,第二个 jacoco-report。之后,我认为你可以模块A,例如记者模块。 Reporter 模块的 pom 再次具有 jacoco 插件,但执行方式不同。该执行是 jacoco-site-aggregate。此执行计算依赖于模块的所有报告聚合。
你的根 pom.xml 应该有这个;
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.5</version>
<executions>
<execution>
<id>jacoco-initialize</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>jacoco-report</id>
<phase>verify</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
你的 module-A 的 pom.xml 应该有这个;
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>Module-B</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>Module-C</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.5</version>
<executions>
<execution>
<id>jacoco-site-aggregate</id>
<phase>verify</phase>
<goals>
<goal>report-aggregate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
请运行maven命令
mvn clean verify
现在,合并的报告应该位于您的模块-A>目标>站点>jacoco-aggregate中。
希望这个答案可以帮助您解决这个问题。也许这篇文章可以帮助你更多。
我通过在每个模块添加以下属性解决了同样的问题:
<propertie>
<sonar.sources>target/generated-sources/java</sonar.sources>
</properties>
我决定通过一个 Maven 插件来解决这个问题,该插件与 Jacoco 的插件很接近(相同的目标),但读取 Maven 的反应器来收集依赖项。这避免了创建一个专用模块来声明上面建议的依赖项并推荐用于声纳扫描仪。如果您仍然在解决这个问题,请看一下:https://github.com/codefilarete/jacoco-aggregate-submodule。 请注意,该功能已在 Jacoco 上提出,但没有得到答复:https://github.com/jacoco/jacoco/issues/1241