49
votes

Can anyone suggest if there is a way to execute Jacoco in a Jenkins Pipeline? I have downloaded the plugin but I do not get the option for Jacoco in the 'Pipeline Syntax', which is the Pipeline script help .

Referred this URL: https://wiki.jenkins-ci.org/display/JENKINS/JaCoCo+Plugin which has no information for a jenkins jacoco pipeline

5
If your build tool is Gradle, you can do like this: sh "./gradlew jacocoTestReport" publishHTML(target: [reportDir:'build/jacocoHtml', reportFiles: 'index.html', reportName: 'Code Coverage']) - Jacob Aae Mikkelsen
Not sure why the question is under rated. Would like to know what could be a better way to ask the question. I know how to configure the Jacoco Jenkins plugin, but could not get any information about how to do it in a pipeline. Hence I posted in this forum. Highly disappointed :( - user5917011
JaCoCo is about jacoco.org/jacoco , so you probably get a downvote, because Jenkins JaCoCo Plugin wasn't mentioned at all initially ( stackoverflow.com/revisions/41893846/1 ) and so question wasn't clear. While now it is mentioned, you still can improve title and reword body. - Godin

5 Answers

33
votes

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

47
votes

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

17
votes

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

3
votes

If you'll have a look on a list of plugins compatible with pipeline, you'll find that Jenkins JaCoCo Plugin was made compatible, but without any update of documentation, except entry in changelog:

Version 2.1.0 (Sep 29, 2016)

JENKINS-27120 Adding Workflow support for JaCoCo publisher

probably because its usage is simple and similar to usage of many other steps:

step([$class: 'JacocoPublisher', ...])
1
votes

The best way to use Jacoco jenkins plugin is to take care of generating the executable (jacoco.exec) in the application and then pointing the location of that file in the pipeline. Although for a multi module project, the configuration would be slightly different, refer : Jacoco code coverage for multi module maven project

Jenkins Pipeline would look like :

jacoco(
    execPattern: '**/path_to_file/jacoco.exec',
    classPattern: '**/coverage/**',
    sourcePattern: '**/coverage/**',
    inclusionPattern: '**/*.class'
)