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

Jacoco-aggregate生成空报告求助(SonarQube集成场景)

解决Jacoco-Aggregate生成空报告的问题

1. 核对Coverage模块的Jacoco配置

得保证jacoco-aggregate目标正确引用了child1和common的jacoco.exec文件,配置示例如下:

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.8.11</version>
    <executions>
        <execution>
            <id>report-aggregate</id>
            <phase>verify</phase>
            <goals>
                <goal>aggregate</goal>
            </goals>
            <configuration>
                <dataFiles>
                    <dataFile>../child1/target/jacoco.exec</dataFile>
                    <dataFile>../common/target/jacoco.exec</dataFile>
                </dataFiles>
                <outputDirectory>${project.reporting.outputDirectory}/jacoco-aggregate</outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

重点检查dataFile的相对路径——是从coverage模块根目录出发的,别写错路径导致找不到exec文件。

2. 确认各模块的测试与exec生成时机

  • child1和common这类业务模块,必须配置Jacoco在test阶段生成exec,配置如下:
<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.8.11</version>
    <executions>
        <execution>
            <id>prepare-agent</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
        </execution>
    </executions>
</plugin>

这个配置会在测试启动前注入Jacoco代理,测试跑完自动生成jacoco.exec。

  • 替换执行命令:别用mvn clean install,改成mvn clean verify。因为verify阶段会触发所有模块的测试和验证流程,而coverage模块的aggregate绑定在verify阶段的话,能确保其他模块的exec已经生成完毕。

3. 检查exec文件是否有效

测试跑完后,手动查看child1和common下的jacoco.exec大小,如果文件几乎是空的,说明测试没执行或者Jacoco代理没生效。
可以单独跑某个模块的测试:mvn -pl child1 clean test,看控制台有没有[INFO] jacoco: Starting agent的日志,确认代理正常启动。

4. 排除模块自身的干扰

  • coverage模块就做聚合用,别加自己的业务代码或测试,不然Jacoco可能会读取自身空的exec文件,导致报告异常。
  • 检查父pom和子模块的Jacoco配置有没有冲突,比如有没有模块覆盖了jacoco.dataFile属性,导致exec生成路径不对。

5. SonarQube集成的收尾配置

在Sonar扫描的配置里,指定聚合XML报告的路径:

sonar.coverage.jacoco.xmlReportPaths=coverage/target/site/jacoco-aggregate/jacoco.xml

确保路径准确指向coverage模块生成的XML文件。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 12:34:56