I have a gradle task that uses ant to create code coverage reports via Cobertura. This works as expected when excluding one file:
fileset(dir: classes,
includes:"**/*.class",
excludes:"**/*Test.class")
But when I want to exclude multiple files the *Test.class
files remain exlcuded but not the *Jar.class
.
fileset(dir: classes,
includes:"**/*.class",
excludes:["**/*Test.class", "**/*Jar.java"])
I'm now testing this with one file but the end goal is to exclude an entire package. What would be the correct syntax for excluding multiple files?
Entire gradle task:
logger.info "Configuring Cobertura Plugin"
configurations{
coberturaRuntime {extendsFrom testRuntime}
}
dependencies {
coberturaRuntime 'net.sourceforge.cobertura:cobertura:2.1.1'
}
def serFile="${project.buildDir}/cobertura.ser"
def classes="${project.buildDir}/"
def classesCopy="${classes}-copy"
task cobertura(type: Test){
dependencies {
testRuntime 'net.sourceforge.cobertura:cobertura:2.1.1'
}
systemProperty "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,
includes:"**/*.class",
excludes:["**/*Test.class", "**/*Jar.class"])
}
}
}
cobertura.doLast{
if (new File(classesCopy).exists()) {
//create html cobertura report
// ant.'cobertura-report'(destdir:"${project.reportsDir}/cobertura",
// format:'html', srcdir:"src/main/java", datafile: serFile)
//create xml cobertura report
ant.'cobertura-report'(destdir:"${project.reportsDir}/cobertura",
format:'xml', srcdir:"src/main/java", datafile: serFile)
ant.delete(file: classes)
ant.move(file: classesCopy, tofile: classes)
}
}
The answer turned out to be quite simple. Instead of giving the includes and excludes as parameters to fileset it is possible to use multiple include and exclude methods inside a fileset method. So replace the fileset line from my question with this:
fileset(dir: classes) {
include(name: "**/*.class")
exclude(name: "**/*Test.class")
exclude(name: "**/*Jar.class")
}
I found this in gradles documentation about passing nested elements to an Ant task.
See: https://docs.gradle.org/current/userguide/ant.html#example_passing_nested_elements_to_an_ant_task