Proper Configuration for Excluding Classes and Packages in Maven Jacoco

Nov 22, 2025 · Programming · 15 views · 7.8

Keywords: Maven | Jacoco | Code Coverage

Abstract: This article provides an in-depth analysis of correctly configuring exclusion rules in Maven multi-module projects using Jacoco for code coverage testing. It addresses common configuration errors, offers proper XML configuration examples with wildcard usage guidelines, and explains the application of exclusion rules on compiled class file paths. The discussion extends to additional configuration requirements when integrating with SonarQube, helping developers obtain accurate code coverage reports.

Problem Background and Common Errors

In Maven multi-module projects utilizing Jacoco for code coverage testing, developers often need to exclude specific classes or packages, such as Spring configuration classes or development-specific classes. However, many developers encounter issues where excluded classes still appear in coverage reports despite following documentation guidelines.

Correct Configuration Approach

The core issue lies in incorrect XML configuration structure. Proper configuration requires nesting exclusion rules within a parent <excludes> element, rather than using multiple <exclude> elements directly under <configuration>. Below is a correct configuration example:

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

Wildcard Usage Guidelines

Jacoco employs standard wildcard syntax for matching class paths:

Exclusion rules should be based on compiled class file paths, relative to the target/classes/ directory. For instance, to exclude an entire package and all its subpackages:

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

This excludes all classes in the some.package package, including those in subpackages like some.package.child.

Practical Verification and Effects

After applying correct configuration, executing the mvn clean verify command demonstrates exclusion effectiveness in console output:

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

In contrast, without proper exclusion configuration, reports include more classes:

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

Integration Considerations with SonarQube

Even with correct Jacoco exclusions, projects using SonarQube for code quality analysis may require additional exclusion configuration in SonarQube settings:

Settings > General Settings > Exclusions > Code Coverage

This ensures excluded classes do not appear in SonarQube coverage reports, maintaining report consistency.

Common Issue Troubleshooting

Based on reference article experiences, certain Jacoco versions may have compatibility issues. If exclusion rules are not taking effect, consider:

By adhering to these best practices, developers can effectively configure Jacoco exclusion rules to obtain accurate and meaningful code coverage reports.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.