如何在Maven子模块中覆盖父POM的jacoco-check代码覆盖率配置
解决方案
修正子模块prepare-agent配置
你子模块default-prepare-agent执行中配置的<exclude>**/*</exclude>会导致Jacoco代理完全不统计任何代码的覆盖数据,先把这段exclude配置删除,或者调整为你真正需要排除的类路径。
配置子模块完全覆盖父POM规则
相同id的execution默认会合并配置,可通过以下两种方式实现子模块自定义阈值:
方式1:使用合并控制属性(推荐)
保留父POM的通用配置,仅覆盖规则部分,确认子模块jacoco-check的configuration节点已正确添加combine.self="override":
<execution> <id>jacoco-check</id> <goals> <goal>check</goal> </goals> <configuration combine.self="override"> <rules> <rule> <element>PACKAGE</element> <limits> <limit> <counter>LINE</counter> <value>COVEREDRATIO</value> <minimum>0.5</minimum> </limit> </limits> </rule> </rules> </configuration> </execution>
方式2:完全不继承父POM插件配置
如果仍存在合并冲突,直接在子模块Jacoco插件根节点添加<inherited>false</inherited>,完全屏蔽父POM的该插件配置,自行补全所有需要的执行逻辑:
<plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <inherited>false</inherited> <executions> <execution> <id>default-prepare-agent</id> <goals> <goal>prepare-agent</goal> </goals> </execution> <execution> <id>jacoco-check</id> <goals> <goal>check</goal> </goals> <configuration> <rules> <rule> <element>PACKAGE</element> <limits> <limit> <counter>LINE</counter> <value>COVEREDRATIO</value> <minimum>0.5</minimum> </limit> </limits> </rule> </rules> </configuration> </execution> </executions> </plugin>
校验生效配置
执行mvn help:effective-pom -pl test查看test子模块的有效POM,搜索Jacoco相关配置,确认check规则的minimum值已经是0.5,没有父POM的0.75规则残留。
子模块完全豁免校验配置
如果有子模块需要完全跳过Jacoco校验,直接在对应子模块的jacoco-check执行中添加<skip>true</skip>即可:
<execution> <id>jacoco-check</id> <goals> <goal>check</goal> </goals> <configuration> <skip>true</skip> </configuration> </execution>
内容的提问来源于stack exchange,提问作者Nanor
相关产品推荐
相关产品推荐

