SonarQube显示0%代码覆盖率但JaCoCo报告正常问题咨询
多模块Maven JaCoCo聚合报告SonarQube覆盖率为0%解决方案
根因分析
核心问题是SonarQube默认按模块维度匹配覆盖率数据:report-aggregate生成的全量覆盖率报告存放在module.tests模块下,其余业务模块(module/module.feature/module.repository等)默认只读取自身模块下的覆盖率报告,找不到对应业务代码的覆盖率数据,最终全项目覆盖率统计为0%。你用report目标仅能匹配到module.tests模块内的测试类覆盖率,也验证了这一逻辑。
所需配置调整
1. 调整JaCoCo配置
- 根pom.xml中仅保留
prepare-agent目标,删除根pom中的report-aggregate执行配置 - 将
report-aggregate执行配置仅放在module.tests模块的pom.xml中,同时在module.tests的依赖中引入所有业务模块,确保聚合报告能收集全量业务代码覆盖率:
<!-- module.tests/pom.xml 新增业务模块依赖 --> <dependencies> <dependency> <groupId>替换为你的项目groupId</groupId> <artifactId>module</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>替换为你的项目groupId</groupId> <artifactId>module.feature</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>替换为你的项目groupId</groupId> <artifactId>module.repository</artifactId> <version>${project.version}</version> </dependency> <!-- 其余需要统计覆盖率的业务模块依次添加 --> </dependencies>
调整后全量聚合报告固定生成在module.tests/target/site/jacoco/jacoco.xml路径。
2. 调整SonarQube扫描参数
在根pom.xml的properties节点,或者Bamboo的Sonar扫描任务参数中添加以下配置,让所有模块统一读取同一份聚合报告:
<properties> <!-- 所有模块统一读取module.tests下的聚合报告 --> <sonar.coverage.jacoco.xmlReportPaths>${project.basedir}/../module.tests/target/site/jacoco/jacoco.xml</sonar.coverage.jacoco.xmlReportPaths> <!-- 可选:排除不需要统计覆盖率的模块,比如文档模块、测试模块 --> <sonar.exclusions>**/module.doc/**/*,**/module.tests/**/*</sonar.exclusions> </properties>
如果是在Bamboo的Sonar扫描命令中传参,直接添加以下参数即可:-Dsonar.coverage.jacoco.xmlReportPaths=./module.tests/target/site/jacoco/jacoco.xml
验证
重新执行Bamboo构建,业务模块会从聚合报告中匹配对应代码的覆盖率数据,全项目覆盖率即可正常展示。
内容的提问来源于stack exchange,提问作者Patze
相关产品推荐
相关产品推荐

