11

I want to use jacoco maven plugin for checking minimum level of code coverage during build process using 'check' goal.

For one-module project everything works fine. But for multi-module I want to check average level of code coverage from all modules, but check goal checks every module separately.

For example, module1 has 70% of code coverage, module2 has 100% code coverage, in average for all lines from both modules code coverage is 85%. And I am trying to set code coverage for all project to 80%, but it fails because of first module.

From pom:

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.7.6.201602180812</version>
    <executions>
        <execution>
            <id>default-prepare-agent</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
        </execution>
        <execution>
            <id>default-report</id>
            <phase>prepare-package</phase>
            <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>COMPLEXITY</counter>
                                <value>COVEREDRATIO</value>
                                <minimum>0.80</minimum>
                            </limit>
                        </limits>
                    </rule>
                </rules>
            </configuration>
        </execution>
    </executions>
</plugin>
A_Di-Matteo
  • 26,902
  • 7
  • 94
  • 128

2 Answers2

5

Short answer: not possible (at the time of writing) using merely Maven and Jacoco.

from official jacoco github page:

The current JaCoCo Maven goals work on single modules only: Tests are executed within the module and contribute coverage only to code within the same module. Coverage reports are created for each module separately. There is no built-in support for cross-module coverage or combined reports for multiple modules.

Hence your requirements cannot be met merely using Maven and Jacoco. You can however use a common approach in enterprise settings: Sonarqube, which will process the generated jacoco files (i.e. jacoco.exec) and aggregate reporting and governance via its Jacoco integration (provided out-of-the-box on its latest versions).

A_Di-Matteo
  • 26,902
  • 7
  • 94
  • 128
  • 1
    Thanks, I implemented such approach via Jacoco plugin for Jenkins – Evgeniy Strepetov Aug 05 '16 at 13:49
  • 2
    Hello, is there any update to this answer in 2019? I am able to use `report-aggregate` to generate a nice html report with aggregates. If that's possible, then jacoco should be able to generate an aggregate `jacoco.exec`, and then I should be able to check the aggregate coverage, no? – user770119 Jan 25 '19 at 01:00
  • 1
    @user770119 - This is still not supported as discussed in https://groups.google.com/d/msg/jacoco/0haFMvGBng0/A4UfH_sjBgAJ – tuk Aug 05 '19 at 14:17
  • It's 2021. Does anyone have any news? *hopeful* – His Jul 29 '21 at 07:02
0

Source: https://www.jacoco.org/jacoco/trunk/doc/check-mojo.html

Use Bundle with counter Intruction, this will check overall code coverage for the whole project:

<rules>
<rule>
    <element>BUNDLE</element>
    <limits>
        <limit>
            <counter>INSTRUCTION</counter>
            <value>COVEREDRATIO</value>
            <minimum>0.80</minimum>
        </limit>
    </limits>
</rule>
tenstormavi
  • 315
  • 1
  • 7
  • 17