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

SonarCloud覆盖率未计算及Jacoco与POM配置相关问题咨询

SonarCloud Jacoco覆盖率配置问题

问题背景

执行mvn verify后,target目录生成了jacoco.exec文件,本地SonarQube可正常显示覆盖率,但SonarCloud无法计算覆盖率。同时有两个疑问:

  1. 是否需要在SonarCloud中配置jacoco.exec文件的路径?
  2. 在profile中配置Jacoco而非直接配置plugin的意义是什么?

当前POM配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.4</version>
        <relativePath/> 
    </parent>

    <groupId>group</groupId>
    <artifactId>backend</artifactId>
    <version>1.0.0</version>
    <name>project</name>
    <description>Spring Boot Backend</description>

    <properties>
        <java.version>17</java.version>
        <sonar.java.source>17</sonar.java.source>
        <checkstyle-maven-plugin.version>3.2.0</checkstyle-maven-plugin.version>
        <checkstyle-version>10.3.4</checkstyle-version>
        <maven-site.version>3.7.1</maven-site.version>
        <jacoco.version>0.8.8</jacoco.version>
        <jacoco.path>${project.basedir}/../target/jacoco.exec</jacoco.path>
        <sonar.coverage.jacoco.xmlReportPaths>${basedir}/target/jacoco_report/jacoco.xml</sonar.coverage.jacoco.xmlReportPaths>
    </properties>

    <dependencies>
    
        <!-- Check style -->
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-checkstyle-plugin</artifactId>
            <version>${checkstyle-maven-plugin.version}</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-checkstyle-plugin</artifactId>
                <version>${checkstyle-maven-plugin.version}</version>
                <configuration>
                    <consoleOutput>true</consoleOutput>
                    <failsOnError>true</failsOnError>
                    <configLocation>google_checks.xml</configLocation>
                </configuration>
                <dependencies>
                    <dependency>
                            <groupId>com.puppycrawl.tools</groupId>
                            <artifactId>checkstyle</artifactId>
                            <version>${checkstyle-version}</version>
                    </dependency>
                </dependencies>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>check</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-site-plugin</artifactId>
                <version>${maven-site.version}</version>
            </plugin>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.2</version>
                <configuration>
                    <!--suppress UnresolvedMavenProperty -->
                    <argLine>${surefireArgLine}</argLine>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>${jacoco.version}</version>
                <executions>
                    <execution>
                        <id>pre-unit-test</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                        <configuration>
                            <propertyName>surefireArgLine</propertyName>
                        </configuration>
                    </execution>
                    <execution>
                        <id>post-unit-test</id>
                        <phase>test</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${jacoco.path}</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <reporting>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-checkstyle-plugin</artifactId>
                <version>${checkstyle-maven-plugin.version}</version>
                <reportSets>
                    <reportSet>
                        <reports>
                            <report>checkstyle</report>
                        </reports>
                    </reportSet>
                </reportSets>
            </plugin>
        </plugins>
    </reporting>
</project>

一、POM文件缺失的关键配置

1. 修正覆盖率文件路径错误

当前jacoco.path指向父目录的target,会导致覆盖率文件生成到当前项目外部,SonarCloud无法读取。同时sonar.coverage.jacoco.xmlReportPaths指定的XML路径与实际生成路径不匹配,需同步修正:

  • 修改properties中的jacoco.path为当前项目的target子目录:
    <jacoco.path>${project.build.directory}/jacoco-report</jacoco.path>
    
  • 确保sonar.coverage.jacoco.xmlReportPaths与生成路径一致:
    <sonar.coverage.jacoco.xmlReportPaths>${jacoco.path}/jacoco.xml</sonar.coverage.jacoco.xmlReportPaths>
    

2. 添加Jacoco XML报告生成配置

SonarCloud优先读取XML格式的覆盖率报告(二进制exec文件跨环境兼容性差),需在jacoco-maven-plugin的post-unit-test执行中添加XML格式生成配置:

<execution>
    <id>post-unit-test</id>
    <phase>verify</phase> <!-- 调整到verify阶段,确保Sonar分析前完成报告生成 -->
    <goals>
        <goal>report</goal>
    </goals>
    <configuration>
        <outputDirectory>${jacoco.path}</outputDirectory>
        <formats>
            <format>XML</format> <!-- 强制生成XML报告 -->
            <format>HTML</format> <!-- 保留HTML方便本地查看 -->
        </formats>
    </configuration>
</execution>

3. 确保构建阶段联动

执行Sonar分析时,需先完成测试与覆盖率报告生成,建议使用命令:

mvn verify sonar:sonar

二、额外问题解答

1. 是否需要在SonarCloud中配置jacoco.exec文件的路径?

不需要。SonarCloud默认会识别标准路径下的jacoco.exec,但更推荐使用XML报告(兼容性更强)。只需在POM中通过sonar.coverage.jacoco.xmlReportPaths指定XML报告路径,SonarCloud会自动读取,无需在后台额外配置。

2. 在profile中配置Jacoco而非直接配置plugin的意义是什么?

  • 环境隔离:日常开发时可跳过覆盖率生成,仅在CI/CD或Sonar分析时通过激活profile启用,避免拖慢本地构建速度。
  • 避免冲突:部分测试场景或插件可能与Jacoco代理冲突,放在profile中可灵活开关,不影响常规构建流程。
  • 多场景适配:不同环境(本地、测试、生产)可定义不同的Jacoco配置(如不同的报告格式、阈值要求),通过profile快速切换。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 05:03:37