多模块Maven项目Jacoco聚合报告生成方案咨询(GitHub Actions)
多模块Maven项目Jacoco聚合覆盖率报告解决方案
核心思路:利用Maven reactor自动识别子模块,结合Jacoco的report-aggregate动态配置
不用手动维护所有子模块依赖,通过Maven特性让report-aggregate自动扫描services下的所有子模块,同时复用已构建的源码和类文件,避免重复操作。
步骤1:在父POM中统一配置Jacoco插件
在项目根POM的<build><plugins>中添加Jacoco配置,确保所有子模块都生成.exec文件,并保留源码和类文件路径:
<plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.8.11</version> <executions> <execution> <id>prepare-agent</id> <goals> <goal>prepare-agent</goal> </goals> </execution> <execution> <id>report</id> <goals> <goal>report</goal> </goals> <phase>test</phase> </execution> </executions> <configuration> <destFile>${project.build.directory}/jacoco.exec</destFile> <dataFile>${project.build.directory}/jacoco.exec</dataFile> </configuration> </plugin>
步骤2:创建专门的聚合模块(可选但推荐)
在services模块下新增一个coverage-aggregate子模块,无需业务代码,仅用来配置report-aggregate。关键是用Maven通配符依赖,动态引入services下的所有子模块:
<!-- coverage-aggregate模块的POM.xml --> <project> <parent> <groupId>your.group.id</groupId> <artifactId>services</artifactId> <version>your.version</version> </parent> <artifactId>coverage-aggregate</artifactId> <packaging>pom</packaging> <dependencies> <!-- 动态引入services下所有子模块,无需手动添加 --> <dependency> <groupId>your.group.id</groupId> <artifactId>child-*</artifactId> <version>${project.version}</version> <type>test-jar</type> <scope>test</scope> </dependency> <!-- 引入common模块的测试依赖 --> <dependency> <groupId>your.group.id</groupId> <artifactId>common</artifactId> <version>${project.version}</version> <type>test-jar</type> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <executions> <execution> <id>report-aggregate</id> <phase>verify</phase> <goals> <goal>report-aggregate</goal> </goals> <configuration> <!-- 指定输出XML和CSV格式,可选HTML用于直观查看 --> <formats> <format>XML</format> <format>CSV</format> <format>HTML</format> </formats> <outputDirectory>${project.build.directory}/jacoco-aggregate</outputDirectory> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
步骤3:GitHub Actions中执行构建和聚合命令
在GitHub Actions的Workflow文件里,执行Maven命令时,通过-am自动构建聚合模块依赖的所有子模块:
jobs: coverage: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Set up JDK 17 uses: actions/setup-java@v4 with: java-version: '17' distribution: 'temurin' cache: maven - name: Build and generate aggregate coverage run: mvn clean verify -pl services/coverage-aggregate -am
解释:-pl指定构建聚合模块,-am会自动构建该模块依赖的所有子模块(services下的child系列和common),自动收集所有.exec文件、源码和类文件。
替代方案:无需新增聚合模块,直接在根POM配置动态聚合
如果不想新增模块,可在根POM中添加profile,利用Maven reactor的projects属性动态指定要聚合的模块:
<profiles> <profile> <id>jacoco-aggregate</id> <build> <plugins> <plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <executions> <execution> <id>report-aggregate</id> <phase>verify</phase> <goals> <goal>report-aggregate</goal> </goals> <configuration> <formats> <format>XML</format> <format>CSV</format> </formats> <outputDirectory>${project.build.directory}/jacoco-aggregate</outputDirectory> <!-- 动态指定要聚合的模块路径 --> <modules> <module>${project.basedir}/services/child-*</module> <module>${project.basedir}/common</module> </modules> </configuration> </execution> </executions> </plugin> </plugins> </build> </profile> </profiles>
GitHub Actions中执行命令:
run: mvn clean verify -P jacoco-aggregate
关键优势
- 无需手动维护子模块列表:新增/删除子模块时无需修改POM,Maven自动识别
- 复用构建产物:利用Maven reactor的依赖传递,自动收集所有子模块的.exec、源码和类文件,无需额外脚本
- 输出格式适配CI:直接生成XML和CSV报告,可用于后续代码覆盖率平台上传等流程
内容的提问来源于stack exchange,提问作者Wollfie
相关产品推荐
相关产品推荐

