Search code examples
mavenconfigurationjacocojacoco-maven-plugin

Maven Jacoco Configuration - Exclude classes/packages from report not working


I have a maven multi-module project and I'm using jacoco-maven for code coverage reports. Some classes should not be reported, as they're Spring configuration and I'm not interested in them.

I have declared the maven-jacoco plugin as follow:

<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.2.201409121644</version>
<configuration>
    <outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory>
    <exclude>some.package.*</exclude>
    <exclude>**/*Config.*</exclude>
    <exclude>**/*Dev.*</exclude>
    <exclude>some/package/SomeClass.java</exclude>
</configuration>
<executions>
    <execution>
        <goals>
            <goal>prepare-agent</goal>
        </goals>
    </execution>
    <execution>
        <id>report</id>
        <phase>prepare-package</phase>
        <goals>
            <goal>report</goal>
        </goals>
    </execution>
    <execution>
        <id>post-unit-test</id>
        <phase>test</phase>
        <goals>
            <goal>report</goal>
        </goals>
    </execution>
</executions>
</plugin>

The problem is that when I execute mvn clean verify jacoco still reports classes that should have been excluded as my xml configuration points out. How can I configure it properly?


Solution

  • Your XML is slightly wrong, you need to add any class exclusions within an excludes parent field, so your above configuration should look like the following as per the Jacoco docs

    <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>0.8.8</version>
        <configuration>
            <excludes>
                <exclude>**/*Config.*</exclude>
                <exclude>**/*Dev.*</exclude>
            </excludes>
        </configuration>
        <executions>
            <execution>
                <id>jacoco-report</id>
                <goals>
                    <goal>report</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    

    The values of the exclude fields should be class paths (not package names) of the compiled classes relative to the directory target/classes/ using the standard wildcard syntax

    *   Match zero or more characters
    **  Match zero or more directories
    ?   Match a single character
    

    You may also exclude a package and all of its children/subpackages this way:

    <exclude>some/package/**/*</exclude>
    

    This will exclude every class in some.package, as well as any children. For example, some.package.child wouldn't be included in the reports either.

    I have tested and my report goal reports on a reduced number of classes using the above.

    If you are then pushing this report into Sonar, you will then need to tell Sonar to exclude these classes in the display which can be done in the Sonar settings

    Settings > General Settings > Exclusions > Code Coverage

    Sonar Docs explains it a bit more

    Running your command above

    mvn clean verify
    

    Will show the classes have been excluded

    No exclusions

    [INFO] --- jacoco-maven-plugin:0.7.4.201502262128:report (post-test) @ ** ---
    [INFO] Analyzed bundle '**' with 37 classes
    

    With exclusions

    [INFO] --- jacoco-maven-plugin:0.7.4.201502262128:report (post-test) @ ** ---
    [INFO] Analyzed bundle '**' with 34 classes