Jacoco coverage in Jenkins Pipeline

The jacoco pipeline step configuration uses this format:

step([$class: 'JacocoPublisher', 
      execPattern: 'target/*.exec',
      classPattern: 'target/classes',
      sourcePattern: 'src/main/java',
      exclusionPattern: 'src/test*'
])

Or with a simpler syntax for declarative pipeline:

jacoco( 
      execPattern: 'target/*.exec',
      classPattern: 'target/classes',
      sourcePattern: 'src/main/java',
      exclusionPattern: 'src/test*'
)

You can find more options in the JaCoCo Pipeline Steps Reference


After trying to scour the internet for a simple example of how to do this, I eventually found the "step" tool within our Jenkins instance.

It knows how to generate snippets of Jenkinsfile pipeline code based on the plugins and modules you have installed.

The long and short of it is that the basic entry looks like:

stage('Build') {
     steps {
        sh './jenkins_build.sh'
        junit '*/build/test-results/*.xml'
        step( [ $class: 'JacocoPublisher' ] )
     }
}

The jenkins documentation really needs an update with some one-liner examples.

Example from Jenkins 2.32.x


As of the Jacoco plugin 2.2.1, you can now use jacoco(execPattern: 'target/jacoco.exec')

I personally have a couple of different Jacoco files for different executions and wanted to support both Maven and Gradle (so build/ and target/ directories), so I use jacoco(execPattern: '**/*.exec').

Reference: https://github.com/jenkinsci/jacoco-plugin/pull/83