JaCoCo Maven Plugin | Generated by AI

Home PDF

Here’s a step-by-step guide on how to use the JaCoCo Maven plugin to measure code coverage in a Maven-based Java project. JaCoCo (Java Code Coverage) is a tool that integrates with Maven to track how much of your code is exercised by tests, typically unit tests. By adding and configuring the JaCoCo Maven plugin in your project, you can generate detailed coverage reports during the build process.


Steps to Use the JaCoCo Maven Plugin

1. Add the JaCoCo Maven Plugin to Your pom.xml

To use the JaCoCo Maven plugin, you need to include it in the <build><plugins> section of your project’s pom.xml file. Below is a basic configuration that sets up the plugin:

<build>
    <plugins>
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.8.12</version> <!-- Use the latest version available -->
            <executions>
                <execution>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                </execution>
                <execution>
                    <id>report</id>
                    <phase>verify</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

2. Ensure Tests Are Configured

The JaCoCo plugin works by analyzing test execution, typically unit tests run by the Maven Surefire Plugin. In most Maven projects, Surefire is included by default and runs tests located in src/test/java. No additional configuration is needed unless your tests are non-standard. Verify that:

If you need to explicitly configure Surefire, it might look like this:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.5.0</version> <!-- Use the latest version -->
</plugin>

The prepare-agent goal sets up the JaCoCo agent by modifying the argLine property, which Surefire uses to run tests with coverage tracking enabled.

3. Run the Maven Build

To generate the coverage report, execute the following command in your project directory:

mvn verify

Alternatively, if you only want to run tests without proceeding to verify, use:

mvn test

However, since the report goal is bound to verify in this configuration, you’ll need to run mvn verify to see the report. If you prefer the report to generate during mvn test, you can change the <phase> for the report execution to test, though verify is a common convention.

4. View the Coverage Report

After running mvn verify, JaCoCo generates an HTML report by default. You can find it at:

target/site/jacoco/index.html

Optional Customizations

For more advanced use cases, you can tweak the plugin configuration:


Summary

To use the JaCoCo Maven plugin:

  1. Add the plugin to your pom.xml with prepare-agent and report goals.
  2. Ensure your project has tests configured (typically via Surefire).
  3. Run mvn verify to generate the coverage report.
  4. Check the report at target/site/jacoco/index.html.

This setup provides a straightforward way to integrate code coverage into your Maven build process, helping you assess the effectiveness of your tests. For the latest plugin version or advanced configurations, refer to the official JaCoCo documentation.


Back 2025.03.04 Donate