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

多模块Maven项目中JaCoCo报告未包含全部依赖类的解决问询

多模块Maven项目JaCoCo聚合报告包含所有依赖类的配置方法

问题描述

我维护一个多模块Maven项目,根pom.xml和mytests模块的pom.xml配置如下:

根pom.xml配置

<build>
    <plugins>
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.8.11</version>
            <executions>
                <execution>
                    <id>default-prepare-agent</id>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
<modules>
    <module>...</module>
    <module>mytests</module>
</modules>

mytests模块pom.xml配置

<build>
    <plugins>
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.8.11</version>
            <executions>
                <execution>
                    <id>report-aggregate</id>
                    <phase>verify</phase>
                    <goals>
                        <goal>report-aggregate</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

<dependencies>
    <dependency>
        <groupId>a.b.company</groupId>
        <artifactId>my.company.services</artifactId>
    </dependency>
</dependencies>

my.company.services依赖本身还有其他子依赖,但执行根项目命令mvn test jacoco:report-aggregate后,生成的聚合报告仅包含my.company.services模块的类,其余依赖类未被纳入报告,需要调整JaCoCo配置以覆盖所有相关依赖类。

解决配置步骤

1. 扩展mytests模块的Jacoco插件配置

在mytests模块的Jacoco插件report-aggregate执行节点中,添加configuration块,通过includes指定需要包含的类路径(支持通配符匹配),同时可指定源码目录以在报告中展示源码:

<build>
    <plugins>
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.8.11</version>
            <executions>
                <execution>
                    <id>report-aggregate</id>
                    <phase>verify</phase>
                    <goals>
                        <goal>report-aggregate</goal>
                    </goals>
                    <configuration>
                        <!-- 匹配a.b.company下所有类,包括子依赖的类 -->
                        <includes>
                            <include>a/b/company/**/*.class</include>
                            <!-- 若需包含其他第三方包,添加对应规则 -->
                            <!-- <include>com/thirdparty/**/*.class</include> -->
                        </includes>
                        <!-- 可选:添加依赖模块的源码路径,让报告显示对应源码 -->
                        <sourceDirectories>
                            <sourceDirectory>${project.basedir}/../my.company.services/src/main/java</sourceDirectory>
                            <!-- 其他依赖模块的源码路径按需添加 -->
                        </sourceDirectories>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

2. 确保所有依赖模块编译时生成调试信息

JaCoCo需要类文件包含调试信息(行号、源码关联等)才能生成完整报告,因此所有相关模块需在maven-compiler-plugin中开启调试配置:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.11.0</version>
    <configuration>
        <debug>true</debug>
        <debuglevel>lines,vars,source</debuglevel>
    </configuration>
</plugin>

3. 使用正确的Maven命令执行

推荐直接执行绑定了verify阶段的命令,确保测试执行完成后再生成聚合报告:

mvn clean verify

避免手动执行jacoco:report-aggregate,防止因生命周期顺序问题导致数据不完整。

内容的提问来源于stack exchange,提问作者Marc Gil Sendra

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 19:43:12