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

Maven集成Jacoco覆盖率检查失效:低于85%构建不失败

问题:Maven Jacoco插件未生成执行文件导致覆盖率检查不生效

我想配置Maven构建,当测试覆盖率低于85%时让构建失败。用了Jacoco插件,但不管覆盖率如何构建都不会失败。IDEA里带覆盖率运行测试正常,但执行mvn clean verify或mvn test时,会提示:

[INFO] Skipping JaCoCo execution due to missing execution data file:C:\Users\Shpot\Desktop\payneteasy\EQSP_project\target\jacoco.exec

现有pom.xml配置片段

<build>
    <plugins>
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.8.8</version>
            <executions>
                <execution>
                    <id>default-prepare-agent</id>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                    <configuration>
                        <destFile>${basedir}/target/jacoco.exec</destFile>
                    </configuration>
                </execution>
                <execution>
                    <id>check</id>
                    <goals>
                        <goal>check</goal>
                    </goals>
                    <configuration>
                        <rules>
                            <rule implementation="org.jacoco.maven.RuleConfiguration">
                                <element>BUNDLE</element>
                                <limits>
                                    <limit implementation="org.jacoco.report.check.Limit">
                                        <counter>INSTRUCTION</counter>
                                        <value>COVEREDRATIO</value>
                                        <minimum>0.85</minimum>
                                    </limit>
                                    <limit implementation="org.jacoco.report.check.Limit">
                                        <counter>CLASS</counter>
                                        <value>MISSEDCOUNT</value>
                                        <maximum>0</maximum>
                                    </limit>
                                </limits>
                            </rule>
                        </rules>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
<dependencies>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter</artifactId>
        <version>5.9.2</version>
        <scope>test</scope>
    </dependency>
</dependencies>

JUnit5测试代码

package pne;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

class CcsrchTest {

    @Test
    void hideCreditCardInformationInText() {
     ...
    }
}

问题原因

Jacoco的prepare-agent目标没有正确绑定到测试阶段,导致测试执行时未生成jacoco.exec文件,后续check阶段因找不到文件直接跳过,所以不会触发覆盖率检查失败。另外,缺少Maven Surefire插件的明确配置,可能导致JUnit5测试执行时无法激活Jacoco代理。

修复步骤

  1. 修正Jacoco的prepare-agent执行配置
    自定义的default-prepare-agentID可能干扰了默认绑定逻辑,去掉该ID,让插件自动绑定到test阶段:

    <execution>
        <!-- 移除自定义ID,使用默认绑定 -->
        <goals>
            <goal>prepare-agent</goal>
        </goals>
        <configuration>
            <destFile>${basedir}/target/jacoco.exec</destFile>
        </configuration>
    </execution>
    
  2. 添加Maven Surefire插件配置
    明确配置Surefire插件以支持JUnit5测试,确保测试执行时能触发Jacoco代理:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>3.1.2</version>
        <configuration>
            <includes>
                <include>**/*Test.java</include>
            </includes>
        </configuration>
    </plugin>
    
  3. 验证check阶段的执行时机
    jacoco:check默认绑定到verify阶段,所以需要执行mvn clean verify才会触发检查。如果想在test阶段后立即检查,可给check执行添加阶段绑定:

    <execution>
        <id>check</id>
        <phase>test</phase> <!-- 绑定到test阶段后执行 -->
        <goals>
            <goal>check</goal>
        </goals>
        <configuration>
            <rules>
                <rule implementation="org.jacoco.maven.RuleConfiguration">
                    <element>BUNDLE</element>
                    <limits>
                        <limit implementation="org.jacoco.report.check.Limit">
                            <counter>INSTRUCTION</counter>
                            <value>COVEREDRATIO</value>
                            <minimum>0.85</minimum>
                        </limit>
                        <limit implementation="org.jacoco.report.check.Limit">
                            <counter>CLASS</counter>
                            <value>MISSEDCOUNT</value>
                            <maximum>0</maximum>
                        </limit>
                    </limits>
                </rule>
            </rules>
        </configuration>
    </execution>
    
  4. 清理并重新构建
    执行mvn clean verify,此时会生成jacoco.exec文件,若覆盖率低于85%,构建会直接失败。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 05:45:12