• Home
  • LLMs
  • Python
  • Docker
  • Kubernetes
  • Java
  • Maven
  • All
  • About
Maven | JaCoCo: Java Code Coverage Library
  1. JaCoCo Maven Plugin
  2. The help goal
  3. Example

  1. JaCoCo Maven Plugin
    The JaCoCo Maven Plugin provides the JaCoCo runtime agent to your tests and allows basic report creation.

    Plugin coordinates:
    • Group Id: org.jacoco
    • Artifact Id: jacoco-maven-plugin
    • Version: 0.8.13

    Goal Prefix: jacoco

    The jacoco plugin has the following goals:
    • help: Displays help information for the jacoco-maven-plugin.

    • prepare-agent: Prepares a property pointing to the JaCoCo runtime agent, which can be passed as a VM argument to the application under test. This goal should be executed before running unit tests.

    • prepare-agent-integration: Same as prepare-agent, but provides default values suitable for integration tests. Typically bound to the pre-integration-test phase.

    • report: Creates a code coverage report for tests of a single project in multiple formats (HTML, XML, and CSV). Requires execution data from previous test runs.

    • report-aggregate: Creates a structured code coverage report (HTML, XML, and CSV) from multiple projects within the reactor. Useful for multi-module Maven projects.

    • report-integration: Same as report, but provides default values suitable for integration tests. Uses integration test execution data.

    • check: Verifies that the code coverage metrics are being met. Can be configured with rules to enforce minimum coverage thresholds and fail the build if they are not met.

    • instrument: Performs offline instrumentation, serving as an alternative to the Java agent approach for environments where agents cannot be used.

    • restore-instrumented-classes: Restores original classes to their state before offline instrumentation. Should be used in conjunction with the instrument goal.

    • merge: Merges a set of execution data files (*.exec) into a single file. Useful when you have multiple test execution data files to combine.

    • dump: Requests a dump over TCP/IP from a JaCoCo agent running in tcpserver mode. Used for collecting coverage data from remote applications.
  2. The help goal
    The help goal provides general information about the jacoco plugin, including available goals and their parameters.

    Use the detail parameter to get detailed information about all goals and their configuration options:
    # Display basic plugin information
    $ mvn jacoco:help
    
    # Display detailed information about all goals and parameters
    $ mvn jacoco:help -Ddetail=true
    If you need to use the plugin's full coordinates instead of its prefix, you can do that as follows:
    # Display basic plugin information
    $ mvn org.jacoco:jacoco-maven-plugin:0.8.13:help
    
    # Display detailed information about all goals and parameters
    $ mvn org.jacoco:jacoco-maven-plugin:0.8.13:help -Ddetail=true
    You can also use the Maven help plugin to describe the jacoco plugin:
    # Basic plugin description using help plugin
    $ mvn help:describe -Dplugin="org.jacoco:jacoco-maven-plugin:0.8.13"
    
    # Detailed plugin description with all parameters
    $ mvn help:describe -Dplugin="org.jacoco:jacoco-maven-plugin:0.8.13" -Ddetail=true
    To get specific information about a goal, use the goal parameter:
    # Help for the 'report' goal
    $ mvn org.jacoco:jacoco-maven-plugin:0.8.13:help -Dgoal=report -Ddetail=true
    
    # Help for the 'report' goal using the help plugin
    $ mvn help:describe -Dplugin="org.jacoco:jacoco-maven-plugin:0.8.13" -Dgoal=report -Ddetail=true
    Note: When using the plugin's help goal directly, you don't need to specify the version if the plugin is already configured in your POM or if you want to use the latest version.
  3. Example
    Add the following plugin configuration to your pom.xml file:
    <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>0.8.13</version>
    
        <configuration>
            <append>true</append>
        </configuration>
    
        <executions>
            <execution>
                <id>jacoco-prepare-agent</id>
                <phase>initialize</phase>
                <goals>
                    <goal>prepare-agent</goal>
                </goals>
            </execution>
    
            <execution>
                <id>jacoco-report</id>
                <phase>verify</phase>
                <goals>
                    <goal>report</goal>
                </goals>
            </execution>
    
            <!-- Optional: Add coverage check -->
            <execution>
                <id>jacoco-check</id>
                <phase>verify</phase>
                <goals>
                    <goal>check</goal>
                </goals>
                <configuration>
                    <rules>
                        <rule>
                            <element>BUNDLE</element>
                            <limits>
                                <limit>
                                    <counter>LINE</counter>
                                    <value>COVEREDRATIO</value>
                                    <minimum>0.80</minimum>
                                </limit>
                            </limits>
                        </rule>
                    </rules>
                </configuration>
            </execution>
        </executions>
    </plugin>
    Run the command "mvn clean verify" to execute tests and create the code coverage report.

    The main HTML page of the report can be found at: target/site/jacoco/index.html

    Note: Make sure you have unit tests in your project, as JaCoCo requires test execution to generate meaningful coverage data. The plugin works with popular testing frameworks like JUnit and TestNG.
© 2025  mtitek