Operations 5 min read

Fixing JaCoCo Class Path Errors: The Correct Ant build.xml Configuration

The author explains why JaCoCo coverage failed after changing file paths, reveals that class file paths are flexible but source paths must point to the package root (usually src/main/java), and provides a complete, working Ant build.xml example to generate accurate coverage reports.

FunTester
FunTester
FunTester
Fixing JaCoCo Class Path Errors: The Correct Ant build.xml Configuration

The author encountered a problem when using JaCoCo to measure test coverage: after initially succeeding with the documentation‑provided build.xml, modifying the file paths to separate projects caused the coverage generation to fail.

Investigation showed that JaCoCo does not strictly require a particular class‑file path and the JVM include parameter is also tolerant, but the source‑file path must be set to the directory one level above the package root—typically the src/main/java folder. This mismatch was the root cause of the failure.

Below is a complete Ant build.xml that works for the described scenario. It defines properties for the project name, JaCoCo JAR location, report folder, server IP, class‑file directory, and source‑file directory, then configures the necessary Ant tasks:

<?xml version="1.0" ?>
<project name="user-center" basedir="/home/jmsmanager/report/user-center"
    xmlns:jacoco="antlib:org.jacoco.ant"
    xmlns:sonar="antlib:org.sonar.ant" default="all">
    <!-- Project name -->
    <property name="projectName" value="user-center"/>
    <!-- JaCoCo installation path -->
    <property name="jacocoantPath" value="/home/jmsmanager/jacoco/lib/jacocoant.jar"/>
    <!-- Report output folder -->
    <property name="reportfolderPath" value="${basedir}/report/"/>
    <!-- Remote service IP (adjust if multiple) -->
    <property name="server_ip" value="127.0.0.1"/>

    <!-- Class files to be measured -->
    <property name="waterommpClasspath" value="/home/jmsmanager/report/${projectName}/source/BOOT-INF/classes/com/noriental/center/moudle/"/>

    <!-- Source files (must point to package root) -->
    <property name="mcmSrcpath" value="${basedir}/source/src/main/java"/>
    <taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml">
        <classpath path="${jacocoantPath}"/>
    </taskdef>

    <!-- Merge multiple .exec files before reporting -->
    <target name="merge" depends="dump">
        <jacoco:merge destfile="jacoco.exec">
            <fileset dir="${basedir}" includes="*.exec"/>
        </jacoco:merge>
    </target>

    <!-- Dump coverage data from the running JVM -->
    <target name="dump">
        <jacoco:dump address="${server_ip}" reset="false" destfile="${basedir}/jacoco.exec" port="12347" append="true"/>
    </target>

    <!-- Generate HTML, CSV and XML reports -->
    <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="**/request/*.class"/>
                            <exclude name="**/response/*.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>
</project>

By copying this file and adjusting the path values to match their own project layout, developers can run the appropriate Ant targets (e.g., ant report) to produce a detailed JaCoCo coverage report in HTML, CSV, and XML formats.

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 coverageBuild AutomationtestingJaCoCoAnt
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.