Jacoco coverage in Jenkins Pipeline

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


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