51

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

user5917011
  • 1,137
  • 5
  • 14
  • 22
  • 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 Jan 27 '17 at 16:24
  • 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 Jan 27 '17 at 17:00
  • JaCoCo is about http://www.jacoco.org/jacoco/ , so you probably get a downvote, because Jenkins JaCoCo Plugin wasn't mentioned at all initially ( http://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 Jan 27 '17 at 18:25
  • See: https://stackoverflow.com/a/48685604/1279002 – theINtoy Feb 08 '18 at 13:39

6 Answers6

49

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

haylem
  • 22,460
  • 3
  • 67
  • 96
user2688838
  • 761
  • 5
  • 11
35

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

20

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

phillipuniverse
  • 2,025
  • 1
  • 14
  • 15
  • 4
    WARNING: if you do not specify a class pattern or exclusion pattern then Jenkins will include your test classes in the coverage report, which totally skew the numbers. For gradle builds this is what I am now using: `jacoco(execPattern: '**/build/jacoco/**.exec', classPattern: '**/classes/*/main')` – nerdherd Sep 12 '19 at 14:32
4

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', ...])
Godin
  • 9,801
  • 2
  • 39
  • 76
1

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'
)
addu390
  • 61
  • 1
  • 6
1

Posting one more answer since jacoco DSL from the accepted one did not work for me.

WorkflowScript: 86: Invalid parameter "execPattern", did you mean "path"? @ line 86, column 20.
               jacoco(execPattern: 'build/jacoco/test.exec')

I can't say exactly what set of plugins is used on my Jenkins. I only know that the version of Jenkins is 2.289.1

The following DSL for collecting coverage from the Code Coverage API plugin worked out for me:

publishCoverage adapters: [jacocoAdapter('build/reports/jacoco/test/jacocoTestReport.xml')]
Kirill
  • 6,762
  • 4
  • 51
  • 81