How to Correctly Configure JaCoCo in Maven for Java Code Coverage Reporting

Question

How can I properly configure JaCoCo with Maven to generate a code coverage report for my Java project?

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.8.7</version>
    <configuration>
        <outputDirectory>${project.build.directory}/jacoco-report</outputDirectory>
        <reportFormat>html</reportFormat>
    </configuration>
    <executions>
        <execution>
            <id>prepare-agent</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
        </execution>
        <execution>
            <id>generate-report</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>report</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Answer

JaCoCo is a widely used code coverage library for Java applications. When using it with Maven, ensuring the correct configuration in the `pom.xml` is crucial for generating the desired coverage reports. If you're facing issues with reports not being generated, this guide provides an overview of necessary steps and common pitfalls.

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.8.7</version>
    <executions>
        <execution>
            <id>prepare-agent</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
        </execution>
        <execution>
            <id>generate-report</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>report</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Causes

  • Outdated JaCoCo version: Using older versions like 0.5.10 can lead to compatibility issues with JDK 1.8.
  • Incorrect execution phase: If the report generation goal is not bound to the appropriate build phase, it may not execute properly.
  • Missing dependencies: Ensure that all necessary dependencies for testing are included in the pom.xml.

Solutions

  • Update JaCoCo to the latest version (currently 0.8.7) to ensure compatibility and access to the latest features.
  • Set the execution phase of the report goal to 'prepare-package' to ensure it runs after tests complete but before packaging.
  • Ensure your Java tests are executing successfully to generate coverage data needed for reports. Add sufficient test cases that cover your code.

Common Mistakes

Mistake: Not running tests before generating the report. If the tests haven't run, there's no data to report on.

Solution: Always run `mvn test` before `mvn jacoco:report`.

Mistake: Using outdated JaCoCo version that isn't compatible with the Java version used.

Solution: Upgrade to a recent version of JaCoCo, such as 0.8.7, that supports Java 1.8.

Mistake: Failing to include the JaCoCo configuration in the project's pom.xml.

Solution: Ensure the JaCoCo plugin is correctly added and configured in your pom.xml.

Helpers

  • JaCoCo Maven
  • Java code coverage
  • generate JaCoCo report
  • Maven configuration for JaCoCo
  • JaCoCo troubleshooting

Related Questions

⦿How to Fix IllegalMonitorStateException When Notifying Adapters in onPostExecute

Learn how to resolve java.lang.IllegalMonitorStateException when notifying adapters in onPostExecute in Android applications.

⦿How to Exclude the First Element of a Stream in Java 8

Learn elegant methods to remove the first element from a Java 8 Stream using Files.walk and other techniques.

⦿How to Resolve the 'Large Objects may not be used in auto-commit mode' Exception in Hibernate with PostgreSQL

Learn how to fix the Large Objects may not be used in autocommit mode exception in Hibernate when dealing with large objects in PostgreSQL.

⦿How to Convert Relative Paths to Absolute Paths in Programming

Learn how to effectively convert relative paths to absolute paths using a structured approach. Discover tips code examples and common mistakes.

⦿How to List All Classes Loaded by a Specific Class Loader in Java

Learn how to list all classes loaded by a specific class loader in Java for debugging. Follow our expert tips and code snippets for a clear approach.

⦿How to Sort Strings in Case-Sensitive Alphabetical Order in Java

Learn how to sort strings in Java with case sensitivity ensuring accurate alphabetical order with examples and troubleshooting tips.

⦿How to Run a Tomcat Server on Multiple Ports Simultaneously

Learn how to configure Apache Tomcat to listen on multiple ports for HTTP requests allowing access on different port numbers concurrently.

⦿Understanding the Differences Between @Qualifier and @Resource Annotations in Spring

Learn the key differences between Qualifier and Resource annotations in Spring for dependency injection.

⦿Why Does `mvn clean install -DskipTests` Still Run Tests with Surefire Plugin?

Learn why Maven carries out tests despite the DskipTests option and how Surefire plugin configurations might affect this behavior.

⦿How to Resolve JSchException: Algorithm Negotiation Fail When Connecting to SFTP Server

Learn how to fix the JSchException Algorithm negotiation fail error when connecting to an SFTP server using JSch.

© Copyright 2025 - CodingTechRoom.com