0

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:

  • mvn verify
  • mvn install
  • mvn deploy

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

c_anirudh
  • 375
  • 1
  • 22
  • 1
    During which phase does jacoco run? If it runs during any phase before package - it will do what you want, otherwise it just won't run so won't fail :) – Mark Bramnik Feb 23 '22 at 10:28
  • That helped, thank you for making me think in the right direction ^_^. I added `package` to the `check` execution which makes JaCoCo run during the package phase. – c_anirudh Feb 23 '22 at 10:38
  • An after thought, it will probably be better to make it run in the `test` phase. – c_anirudh Feb 23 '22 at 10:41

1 Answers1

2

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.

Code Journal
  • 161
  • 4