Skip to content

Jacoco: configuring Jenkins job to count coverage for project with subprojects

August 26, 2015

It happened that our project faced problem with identifying code unit test coverage on every build basis. To put it into CI flow we decided to use JaCoCo. JaCoCo is a free code coverage library for Java, which has been created by the EclEmma team. One can find documentation, download and see sample reports by following link http://www.eclemma.org/jacoco/ . For performing everyday code coverage reports and graphed coverage trends in CI environment JaCoCo Jenkins plugin https://wiki.jenkins-ci.org/display/JENKINS/JaCoCo+Plugin was used and in job configuration looks like(also you can exclude some classes you don’t want to calculate coverage):

jacoco_jenkins

Also we need to add additional build task in build task, e.g.:

gradle clean ear jacocoTestReport

So after it we configured Jenkins and need to add some gradle task into your project. In our case we’ve got root project with subprojects to calculate coverage for.

Root project build.gradle additional tasks look like:

...
allprojects {
        ...
	apply plugin: 'java'
	apply plugin: 'jacoco'
}

...
task codeCoverageReport(type: JacocoReport) {
    // Gather execution data from all subprojects
    // (change this if you e.g. want to calculate unit test/integration test coverage separately)
    executionData fileTree(project.rootDir.absolutePath).include("**/build/jacoco/*.exec")

    // Add all relevant sourcesets from the subprojects 
    subprojects.each {
       sourceSets it.sourceSets.main
    }

    reports {
      xml.enabled true
      html.enabled true
      html.destination "${buildDir}/reports/jacoco"
      csv.enabled false
    }
}

// always run the tests before generating the report
codeCoverageReport.dependsOn {
    subprojects*.test
}

Subproject build.gradle additional tasks:

...

allprojects {
...
apply plugin: 'java'
apply plugin: 'jacoco'
}

...
task codeCoverageReport2(type: JacocoReport) {

    // Gather execution data from all subprojects
    // (change this if you e.g. want to calculate unit test/integration test coverage separately)
    executionData fileTree(project.rootDir.absolutePath).include("**/build/jacoco/*.exec")

    // Add all relevant sourcesets from the subprojects 
    subprojects.each {
       sourceSets it.sourceSets.main
    }

    reports {
      xml.enabled true
      html.enabled true
      html.destination "${buildDir}/reports/jacoco"
      csv.enabled false
    }
}

// always run the tests before generating the report
codeCoverageReport2.dependsOn {
    subprojects*.test
}
...

After configuring projects, Jenkins one can build job and get JaCoCo coverage info.
Detailed information about code coverage counters you can read here http://www.eclemma.org/jacoco/trunk/doc/counters.html

Report and coverage trand looks like:

jacoco_coverage

All links can be clicked to see coverage issues, e.g.:

jacoco_code_problems

I hope current post will help you.

Bye.

.

Advertisements

From → Java, Jenkins

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: