尝试在子模块上设置jacoco-check代码覆盖率



我的应用程序中有一些子模块。有些我希望有代码覆盖规则,有些我希望完全免除。

我的项目的根POM继承自父POM,父POM将JaCoCo配置为:

<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco-maven-plugin.version}</version>
<configuration>
<fileSets>
<fileSet>
<directory>domain/target</directory>
<includes>
<include>*.exec</include>
</includes>
</fileSet>
<fileSet>
<directory>integration/target</directory>
<includes>
<include>*.exec</include>
</includes>
</fileSet>
</fileSets>
<destFile>target/jacoco.exec</destFile>
</configuration>
</plugin>

在子模块test中,我希望具有50%的代码覆盖率,我已将JaCoCo配置为:

<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<executions>
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<excludes>
<exclude>**/*</exclude>
</excludes>
</configuration>
</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 clean verify不会产生任何错误。

我想也许我必须在根POM中定义它,所以我这样做:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<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.75</minimum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>
</executions>
</plugin>

mvn clean verify产生错误:

[WARNING] package com违反规则。项目:线覆盖率为0.00,但期望最低为0.75

问题是,我想要0.75的其他子模块的毯子值,但对于这个模块,test我想要0.5的值。我发现了这个描述继承和combine.self="override"的链接。因此,我将此添加到test中的配置选项:

<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>

尽管有这个覆盖,我仍然得到错误抱怨它期望0.75,而我想要0.5:

[WARNING] package com违反规则。项目:线覆盖率为0.00,但期望最低为0.75

我如何在子模块中覆盖此?

我的解决方案是在项目根目录的POM中使用<pluginManagement>,然后手动声明每个子模块的代码覆盖率。

根POM:

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>

我想要0代码覆盖率的子模块POM:

<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<executions>
<execution>
<id>test</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<rules>
<rule>
<element>BUNDLE</element>
<limits>
<limit>
<counter>LINE</counter>
<value>COVEREDRATIO</value>
<minimum>0</minimum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>
</executions>
</plugin>

最新更新