Issue
I have some submodules in my application. Some I wish to have code coverage rules and some I wish to exempt completely.
My project's root POM inherits from a parent POM which has JaCoCo configured as so:
<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>
In a submodule, test
, in which I wish to have 50% code coverage I have configured JaCoCo as so:
<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>
This module has absolutely zero code coverage but running mvn clean verify
does not produce any error.
I figured that maybe I had to define it in the root POM, so I did this as:
<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
does produce an error:
[WARNING] Rule violated for package com.project: lines covered ratio is 0.00, but expected minimum is 0.75
The problem is, I want a blanket value of 0.75 for my other submodules but for this module, test
I want a value of 0.5. I found this link that described inheritance and combine.self="override"
. So I added this to the configuration options in 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>
Despite this override, I still get the error complaining it expects 0.75 when I want 0.5:
[WARNING] Rule violated for package com.project: lines covered ratio is 0.00, but expected minimum is 0.75
How can I override this in the submodule?
Solution
My solution to this was to use <pluginManagement>
in the POM in the root of the project, and then manually declare the code coverage in each submodule.
Root 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>
A Submodule POM where I desired 0 code coverage:
<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>
Answered By - Nanor