Integrating JaCoCo Code Coverage into Jenkins‑Driven API Testing Across Multiple Servers
This guide explains how to add JaCoCo to a Tomcat service, use Ant build.xml to collect and merge execution data from remote JVMs, generate HTML coverage reports, and centralize the results for API testing in a multi‑server Jenkins environment.
To quantify the effectiveness of API test cases, the author introduces code coverage as a key metric and presents a practical solution that works across three separate machines: the server running the JVM, the Jenkins build server, and the server hosting the test scripts.
The approach starts by modifying the Tomcat container's startup script to include a JaCoCo -javaagent parameter, then launching the service. An Ant build.xml file reads the generated .exec data, merges it if necessary, and produces an HTML coverage report that can be viewed via a web service.
The required JaCoCo configuration is added to the JVM launch parameters:
-javaagent:/home/jmsmanager/jacoco/lib/jacocoagent.jar=includes=com.noriental.*,output=tcpserver,address=127.0.0.1,port=12345Key options:
includes : package pattern to instrument.
output : type of output, default tcpserver.
address : IP of the service (example uses 127.0.0.1).
port : chosen port for the JaCoCo server.
The Ant build file defines several properties (project name, JaCoCo paths, report folder, server IP, classpath, source path) and 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">
<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>The build defines three main targets: merge (combines multiple .exec files), dump (connects to the JaCoCo server to retrieve coverage data), and report (generates HTML, CSV, and XML reports after cleaning the output directory).
Because the service runs on multiple machines, the generated reports are centralized on a designated server for convenient web access.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
