Skip to content

Instantly share code, notes, and snippets.

@dstine
Created September 13, 2012 11:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dstine/3713721 to your computer and use it in GitHub Desktop.
Save dstine/3713721 to your computer and use it in GitHub Desktop.
cobertura snippet
dependencies {
testRuntime 'net.sourceforge.cobertura:cobertura:1.9.4.1'
}
def coberturaSerFile="${project.buildDir}/cobertura.ser"
def coberturaSrcOriginal="${project.sourceSets.main.output.classesDir}"
def coberturaSrcCopy="${coberturaSrcOriginal}-copy"
def coberturaReportDirName='coverage'
test.doFirst {
ant {
// delete data file for cobertura, otherwise coverage would be added
delete(file:coberturaSerFile, failonerror:false)
// delete copy of original classes
delete(dir: coberturaSrcCopy, failonerror:false)
// import cobertura task, so it is available in the script
taskdef(resource:'tasks.properties', classpath: configurations.testRuntime.asPath)
// create copy (backup) of original class files
copy(todir: coberturaSrcCopy) {
fileset(dir: coberturaSrcOriginal)
}
// instrument the relevant classes in-place
'cobertura-instrument'(datafile:coberturaSerFile) {
fileset(dir: coberturaSrcOriginal,
includes:'com/copyright/**/*.class')
}
}
}
test {
systemProperties['net.sourceforge.cobertura.datafile'] = coberturaSerFile
// Avoid "Expecting a stackmap frame at branch target" error
// See http://stackoverflow.com/questions/7970622/java-7-jvm-verifyerror-in-eclipse
jvmArgs '-XX:-UseSplitVerifier'
}
test.doLast {
if (new File(coberturaSrcCopy).exists()) {
// replace instrumented classes with backup copy again
ant {
delete(file: cgroberturaSrcOriginal)
move(file: coberturaSrcCopy, tofile: coberturaSrcOriginal)
}
// create cobertura reports
ant.'cobertura-report'(
destdir:"${project.buildDir}/test-results",
format:'xml',
srcdir:"${sourceSets.main.groovy.srcDirs.toArray()[0]}",
datafile:coberturaSerFile)
ant.'cobertura-report'(
destdir:"${project.buildDir}/reports/${coberturaReportDirName}",
format:'html',
srcdir:"${sourceSets.main.groovy.srcDirs.toArray()[0]}",
datafile:coberturaSerFile)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment