Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Gradle JaCoCo plug-in


May 25, 2021 Gradle


Table of contents


JaCoCo plug-in

The JaCoCo plug-in is still in hatching status. I t is important to note that DSL and other configurations may change in future Gradle releases.

The JaCoCo plug-in provides code coverage metrics for Java code through integrated JaCoCo.

Entry

To get started, apply the JaCoCo plug-in to the project where you want to calculate code coverage.

Apply the JaCoCo plug-in

build.gradle

apply plugin: "jacoco"  

If the Java plug-in is also applied to your project, a new task named jacocoTestReport is created that depends on the test task. T he report $buildDir in the website/reports/jacoco/test. By default, an HTML report is generated.

Configure the JaCoCo plug-in

The JaCoCo plug-in adds a project extension called JacocoPluginExtension, which allows you to configure the default values used by JaCoCo in your build.

Configure the JaCoCo plug-in settings

build.gradle

jacoco
    toolVersion = "0.6.2.201302030002"
    reportsDir = file("$buildDir/customJacocoReportDir")
}  

Table 34.1. The Gradle default value for the JaCoCo property

Property Gradle default
reportsDir " $buildDir /reports/jacoco"

JaCoCo reports the configuration

The JacocoReport task can be used to generate code coverage reports in different formats. It implements the standard Gradle type Reporting and presents a JacocoReportsContainer type reporting container.

Configure the test task

build.gradle

jacocoTestReport {
    reports
        xml.enabled false
        csv.enabled false
        html.destination "${buildDir}/jacocoHtml"
    }
}  

Gradle JaCoCo plug-in

JaCoCo's specific task configuration

The JaCoCo plug-in adds a JacocoTaskExtension extension to all tasks of the Test type. This extension allows you to configure some specific properties of test tasks in JaCoCo.

Configure the test task

build.gradle

test {
    jacoco
        append = false
        destinationFile = file("$buildDir/jacoco/jacocoTest.exec")
        classDumpFile = file("$buildDir/jacoco/classpathdumps")
    }
}  

Table 34.2. The default value for JaCoCo task extensions

Property Gradle default
enabled true
destPath $buildDir /jacoco
append true
includes []
excludes []
excludeClassLoaders []
sessionId auto-generated
dumpOnExit true
output Output.FILE
address -
port -
classDumpPath -
Jmx false

Although all of Test's tasks are automatically enhanced to provide coverage information when the java plug-in is configured for use, any task that implements JavaForkOptions can be enhanced with the JaCoCo plug-in. This means that any task fork Java process can be used to generate coverage information.

For example, you can configure your build to use the application plug-in to generate code coverage.

Use the application plug-in to generate code coverage data

build.gradle

apply plugin: "application"
apply plugin: "jacoco"
mainClassName = "org.gradle.MyMain"
jacoco {
    applyTo run
}
task applicationCodeCoverageReport(type:JacocoReport){
    executionData run
    sourceSets sourceSets.main
}  

Note: The code in this example can be found in Gradle's binary distribution package and in the samples/testing/jacoco/application in the source code distribution package.

Coverage report generated by applicationCodeCoverageReport

Build the layout

application
  build
    jacoco
      run.exec
    reports/jacoco/applicationCodeCoverageReport/html/
      index.html  

Task

For projects that also have Java plug-ins configured, the JaCoCo plug-in automatically adds the following tasks:

Table 34.3. JaCoCo plug-in - task

The name of the task Depends on Type Describe
jacocoTestReport - JacocoReport Generate a code coverage report for the test task.

Rely on management

The JaCoCo plug-in adds the following dependency configurations:

Table 34.4. JaCoCo plug-ins - Dependent on configuration

Name Significance
jacocoAnt The JacocoMerge Ant library for running Jacoo Merce tasks.
jacocoAgent The JaCoCo client library for testing code under test.