Piotr Gabryanczyk’s Blog

Java, Refactoring, AOP, Spring, DDD, TDD, etc.

  • Blogroll

    • I have joined Anti-IF Campaign

Posts Tagged ‘gradle’

Gradle Cobertura Integration Revisited

Posted by Piotr Gabryanczyk on April 17, 2010


I’d like to run cobertura coverage report using gradle in a non intrusive way similar to maven.
I just want to do this:

gradle cobertura

and in my script:

apply from: 'cobertura.gradle'

Current solution

There is a great solution to this problem here. It works, but is not as unintrusive and not as easy to use as I would like it to be.

My solution

This works for Gradle version 0.9-preview-1.


logger.info "Configuring Cobertura Plugin"

  coberturaRuntime {extendsFrom testRuntime}

dependencies {
  coberturaRuntime 'net.sourceforge.cobertura:cobertura:1.9.3'

def serFile="${project.buildDir}/cobertura.ser"
def classes="${project.sourceSets.main.classesDir}"
def classesCopy="${classes}-copy"

task cobertura(type: Test){
  dependencies {
    testRuntime 'net.sourceforge.cobertura:cobertura:1.9.3'

  systemProperties["net.sourceforge.cobertura.datafile"] = serFile

cobertura.doFirst  {
  logger.quiet "Instrumenting classes for Cobertura"
  ant {
    delete(file:serFile, failonerror:false)
    delete(dir: classesCopy, failonerror:false)    
    copy(todir: classesCopy) { fileset(dir: classes) }

    taskdef(resource:'tasks.properties', classpath: configurations.coberturaRuntime.asPath)
    'cobertura-instrument'(datafile: serFile) {
      fileset(dir: classes,

  if (new File(classesCopy).exists()) {
            format:'html', srcdir:"src/main/java", datafile: serFile)

    ant.delete(file: classes)
    ant.move(file: classesCopy, tofile: classes)

And then your build.gradle could look like that:

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


Posted in Uncategorized | Tagged: , , | 4 Comments »