Fundamentals 4 min read

How to Generate JaCoCo Coverage Reports with Ant: Step‑by‑Step Guide

This guide walks you through configuring JaCoCo agent parameters, running Ant tasks to produce a jacoco.exec file, and generating detailed HTML coverage reports, including class and line‑level insights, with practical command examples and visual explanations.

JavaEdge
JavaEdge
JavaEdge
How to Generate JaCoCo Coverage Reports with Ant: Step‑by‑Step Guide

Introduction

The article explains how to use JaCoCo, an open‑source code coverage tool that records execution traces via instrumentation, together with Ant, a Java build tool that requires an XML build file.

Coverage Tool Comparison

An image (not reproduced here) compares JaCoCo with other coverage tools.

Process Overview

Configure JaCoCo agent parameters and start the service.

Generate the jacoco.exec file.

Use Ant to build and produce the coverage report.

Starting the JaCoCo Agent

The Java agent is introduced in JDK 1.5 and can be specified with the -javaagent option. The following command starts the JaCoCo agent with common parameters:

java -javaagent:/fs/jacocoagent.jar=includes=*,output=tcpserver,port=8888,address=127.0.0.1

includes : List of class names to include; * means all.

output : Uses tcpserver to listen on the specified address and port, writing execution data to the TCP connection for real‑time report generation.

port : Port number for the TCP server (e.g., 8888).

address : IP address to bind (use 127.0.0.1 for local).

Generating the Report

Run the following Ant targets: ant dump – creates the jacoco.exec file. ant report – generates the HTML coverage report.

Images in the original article show the generated jacoco.exec file and the resulting HTML report.

Viewing Detailed Coverage

Open the generated index.html to see class‑level coverage. To view line‑level coverage, you must configure the paths to your compiled .class files and source code directories (the parent directory of the com package). This allows the report to map coverage data back to the original source lines.

Green rows indicate fully covered lines.

Red rows indicate uncovered lines.

Red diamonds show branches with no coverage.

Yellow diamonds indicate partially covered branches.

Green diamonds represent fully covered branches.

Conclusion

The tutorial covers the essential steps to set up JaCoCo with Ant, generate execution data, and produce a comprehensive HTML report, providing a solid foundation for further advanced customization and optimization.

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 coveragetestingJaCoCoAnt
JavaEdge
Written by

JavaEdge

First‑line development experience at multiple leading tech firms; now a software architect at a Shanghai state‑owned enterprise and founder of Programming Yanxuan. Nearly 300k followers online; expertise in distributed system design, AIGC application development, and quantitative finance investing.

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.