javamavenjacocojacoco-maven-plugin

Is it possible to fail "mvn package" for not meeting JaCoCo code coverage requirements?


I have included the JaCoCo maven plugin in my project's POM

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.8.7</version>
    <executions>
        <execution>
            <id>default-prepare-agent</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
        </execution>
        <execution>
            <id>default-report</id>
            <goals>
                <goal>report</goal>
            </goals>
        </execution>
        <execution>
            <id>default-check</id>
            <goals>
                <goal>check</goal>
            </goals>
            <configuration>
                <rules>
                    <rule>
                        <element>BUNDLE</element>
                        <limits>
                            <limit>
                                <counter>LINE</counter>
                                <value>COVEREDRATIO</value>
                                <minimum>0.70</minimum>
                            </limit>
                        </limits>
                    </rule>
                </rules>
            </configuration>
        </execution>
    </executions>
</plugin>

I noticed that only the following commands fail when the code coverage requirements is not met:

Is it possible to configure the JaCoCo plugin to fail the mvn package command as well?


Solution

  • You can configure your check goal to run on the prepare-package phase. prepare-package phase runs just before the package goal. This way, your build will fail if the coverage rules are not met.

    Here is how your pom.xml will change.

    <execution>
        <id>default-check</id>
        <goals>
            <goal>check</goal>
        </goals>
        <phase>prepare-package</phase> <!-- Fail just before package phase is executed -->
        <configuration>
            <rules>
                <!-- Rule configuration here -->
            </rules>
        </configuration>
    </execution>
    

    Checkout the documentation for Maven Lifecycle Goals for phases and the order in which they are executed.