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

查看:88
本文介绍了如何配置多模块 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.

声纳文档 参考 一个带有示例的 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天全站免登陆