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

如何用Jacoco Maven插件在组件测试模块统计全项目代码覆盖率?

问题描述

项目结构

myProject
    app
        module1
            submodule
            pom.xml
        pom.xml
    automation
        component-tests
        pom.xml
    pom.xml
pom.xml

场景与问题

使用Jacoco Maven插件统计代码覆盖率,component-tests模块仅含测试代码、无任何源码,该模块的测试基于Karate框架启动应用并执行微服务组件测试。

运行测试后,各模块均生成jacoco.exec文件,但component-tests模块虽有该文件却无覆盖率数据,未对全项目代码插桩。尝试过includes配置,但仅在模块范围内生效,无法解决问题。

当前Maven Surefire参数:

javaagent:/Users/me/.m2/repo/org/jacoco/org.jacoco.agent/0.8.10/org.jacoco.agent-0.8.10-runtime

当前相关插件配置

Jacoco插件配置(coverage profile)

<profiles>
<profile>
  <id>coverage</id>
  <build>
    <plugins>
      <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>0.8.11</version>
        <executions>
          <execution>
            <id>generate-jacoco-coverage</id>
            <goals>
              <goal>prepare-agent</goal>
            </goals>
            <configuration>
              <propertyName>surefireArgLine</propertyName>
            </configuration>
          </execution>
          <execution>
            <id>jacoco-report</id>
            <phase>test</phase>
            <goals>
              <goal>report</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</profile>

Maven Surefire插件配置

<plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <configuration>
        <argLine>
          ${surefireArgLine}
        </argLine>
      </configuration>
    </plugin>
解决方案

要让component-tests模块的测试统计全项目代码覆盖率,核心是让Jacoco在生成报告时关联其他模块的源码,同时确保测试启动的应用进程被Jacoco插桩。具体调整步骤如下:

1. 统一管理Jacoco执行文件

在项目最顶层的pom.xml中,修改Jacoco插件配置,指定全局统一的jacoco.exec输出路径,避免各模块分散生成:

<profiles>
<profile>
  <id>coverage</id>
  <build>
    <plugins>
      <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>0.8.11</version>
        <configuration>
          <!-- 统一输出到项目根目录的target下 -->
          <destFile>${project.basedir}/target/jacoco.exec</destFile>
          <propertyName>surefireArgLine</propertyName>
        </configuration>
        <executions>
          <execution>
            <id>jacoco-prepare-agent</id>
            <goals>
              <goal>prepare-agent</goal>
            </goals>
          </execution>
          <!-- 移除单个模块的report执行,后续在component-tests模块统一生成 -->
        </executions>
      </plugin>
    </plugins>
  </build>
</profile>
</profiles>

2. 为component-tests配置源码关联

在automation/component-tests/pom.xml中,添加Jacoco的报告配置,明确指定需要统计覆盖率的其他模块源码路径:

<build>
  <plugins>
    <plugin>
      <groupId>org.jacoco</groupId>
      <artifactId>jacoco-maven-plugin</artifactId>
      <version>0.8.11</version>
      <executions>
        <execution>
          <id>jacoco-report</id>
          <phase>test</phase>
          <goals>
            <goal>report</goal>
          </goals>
          <configuration>
            <!-- 关联app模块的源码 -->
            <sourceDirectory>${project.parent.parent.basedir}/app/src/main/java</sourceDirectory>
            <!-- 关联module1的源码,多模块可重复添加该标签 -->
            <sourceDirectory>${project.parent.parent.basedir}/app/module1/src/main/java</sourceDirectory>
            <!-- 指定全局统一的jacoco.exec文件路径 -->
            <dataFile>${project.parent.parent.basedir}/target/jacoco.exec</dataFile>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

3. 确保应用启动时被插桩

如果component-tests的测试通过Maven插件(如spring-boot-maven-plugin)启动应用,需在对应插件配置中加入Jacoco代理参数:

<plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
  <configuration>
    <jvmArguments>${surefireArgLine}</jvmArguments>
  </configuration>
</plugin>

4. 运行测试生成报告

执行以下命令,激活coverage profile并运行component-tests的测试:

mvn clean test -Pcoverage -pl automation/component-tests -am

测试完成后,在automation/component-tests/target/site/jacoco目录下即可查看包含全项目代码的覆盖率报告。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 19:07:10