如何配置多模块 Maven + Sonar + JaCoCo 以提供合并的覆盖率报告? [英] How to configure multi-module Maven + Sonar + JaCoCo to give merged coverage report?

查看:33
本文介绍了如何配置多模块 Maven + Sonar + JaCoCo 以提供合并的覆盖率报告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在互联网上搜索了这个.关于 Maven 属性,例如 ${sonar.jacoco.reportPath}org.jacoco:jacoco-maven-plugin:prepare-agent 或使用 -javaagent 设置 maven-surefire-plugin argLine.

I've searched up and down the internet for this one. There's lots of half-answers out there, to do with Maven properties such as ${sonar.jacoco.reportPath}, or org.jacoco:jacoco-maven-plugin:prepare-agent or setting maven-surefire-plugin argLine with -javaagent.

这些答案,无论是单独的还是组合的,都没有产生我想要的东西:覆盖率报告,如果某个类用于堆栈更高层的测试(例如 DAO 使用的实体,即使它自己的模块中的测试未完全覆盖),则显示该类已覆盖.

Some how, none of these answers, either on their own, or in combination, are producing what I'm after: A coverage report which shows a class as covered if it is used in tests higher up the stack, such as entities being used by DAOs, even though it was not fully covered by tests in its own module.

请问是否有明确的配置来实现这一点?

Is there a definitive config somewhere, to achieve this, please?

推荐答案

我和你的情况一样,网上散落的一半答案很烦人,因为好像很多人都有同样的问题,但没有人们可能会费心去充分解释他们是如何解决这个问题的.

I was in the same situation as you, the half answers scattered throughout the Internet were quite annoying, since it seemed that many people had the same issue, but no one could be bothered to fully explain how they solved it.

Sonar 文档 参考 一个带有示例的 GitHub 项目很有帮助.我为解决这个问题所做的是将集成测试逻辑应用于常规单元测试(尽管正确的单元测试应该是特定于子模块的,但情况并非总是如此).

The Sonar docs refer to a GitHub project with examples that are helpful. What I did to solve this was to apply the integration tests logic to regular unit tests (although proper unit tests should be submodule specific, this isn't always the case).

在父 pom.xml 中,添加以下属性:

In the parent pom.xml, add these properties:

<properties>
    <!-- Sonar -->
    <sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
    <sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
    <sonar.jacoco.reportPath>${project.basedir}/../target/jacoco.exec</sonar.jacoco.reportPath>
    <sonar.language>java</sonar.language>
</properties>

这将使 Sonar 为同一位置(父项目中的目标文件夹)中的所有子模块获取单元测试报告.它还告诉 Sonar 重​​用手动运行的报告,而不是滚动自己的报告.我们只需要将 jacoco-maven-plugin 放置在父 pom 中,在 build/plugins 中为所有子模块运行:

This will make Sonar pick up unit testing reports for all submodules in the same place (a target folder in the parent project). It also tells Sonar to reuse reports ran manually instead of rolling its own. We just need to make jacoco-maven-plugin run for all submodules by placing this in the parent pom, inside build/plugins:

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.6.0.201210061924</version>
    <configuration>
        <destFile>${sonar.jacoco.reportPath}</destFile>
        <append>true</append>
    </configuration>
    <executions>
        <execution>
            <id>agent</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
        </execution>
    </executions>
</plugin>

destFile 将报告文件放在 Sonar 查找的位置,append 将其附加到文件中而不是覆盖它.这会将所有子模块的所有 JaCoCo 报告合并到同一文件中.

destFile places the report file in the place where Sonar will look for it and append makes it append to the file rather than overwriting it. This will combine all JaCoCo reports for all submodules in the same file.

Sonar 将查看每个子模块的文件,因为这就是我们在上面指出的内容,为我们提供了 Sonar 中多模块文件的组合单元测试结果.

Sonar will look at that file for each submodule, since that's what we pointed him at above, giving us combined unit testing results for multi module files in Sonar.

这篇关于如何配置多模块 Maven + Sonar + JaCoCo 以提供合并的覆盖率报告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