Operations 6 min read

Integrating JaCoCo Code Coverage into Jenkins CI for Java Services

This article explains how to configure JaCoCo in Java service startup parameters, use Ant build.xml to collect execution data, generate HTML coverage reports, and centralize the results on a dedicated server within a multi‑machine Jenkins CI environment.

FunTester
FunTester
FunTester
Integrating JaCoCo Code Coverage into Jenkins CI for Java Services

During interface testing, code coverage was introduced as a key metric to quantify test case effectiveness. The solution involves configuring JaCoCo on the Java service, collecting execution data via Ant, and generating HTML reports that can be viewed through a web service.

The setup spans three servers: the JVM host, the Jenkins build server, and the server where test scripts run. Jenkins machines are designated for the service and the test project respectively.

First, the JaCoCo agent is added to the Tomcat container's startup script using the -javaagent:/home/jmsmanager/jacoco/lib/jacocoagent.jar=includes=com.noriental.*,output=tcpserver,address=127.0.0.1,port=12345 option, specifying the included packages, output type, IP address, and port.

Next, an Ant build.xml file is created to handle merging, dumping, and reporting of coverage data. The key tasks are:

merge : combines multiple .exec files into a single jacoco.exec before reporting.

dump : contacts the JaCoCo agent at the configured IP and port to retrieve execution data, optionally appending to existing data.

report : deletes previous reports, creates the output directory, and generates HTML, CSV, and XML reports using the collected .exec file, class files, and source files.

<?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 properties -->
    <property name="projectName" value="user-center"/>
    <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="/home/jmsmanager/report/${projectName}/source/BOOT-INF/classes/com/noriental/center/moudle/"/>
    <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>
    <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="false" destfile="${basedir}/jacoco.exec" port="12347" append="true"/>
    </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="**/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>

After the report is generated on the service machine, it is centralized on a designated server for unified viewing, simplifying access across the multiple machines involved.

Readers are invited to view the original article and join the discussion.

Javacode coverageCI/CDJaCoCoJenkinsAnt
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

0 followers
Reader feedback

How this landed with the community

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