How to check minimum code coverage for multi-modul

2019-06-20 01:51发布

问题:

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>

回答1:

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