如何配置多模块Maven + Sonar + JaCoCo以提供合并的覆盖率报告?

我已经在互联网上上下搜索了这个。与Maven属性(例如${sonar.jacoco.reportPath},或org.jacoco:jacoco-maven-plugin:prepare-agent或通过设置)maven-surefire-plugin argLine有关的答案很多-javaagent

这些答案,无论是单独还是组合使用,都不怎么产生我的期望:覆盖率报告,如果在更高级别的测试中使用了某个类(例如正在使用的实体),则该类将覆盖该类由DAO负责,即使它自己的模块中的测试并未完全涵盖它。

请问某处是否有明确的配置来实现这一目标?



万千封印
浏览 417回答 3
3回答

慕的地8271018

我和您处在同一情况下,分散在整个互联网上的一半答案很烦人,因为似乎很多人都遇到了同样的问题,但是没有人会费心去解释他们是如何解决的。该声纳的文档是指 一个GitHub的项目,例子是有帮助的。我要解决的问题是将集成测试逻辑应用于常规单元测试(尽管正确的单元测试应特定于子模块,但并非总是如此)。在父pom.xml中,添加以下属性:<properties>&nbsp; &nbsp; <!-- Sonar -->&nbsp; &nbsp; <sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>&nbsp; &nbsp; <sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>&nbsp; &nbsp; <sonar.jacoco.reportPath>${project.basedir}/../target/jacoco.exec</sonar.jacoco.reportPath>&nbsp; &nbsp; <sonar.language>java</sonar.language></properties>这将使Sonar拾取同一位置(父项目中的目标文件夹)中所有子模块的单元测试报告。它还告诉Sonar重用手动运行的报告,而不是滚动自己的报告。我们只需将jacoco-maven-plugin放置在build / plugins内的父pom中,即可为所有子模块运行:<plugin>&nbsp; &nbsp; <groupId>org.jacoco</groupId>&nbsp; &nbsp; <artifactId>jacoco-maven-plugin</artifactId>&nbsp; &nbsp; <version>0.6.0.201210061924</version>&nbsp; &nbsp; <configuration>&nbsp; &nbsp; &nbsp; &nbsp; <destFile>${sonar.jacoco.reportPath}</destFile>&nbsp; &nbsp; &nbsp; &nbsp; <append>true</append>&nbsp; &nbsp; </configuration>&nbsp; &nbsp; <executions>&nbsp; &nbsp; &nbsp; &nbsp; <execution>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <id>agent</id>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <goals>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <goal>prepare-agent</goal>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </goals>&nbsp; &nbsp; &nbsp; &nbsp; </execution>&nbsp; &nbsp; </executions></plugin>destFile将报告文件放置在Sonar会查找的位置,并将append其添加到文件中,而不是覆盖它。这会将所有JaCoCo报告合并到同一文件中的所有子模块。Sonar将为每个子模块查看该文件,因为这就是我们在上面指出的内容,因此我们可以为Sonar中的多模块文件提供组合的单元测试结果。

忽然笑

自0.7.7版以来的新方法从0.7.7版开始,有一种新方法可以创建汇总报告:您创建一个单独的“报告”项目,该项目将收集所有必要的报告(聚合器项目中的任何目标均在其模块之前执行,因此无法使用)。aggregator pom&nbsp; |- parent pom&nbsp; |- module a&nbsp; |- module b&nbsp; |- report module&nbsp;该根POM看起来是这样的(不要忘了在模块添加新的报表模块):<build><plugins>&nbsp; <plugin>&nbsp; &nbsp; <groupId>org.jacoco</groupId>&nbsp; &nbsp; <artifactId>jacoco-maven-plugin</artifactId>&nbsp; &nbsp; <version>0.7.8</version>&nbsp; &nbsp; <executions>&nbsp; &nbsp; &nbsp; <execution>&nbsp; &nbsp; &nbsp; &nbsp; <id>prepare-agent</id>&nbsp; &nbsp; &nbsp; &nbsp; <goals>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <goal>prepare-agent</goal>&nbsp; &nbsp; &nbsp; &nbsp; </goals>&nbsp; &nbsp; &nbsp; </execution>&nbsp; &nbsp; </executions>&nbsp; </plugin></plugins>每个子模块的poms根本不需要更改。报表模块中的pom 如下所示:<!-- Add all sub modules as dependencies here --><dependencies>&nbsp; <dependency>&nbsp; &nbsp; <module a>&nbsp; </dependency>&nbsp; <dependency>&nbsp; &nbsp; <module b>&nbsp; </dependency>&nbsp;...&nbsp; <build>&nbsp; &nbsp; <plugins>&nbsp; &nbsp; &nbsp; <plugin>&nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.jacoco</groupId>&nbsp; &nbsp; &nbsp; &nbsp; <artifactId>jacoco-maven-plugin</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; <version>0.7.8</version>&nbsp; &nbsp; &nbsp; &nbsp; <executions>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <execution>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <id>report-aggregate</id>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <phase>verify</phase>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <goals>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <goal>report-aggregate</goal>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </goals>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </execution>&nbsp; &nbsp; &nbsp; &nbsp; </executions>&nbsp; &nbsp; &nbsp; </plugin>&nbsp; &nbsp; </plugins>&nbsp; </build>完整的示例可以在这里找到。
打开App,查看更多内容
随时随地看视频慕课网APP