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

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

问题描述

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

无论是单独还是组合使用,这些答案中的任何一个都不怎么产生我所追求的: 覆盖率报告显示了一个类是否在堆栈较高级别的测试中使用,例如DAO正在使用的实体,即使该类未在其自己的模块中被测试完全覆盖.

请问某个地方有明确的配置吗?

声纳文档请参阅带有示例的GitHub项目,是有帮助的.我要解决的问题是将集成测试逻辑应用于常规的单元测试(尽管正确的单元测试应特定于子模块,但并非总是如此).

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

 <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放置在build/plugins内的父pom中,就可以对所有子模块运行jacoco-maven-plugin:

 <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报告合并在一起.

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

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.

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.

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).

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>

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 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 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天全站免登陆