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

多模块Maven项目Jenkins集成SonarQube时Jacoco报告无法找到

多模块Maven项目Jacoco覆盖率报告SonarQube不显示的排查与解决

核心问题定位

日志明确提示SonarQube找不到配置的sonar.coverage.jacoco.xmlReportPaths路径下的报告文件,因此会自动 fallback 到默认路径查找。

分步解决步骤

1. 确认报告文件实际存在性

  • 直接在Jenkins工作区手动检查code-coverage-report/target/site/jacoco-aggregate/jacoco.xml是否存在
    • 若不存在:说明Jacoco聚合报告的生成逻辑有问题,需先确保jacoco:aggregate目标在Maven构建流程中被正确执行
    • 若存在:检查路径是否存在大小写错误(Linux环境下路径区分大小写),或目录名称与配置完全匹配

2. 修正SonarQube路径配置

  • 多模块项目建议使用基于Jenkins工作区根目录的相对路径,或通过Maven属性动态指定:
    mvn sonar:sonar \
      -Dsonar.projectKey=你的项目Key \
      -Dsonar.coverage.jacoco.xmlReportPaths=${project.basedir}/code-coverage-report/target/site/jacoco-aggregate/jacoco.xml
    
  • 若在Jenkins Pipeline的withSonarQubeEnv块中配置,需使用工作区绝对路径:
    stage('SonarQube 分析') {
      steps {
        withSonarQubeEnv('SonarQube Server') {
          sh "mvn sonar:sonar -Dsonar.coverage.jacoco.xmlReportPaths=${WORKSPACE}/code-coverage-report/target/site/jacoco-aggregate/jacoco.xml"
        }
      }
    }
    

3. 确保Jacoco聚合报告正确生成

  • 在聚合模块(如你的code-coverage-report模块)的pom.xml中配置Jacoco插件:
    <build>
      <plugins>
        <plugin>
          <groupId>org.jacoco</groupId>
          <artifactId>jacoco-maven-plugin</artifactId>
          <version>0.8.10</version>
          <executions>
            <execution>
              <id>prepare-agent</id>
              <goals>
                <goal>prepare-agent</goal>
              </goals>
            </execution>
            <execution>
              <id>report-aggregate</id>
              <phase>verify</phase>
              <goals>
                <goal>aggregate</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
      </plugins>
    </build>
    
  • 确保Maven构建时执行verify阶段,触发聚合报告生成:
    mvn clean verify
    

4. 检查SonarQube项目配置

  • 登录SonarQube进入对应项目的项目设置 → 覆盖率,确认未覆盖全局路径配置
  • 确保SonarQube Java插件为最新稳定版,保证对Jacoco XML报告的兼容性

验证步骤

  1. 重新执行Jenkins Pipeline,查看日志是否仍有找不到报告的警告
  2. 执行完成后刷新SonarQube仪表盘,检查覆盖率数据是否正常显示

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 22:12:40