Skip to content
Frank667 edited this page Feb 2, 2018 · 13 revisions

Cobertura Plugin

The Cobertura plugin adds cobertura code coverage targets to a project. See announcements and changes here.

Usage

To use, add the following to your build.gradle file:

Gradle 2.1 and later (But not for android-development, because this will generate an empty report!):

plugins {
    id 'net.saliman.cobertura' version '2.5.4' 
}

Gradle 2.0 and earlier (and android development):
buildscript {
    repositories {
        mavenCentral()
        // The next repo is only needed while using SNAPSHOT versions
        maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
    }
    dependencies {
        classpath "net.saliman:gradle-cobertura-plugin:2.5.4"
    }
}
apply plugin: 'net.saliman.cobertura'

The code will only be instrumented if the coberturaReport task is in the task graph.

Tasks

The Cobertura plugin adds several tasks to a project. See the usage.md file for more details.

Customizing

You can customize the behavior of the Cobertura plugin by using a closure to configure the convention properties

cobertura {
}

See the usage.md document for more details.

Gradle usage and Jenkins

You can create a custom cobertura.gradle plugin file and then apply this in your existing build.gradle file like so:

cobertura.gradle file

buildscript {
    repositories {
      mavenCentral()
	dependencies.classpath 	'net.saliman:gradle-cobertura-plugin:2.5.0',
				'net.sourceforge.cobertura:cobertura:2.1.1',
				'org.apache.commons:commons-lang3:3.2',
				'oro:oro:2.0.8',
				'org.ow2.asm:asm:5.0.1',
				'org.ow2.asm:asm-analysis:5.0.1',
				'org.ow2.asm:asm-commons:5.0.1',
				'org.ow2.asm:asm-tree:5.0.1',
				'org.ow2.asm:asm-util:5.0.1'
    }
}
 
// We use the type of the plugin instead of the id.
// This is the class that defines the plugin. We can leave off 
// .class, because Gradle uses Groovy.
apply plugin: net.saliman.gradle.plugin.cobertura.CoberturaPlugin

//check if the environment is JENKINS or LOCAL
def jenkinsHome = System.getenv('JENKINS_HOME')

//default build dir of local gradle
def jobpath = new File(buildDir.toString() + "/reports/cobertura")
if(jenkinsHome)
    jobpath = new File(System.getenv('JENKINS_HOME') + "/jobs/" + System.getenv('JOB_NAME') + "/builds/reports/cobertura")
mkdir jobpath

cobertura {
    coverageFormats = ['html', 'xml']				
    coverageIgnores = ['org.slf4j.Logger.*']	//only an example to skip instrumentation
    coverageReportDir = jobpath
}

test.finalizedBy(project.tasks.cobertura)
logger.info('DONE Configuring Cobertura Plugin')

build.gradle file

apply plugin: 'java'
apply plugin: 'eclipse'
apply from: 'cobertura.gradle'

dependencies {
    testRuntime "org.slf4j:slf4j-api:1.7.10"
}

task doStuff {
  // STUFF...
}

/*
// This is just an example of how to customize the Cobertura coverage reporting
// to exclude certain classes from being inspected and reported.
cobertura {
   coverageExcludes =
  ['.*com.example.gateway.cache.*',
   '.*com.example.gateway.common.*',
   '.*com.example.gateway.model.*',
   '.*com.example.gateway.dao.ConfigParamDao.*'
  ]
}
*/

Troubleshooting

1. Instrumentation errors might appear where getters/setters are used and so you might need to set this parameter like so, or remove the whole line:

coverageIgnoreTrivial = false

2. If you get all failing tests in builds then you will need to add the SLF4J library into your build.gradle like so:

dependencies {
    testRuntime "org.slf4j:slf4j-api:1.7.10"
}

If you add SLF4J in the cobertura.gradle file in the dependencies.classpath, it does not have any impact and the tests will continue to fail, so add it instead as a testRuntime dependency in your build.gradle file.