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

Maven项目JaCoCo插件执行mvn test无法生成XML覆盖率报告

问题根因
  • 执行mvn test只会运行到test生命周期阶段,你配置的集成测试agent初始化、exec文件合并逻辑都绑定在pre-integration-test、post-integration-test阶段,这部分配置在执行test命令时完全不会触发。
  • 你绑定在test阶段的report-aggregate目标是专门给多模块项目做跨模块覆盖率聚合用的,单模块项目用这个目标本身就不匹配;且该目标默认读取的exec文件路径是各模块target目录下的jacoco.exec,和你自定义的${project.build.directory}/../../target/coverage-reports/jacoco-ut.exec路径不一致,读不到覆盖率原始数据自然不会生成报告。
  • 两个prepare-agent配置了相同的propertyName,后者会覆盖前者的配置,单测场景下的agent参数会被集成测试的配置覆盖,导致参数传递异常。
  • 报告生成目标没有显式指定输出格式、数据源路径,默认配置和你的自定义路径不匹配。
修复方案
  • 单测场景直接使用report目标生成报告,替换掉原来绑定在test阶段的report-aggregate,显式指定读取单测生成的exec文件、输出XML/HTML格式、输出路径。
  • 给单测、集成测试的agent配置不同的propertyName,避免配置互相覆盖。
  • 把合并exec、生成聚合总报告的逻辑绑定到verify阶段,需要看合并覆盖率的时候执行mvn verify触发即可。
  • 确保surefire、failsafe插件正确引用对应的JaCoCo agent参数。

修正后的JaCoCo插件配置参考:

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>${jacoco-maven-plugin.version}</version>
    <executions>
        <!-- 单测agent配置,独立属性名避免冲突 -->
        <execution>
            <id>jacoco-initialize-unit-test</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
            <configuration>
                <destFile>${project.build.directory}/../../target/coverage-reports/jacoco-ut.exec</destFile>
                <append>true</append>
                <propertyName>jacoco.ut.argLine</propertyName>
            </configuration>
        </execution>
        <!-- test阶段生成单测覆盖率报告,显式开启XML格式 -->
        <execution>
            <id>jacoco-generate-ut-report</id>
            <phase>test</phase>
            <goals>
                <goal>report</goal>
            </goals>
            <configuration>
                <dataFile>${project.build.directory}/../../target/coverage-reports/jacoco-ut.exec</dataFile>
                <formats>
                    <format>XML</format>
                    <format>HTML</format>
                </formats>
                <outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory>
            </configuration>
        </execution>

        <!-- 以下为集成测试+合并+聚合报告逻辑,执行mvn verify时触发 -->
        <execution>
            <id>jacoco-initialize-integration-test</id>
            <phase>pre-integration-test</phase>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
            <configuration>
                <destFile>${project.build.directory}/../../target/coverage-reports/jacoco-it.exec</destFile>
                <append>true</append>
                <propertyName>jacoco.it.argLine</propertyName>
            </configuration>
        </execution>
        <execution>
            <id>jacoco-merge</id>
            <phase>post-integration-test</phase>
            <goals>
                <goal>merge</goal>
            </goals>
            <configuration>
                <fileSets>
                    <fileset>
                        <directory>${project.build.directory}/../../target/coverage-reports/</directory>
                        <includes>
                            <include>jacoco-ut.exec</include>
                            <include>jacoco-it.exec</include>
                        </includes>
                    </fileset>
                </fileSets>
                <destFile>${project.build.directory}/../../target/coverage-reports/jacoco.exec</destFile>
            </configuration>
        </execution>
        <execution>
            <id>jacoco-generate-aggregate-report</id>
            <phase>verify</phase>
            <goals>
                <goal>report-aggregate</goal>
            </goals>
            <configuration>
                <dataFile>${project.build.directory}/../../target/coverage-reports/jacoco.exec</dataFile>
                <formats>
                    <format>XML</format>
                    <format>HTML</format>
                </formats>
                <outputDirectory>${project.reporting.outputDirectory}/jacoco-aggregate</outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

同步需要检查maven-surefire-plugin配置,确保引用单测agent参数,否则agent不会挂载到测试进程,会导致exec文件为空无法生成报告:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.1.2</version>
    <configuration>
        <argLine>${jacoco.ut.argLine}</argLine>
    </configuration>
</plugin>

如果使用maven-failsafe-plugin跑集成测试,同理在配置中引用${jacoco.it.argLine}即可。

配置调整完成后,执行mvn clean test,即可在target/site/jacoco-ut目录下找到生成的jacoco.xml格式报告;执行mvn clean verify可在target/site/jacoco-aggregate目录下拿到单测+集成测试合并后的XML覆盖率报告。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.26 11:21:28