logo JaCoCo :: Maven Plugin

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

homepage: www.jacoco.org/jacoco/trunk/doc/maven.html
fresh index:
last release: 9 months ago, first release: 1 decade ago
packaging: maven-plugin
get this artifact from: central
see this artifact on: search.maven.org


Display vulnerabilities (snyk): Vulnerability check

This chart shows how much is this artifact used as a dependency in other Maven artifacts in Central repository and GitHub:


select version:

Add this snippet into pom.xml inside tag <project><build><plugins>:




Switch to artifact.
goals:
jacoco:check (phase: verify)

Checks that the code coverage metrics are being met.

jacoco:dump (phase: post-integration-test)

Request a dump over TCP/IP from a JaCoCo agent running in tcpserver mode. Note concerning parallel builds: While the dump goal as such is thread safe, it has to be considered that TCP/IP server ports of the agents are a shared resource.

jacoco:help

Display help information on jacoco-maven-plugin. Call mvn jacoco:help -Ddetail=true -Dgoal=<goal-name> to display parameter details.

jacoco:instrument (phase: process-classes)

Performs offline instrumentation. Note that after execution of test you must restore original classes with help of "restore-instrumented-classes" goal. Warning: The preferred way for code coverage analysis with JaCoCo is on-the-fly instrumentation. Offline instrumentation has several drawbacks and should only be used if a specific scenario explicitly requires this mode. Please consult documentation about offline instrumentation before using this mode.

jacoco:merge (phase: generate-resources)

Mojo for merging a set of execution data files (*.exec) into a single file

jacoco:prepare-agent (phase: initialize)

Prepares a property pointing to the JaCoCo runtime agent that can be passed as a VM argument to the application under test. Depending on the project packaging type by default a property with the following name is set: tycho.testArgLine for packaging type eclipse-test-plugin and argLine otherwise. If your project already defines VM arguments for test execution, be sure that they will include property defined by JaCoCo. One of the ways to do this in case of maven-surefire-plugin - is to use syntax for late property evaluation: <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <argLine>@{argLine} -your -extra -arguments</argLine> </configuration> </plugin> You can define empty property to avoid JVM startup error Could not find or load main class @{argLine} when using late property evaluation and jacoco-maven-plugin not executed. Another way is to define "argLine" as a Maven property rather than as part of the configuration of maven-surefire-plugin: <properties> <argLine>-your -extra -arguments</argLine> </properties> ... <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <!-- no argLine here --> </configuration> </plugin> Resulting coverage information is collected during execution and by default written to a file when the process terminates.

jacoco:prepare-agent-integration (phase: pre-integration-test)

Same as prepare-agent, but provides default values suitable for integration-tests: bound to pre-integration-test phase different destFile

jacoco:report (phase: verify)

Creates a code coverage report for tests of a single project in multiple formats (HTML, XML, and CSV).

jacoco:report-aggregate

Creates a structured code coverage report (HTML, XML, and CSV) from multiple projects within reactor. The report is created from all modules this project depends on. From those projects class and source files as well as JaCoCo execution data files will be collected. In addition execution data is collected from the project itself. This also allows to create coverage reports when tests are in separate projects than the code under test, for example in case of integration tests. Using the dependency scope allows to distinguish projects which contribute execution data but should not become part of the report: compile, runtime, provided: Project source and execution data is included in the report. test: Only execution data is considered for the report.

jacoco:report-integration (phase: verify)

Same as report, but provides default values suitable for integration-tests: bound to report-integration phase different dataFile

jacoco:restore-instrumented-classes (phase: prepare-package)

Restores original classes as they were before offline instrumentation.

Often used code snippets:

<plugin>
  <groupId>org.jacoco</groupId>
  <artifactId>jacoco-maven-plugin</artifactId>
  <configuration>
    <excludes>
      <exclude>*</exclude>
    </excludes>
  </configuration>
