Latest
Sonar 1 min read

Import Java Test Coverage into SonarQube with JaCoCo

Import Java Test Coverage into SonarQube with JaCoCo
Sonar · September 27, 2026

Version note: references describe SonarQube Server 2025.2 behaviour. Check your server, scanner and edition requirements. These examples were not run against a live SonarQube server.

What this guide covers

JaCoCo records which Java code runs during tests. SonarQube reads its XML report after the build and tests finish. The scanner does not create the report for you.

Before you start

Use a Maven Java project with a working build, the SonarScanner for Maven, and a configured JaCoCo Maven plugin. The coverage profile below must exist in your pom.xml.

Step 1: Configure the JaCoCo plugin

Inside the JaCoCo plugin configuration, add executions that attach the test agent and write a report during Maven’s verify phase:

<executions>
  <execution>
    <id>attach-coverage-agent</id>
    <goals><goal>prepare-agent</goal></goals>
  </execution>
  <execution>
    <id>write-coverage-report</id>
    <phase>verify</phase>
    <goals><goal>report</goal></goals>
  </execution>
</executions>

This is an XML fragment, not a complete pom.xml. Specify a compatible JaCoCo plugin version. If your test plugins set argLine, preserve the JaCoCo agent arguments.

Step 2: Build and generate the report

Run:

mvn -B clean verify -Pcoverage
test -s target/site/jacoco/jacoco.xml

Fix test failures or a missing report before scanning. verify runs the earlier Maven build phases as well.

Step 3: Run SonarQube analysis

Set the server URL and token securely, then run:

mvn -B sonar:sonar -Pcoverage \
  -Dsonar.coverage.jacoco.xmlReportPaths=target/site/jacoco/jacoco.xml

Run this against the same checkout and compiled classes. Set the project key in your Maven or scanner configuration.

Step 4: Check multi-module projects separately

The path above is for one module. Multi-module builds may need an aggregate report and paths visible to each analyzed module. Follow JaCoCo’s report-aggregate documentation rather than copying the single-module path everywhere.

Check the result

Check the scanner log for the XML import, then compare one Java file’s coverage in SonarQube with the JaCoCo report. A report from an older build is not valid evidence for the current code.

References

PB
Written by

Parul Bedi

Notes and guides by Parul Bedi.

Up next

Import Python Test Coverage into SonarQube

Sonar September 30, 2026 1 min read
Open post

Conversation

0 comments

No comments yet. Start the conversation below.

Leave a comment

Replying to a comment.
Be kind. Markdown is not supported in comments.