序号JaCoCo 目标常绑定阶段功能描述pom.xml 配置片段
1 prepare-agent

initialize 或

 process-classes 或 

test-compile

准备 JaCoCo Java 代理以收集测试期间的覆盖率数据。通常在测试运行之前设置代理。

<execution>

    <id>prepare-agent</id>

    <goals>

        <goal>prepare-agent</goal>

    </goals>

    <phase>process-classes</phase>

</execution>

2 instrument process-classes 对类进行离线插桩。用于在不运行测试的情况下对类进行插桩。 <execution>

    <id>instrument</id>

    <goals>

        <goal>instrument</goal>

    </goals>

    <phase>process-classes</phase>

</execution>cution
3 dump

自定义阶段或

 test 后

从运行中的 JVM 中提取当前的执行数据。

<execution>

    <id>dump</id>

    <goals>

        <goal>dump</goal>

    </goals>

    <phase>test</phase>

</execution>

4 report

post-integration-test 或

 verify

基于收集的执行数据生成覆盖率报告。通常在测试完成后生成报告。

<execution>

    <id>report</id>

    <goals>

        <goal>report</goal>

    </goals>

    <phase>post-integration-test</phase>

</execution>

5 report-integration post-integration-test 专门用于生成集成测试的覆盖率报告。

<execution>

    <id>report-integration</id>

    <goals>

        <goal>report-integration</goal>

    </goals>

    <phase>post-integration-test</phase>

</execution>

6 check verify 检查代码覆盖率是否达到预定的阈值标准。通常在报告生成后执行以验证覆盖率。

<execution>

    <id>check</id>

    <goals>

        <goal>check</goal>

    </goals>

    <phase>verify</phase>

</execution>

7 merge verify 合并多个执行数据文件,生成一个综合的覆盖率报告。

<execution>

    <id>merge</id>

    <goals>

        <goal>merge</goal>

    </goals>

    <phase>verify</phase>

</execution>

使用说明

  • 选择合适的阶段: 根据项目需求,将 JaCoCo 的目标绑定到合适的 Maven 构建阶段。例如,prepare-agent 通常在测试编译之后,测试运行之前的阶段执行,以确保在测试运行时能够收集到覆盖率数据。
  • 灵活的阶段绑定: 根据项目的 CI/CD 流程,可以调整阶段绑定以优化构建和测试过程。例如,在集成测试后生成报告可以确保覆盖率数据的完整性。
  • 结合其他插件: JaCoCo 通常与测试插件(如 Surefire 或 Failsafe)结合使用,以确保在测试执行时正确收集覆盖率数据。

通过正确配置 JaCoCo 插件的执行阶段,可以有效地收集和分析代码覆盖率,帮助提升代码质量和测试覆盖率。

<execution>    <id>prepare-agent</id>    <goals>        <goal>prepare-agent</goal>    </goals>    <phase>process-classes</phase></execution>
posted on 2024-12-27 09:55  卡米i  阅读(33)  评论(0)    收藏  举报