</plugin>
<plugin>
  <groupId>org.jacoco</groupId>
  <artifactId>jacoco-maven-plugin</artifactId>
  <version>${jacoco.version}</version>
  <executions>
    <execution>
      <id>default-prepare-agent</id>
      <goals>
        <goal>prepare-agent</goal>
      </goals>
    </execution>
    <execution>
      <id>default-prepare-agent-integration</id>
      <goals>
        <goal>prepare-agent-integration</goal>
      </goals>
    </execution>
    <execution>
      <id>default-report</id>
      <goals>
        <goal>report</goal>
      </goals>
    </execution>
    <execution>
      <id>default-report-integration</id>
      <goals>
        <goal>report-integration</goal>
      </goals>
    </execution>
    <execution>
      <id>default-check</id>
      <goals>
        <goal>check</goal>
      </goals>
      <configuration>
        <rules>
          <rule implementation="org.jacoco.maven.RuleConfiguration">
            <element>BUNDLE</element>
            <limits>
              <limit implementation="org.jacoco.report.check.Limit">
                <counter>COMPLEXITY</counter>
                <value>COVEREDRATIO</value>
              </limit>
            </limits>
          </rule>
        </rules>
      </configuration>
    </execution>
  </executions>
</plugin>
<plugin>
  <groupId>org.jacoco</groupId>
  <artifactId>jacoco-maven-plugin</artifactId>
  <version>${jacoco-maven-plugin.version}</version>
  <executions>
    <execution>
      <id>pre-unit-tests</id>
      <goals>
        <goal>prepare-agent</goal>
      </goals>
      <configuration>
        <destFile>${project.testresult.directory}/coverage/jacoco/jacoco.exec</destFile>
      </configuration>
    </execution>
    <execution>
      <id>post-unit-test</id>
      <phase>test</phase>
      <goals>
        <goal>report</goal>
      </goals>
      <configuration>
        <dataFile>${project.testresult.directory}/coverage/jacoco/jacoco.exec</dataFile>
        <outputDirectory>${project.testresult.directory}/coverage/jacoco</outputDirectory>
      </configuration>
    </execution>
  </executions>
</plugin>
<plugin>
  <groupId>org.jacoco</groupId>
  <artifactId>jacoco-maven-plugin</artifactId>
  <configuration>
    <destFile>${basedir}/target/coverage-reports/jacoco-unit.exec</destFile>
  </configuration>
  <executions>
    <execution>
      <id>jacoco-initialize</id>
      <goals>
        <goal>prepare-agent</goal>
      </goals>
    </execution>
    <execution>
      <id>jacoco-site</id>
      <phase>test</phase>
      <goals>
        <goal>report</goal>
      </goals>
      <configuration>
        <dataFile>${basedir}/target/coverage-reports/jacoco-unit.exec</dataFile>
        <outputDirectory>${basedir}/target/coverage-reports/site</outputDirectory>
      </configuration>
    </execution>
  </executions>
</plugin>
<plugin>
  <groupId>org.jacoco</groupId>
  <artifactId>jacoco-maven-plugin</artifactId>
  <version>${jacoco.version}</version>
  <executions>
    <execution>
      <id>default-instrument</id>
      <goals>
        <goal>instrument</goal>
      </goals>
    </execution>
    <execution>
      <id>default-restore-instrumented-classes</id>
      <goals>
        <goal>restore-instrumented-classes</goal>
      </goals>
    </execution>
    <execution>
      <id>default-report</id>
      <phase>prepare-package</phase>
      <goals>
        <goal>report</goal>
      </goals>
    </execution>
    <execution>
      <id>default-report-integration</id>
      <goals>
        <goal>report-integration</goal>
      </goals>
    </execution>
    <execution>
      <id>default-check</id>
      <goals>
        <goal>check</goal>
      </goals>
      <configuration>
        <rules>
          <rule implementation="org.jacoco.maven.RuleConfiguration">
            <element>BUNDLE</element>
            <limits>
              <limit implementation="org.jacoco.report.check.Limit">
                <counter>COMPLEXITY</counter>
                <value>COVEREDRATIO</value>
              </limit>
            </limits>
          </rule>
        </rules>
      </configuration>
    </execution>
  </executions>
</plugin>

© Jiri Pinkas 2015 - 2022. All rights reserved! Admin login To submit bugs / feature requests please use this github page
related: JavaVids | Top Java Blogs | Java školení
Apache and Apache Maven are trademarks of the Apache Software Foundation. The Central Repository is a service mark of Sonatype, Inc.