How to Filter Non‑Business Classes from JaCoCo Coverage Reports in Java Projects

This article explains why JaCoCo coverage reports become noisy due to config, bean, and adapter classes, compares two filtering approaches, and provides a step‑by‑step Ant build configuration with XML examples to exclude irrelevant classes for more accurate coverage metrics.

FunTester
FunTester
FunTester
How to Filter Non‑Business Classes from JaCoCo Coverage Reports in Java Projects

When using JaCoCo to measure interface test coverage in Java projects, the generated report often includes a large amount of configuration, bean, and adapter classes that contain little or no business logic, causing the overall coverage percentage to be misleading.

Two main ways to eliminate these irrelevant classes are discussed:

Configure JaCoCo to filter out unwanted classes directly in the build configuration.

Manually delete the compiled .class files of the unnecessary modules before generating the report.

The second method is considered irreversible, requires custom scripting, and does not integrate well with existing reporting frameworks, so the article recommends the first approach.

The recommended solution modifies the Ant build.xml file. Key properties such as projectName, jacocoantPath, reportfolderPath, and classpath locations are defined, followed by JaCoCo tasks for merging, dumping, and reporting. The crucial part is the <exclude> element inside the <classfiles> section, which filters out packages (e.g., **/vo/*.class) that should not affect coverage.

<?xml version="1.0" ?>
<project name="studentpad-middle-toc" basedir="/home/jmsmanager/report/studentpad-middle-toc"
    xmlns:jacoco="antlib:org.jacoco.ant" xmlns:sonar="antlib:org.sonar.ant" default="all">
    <property name="projectName" value="studentpad-middle-toc"/>
    <property name="jacocoantPath" value="/home/jmsmanager/jacoco/lib/jacocoant.jar"/>
    <property name="reportfolderPath" value="${basedir}/report/"/>
    <property name="server_ip" value="127.0.0.1"/>
    <property name="waterommpClasspath" value="/xdfapp/${projectName}/webapps/ROOT/WEB-INF/classes/com/noriental/moudle"/>
    <property name="mcmSrcpath" value="${basedir}/source/${projectName}/workspace/src/main/java"/>
    <taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml">
        <classpath path="${jacocoantPath}"/>
    </taskdef>
    <target name="merge" depends="dump">
        <jacoco:merge destfile="jacoco.exec">
            <fileset dir="${basedir}" includes="*.exec"/>
        </jacoco:merge>
    </target>
    <target name="dump">
        <jacoco:dump address="${server_ip}" reset="true" destfile="${basedir}/jacoco.exec" port="12345" append="false"/>
    </target>
    <target name="report">
        <delete dir="${reportfolderPath}"/>
        <mkdir dir="${reportfolderPath}"/>
        <jacoco:report>
            <executiondata>
                <file file="${basedir}/jacoco.exec"/>
            </executiondata>
            <structure name="JaCoCo Report">
                <group name="学生中间层代码覆盖率">
                    <classfiles>
                        <fileset dir="${waterommpClasspath}">
                            <exclude name="**/vo/*.class"/>
                        </fileset>
                    </classfiles>
                    <sourcefiles encoding="UTF-8">
                        <fileset dir="${mcmSrcpath}"/>
                    </sourcefiles>
                </group>
            </structure>
            <html destdir="${reportfolderPath}" encoding="utf-8"/>
            <csv destfile="${reportfolderPath}/report.csv"/>
            <xml destfile="${reportfolderPath}/report.xml"/>
        </jacoco:report>
    </target>
    <target name="all"/>
</project>

By placing the appropriate <exclude> patterns, developers can prevent configuration, bean, and adapter classes from being counted, resulting in a cleaner and more accurate coverage report. Large blocks of classes can also be filtered by specifying the class file path directly.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Javacode coverageXMLJaCoCofilteringAnt
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.