You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Gradle复合构建下如何用Jacoco+SonarQube生成合并测试覆盖率报告

Monorepo下用SonarQube+Jacoco生成合并测试覆盖率报告

核心思路

Monorepo下要生成合并覆盖率报告,核心是先让Jacoco为每个应用/库模块生成独立的覆盖率数据,再通过统一配置将所有数据提交给SonarQube完成汇总。

具体实现步骤

1. 为每个模块配置Jacoco生成覆盖率数据

Gradle项目示例

在每个子模块的build.gradle中添加Jacoco插件并配置报告输出:

plugins {
    id 'jacoco'
}

jacoco {
    toolVersion = "0.8.10" // 选择与SonarQube兼容的版本
}

test {
    finalizedBy jacocoTestReport // 测试完成后自动生成报告
}

jacocoTestReport {
    reports {
        xml.required = true // 必须生成XML格式,供SonarQube解析
        html.required = true // 可选,用于本地查看模块覆盖率
    }
}

Maven项目示例

在每个子模块的pom.xml中配置Jacoco插件:

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.8.10</version>
    <executions>
        <execution>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
        </execution>
        <execution>
            <id>report</id>
            <phase>test</phase>
            <goals>
                <goal>report</goal>
            </goals>
        </execution>
    </executions>
</plugin>

2. 收集并合并各模块的覆盖率数据

Gradle项目:统一收集报告路径

在根项目的build.gradle中,通过任务遍历所有子模块,收集Jacoco XML报告路径:

def allJacocoXmlReports = subprojects.collect { project ->
    "${project.buildDir}/reports/jacoco/test/jacocoTestReport.xml"
}.join(',')

sonarqube {
    properties {
        property 'sonar.coverage.jacoco.xmlReportPaths', allJacocoXmlReports
    }
}

Maven项目:用Jacoco合并.exec文件

在根项目的pom.xml中添加合并配置,将所有子模块的.exec文件合并为一个,再生成统一的XML报告:

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.8.10</version>
    <executions>
        <execution>
            <id>merge-coverage</id>
            <phase>test</phase>
            <goals>
                <goal>merge</goal>
            </goals>
            <configuration>
                <fileSets>
                    <fileSet>
                        <directory>${project.basedir}</directory>
                        <includes>
                            <include>**/target/jacoco.exec</include>
                        </includes>
                    </fileSet>
                </fileSets>
                <destFile>${project.build.directory}/jacoco-merged.exec</destFile>
            </configuration>
        </execution>
        <execution>
            <id>merged-report</id>
            <phase>test</phase>
            <goals>
                <goal>report</goal>
            </goals>
            <configuration>
                <dataFile>${project.build.directory}/jacoco-merged.exec</dataFile>
                <outputDirectory>${project.build.directory}/jacoco-merged-report</outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

3. 配置SonarQube扫描合并数据

在根项目的SonarQube配置文件(sonar-project.properties或构建脚本)中指定合并后的覆盖率报告路径:

# Maven项目用合并后的XML报告路径
sonar.coverage.jacoco.xmlReportPaths=target/jacoco-merged-report/jacoco.xml

# 通用配置:指定项目key和SonarQube地址
sonar.projectKey=your-monorepo-key
sonar.host.url=http://your-sonarqube-server

4. 执行扫描生成合并报告

运行根项目的SonarQube扫描命令:

  • Gradle:./gradlew test sonarqube
  • Maven:mvn test sonar:sonar

扫描完成后登录SonarQube控制台,即可查看整个monorepo的合并测试覆盖率报告。

注意事项

  • 确保所有子模块的测试任务执行完毕后再启动SonarQube扫描,避免覆盖率数据缺失
  • 保持Jacoco与SonarQube版本兼容,建议参考SonarQube官方文档核对版本匹配关系
  • 跨模块调用的代码覆盖率会被Jacoco自动统计,无需额外配置

内容的提问来源于stack exchange,提问作者caeus

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 03:15:56