I am trying to set my project up to run JaCoCo and to fail the build if it has less that 80% test coverage. The caveat is that I want to exclude a particular file and I can't seem to make that work. I have scoured the web and read oodles of Stack Overflow answers, blog posts, and the documentation for the plugin, but I cannot make anything work. It always still includes the file that I am trying to exclude.
I started with:
tasks.jacocoTestCoverageVerification {
violationRules {
rule {
limit {
minimum = "0.8".toBigDecimal()
}
}
}
}
which obviously will include everything and it does. I have tried the following variations based on what I have read, but none of them have worked:
tasks.jacocoTestCoverageVerification {
violationRules {
rule {
classDirectories.setFrom(sourceSets.main.get().output.asFileTree.matching {
exclude("path.to.my.class.HelloWorld")
})
limit {
minimum = "0.8".toBigDecimal()
}
}
}
}
tasks.jacocoTestCoverageVerification {
classDirectories.setFrom(
sourceSets.main.get().output.asFileTree.matching {
exclude("path.to.my.class.HelloWorld")
}
)
violationRules {
rule {
limit {
minimum = "0.8".toBigDecimal()
}
}
}
}
tasks.jacocoTestCoverageVerification {
violationRules {
rule {
limit {
minimum = "0.8".toBigDecimal()
}
}
}
}
tasks {
getByName<JacocoReport>("jacocoTestReport") {
afterEvaluate {
classDirectories.setFrom(files(classDirectories.files.map {
fileTree(it) {
exclude("path.to.my.class.HelloWorld")
}
}))
}
}
}
tasks.jacocoTestCoverageVerification {
violationRules {
rule {
limit {
minimum = "0.8".toBigDecimal()
}
}
}
}
tasks.withType<JacocoReport> {
classDirectories.setFrom(
sourceSets.main.get().output.asFileTree.matching {
exclude("path.to.my.class.HelloWorld")
}
)
}
The output of all of these is exactly the same:
Execution failed for task ':jacocoTestCoverageVerification'.
> Rule violated for bundle OneUIBackend: instructions covered ratio is 0.6, but expected minimum is 0.8
Does anyone have any insight in to how to do this properly?
So it turns out that I was declaring the excluded file as a classpath rather than a filepath. The final solution is:
tasks.jacocoTestReport {
dependsOn(tasks.test)
finalizedBy(tasks.jacocoTestCoverageVerification)
classDirectories.setFrom(
sourceSets.main.get().output.asFileTree.matching {
exclude("path/to/my/file/HelloWorld*")
}
)
}
tasks.jacocoTestCoverageVerification {
violationRules {
rule {
classDirectories.setFrom(sourceSets.main.get().output.asFileTree.matching {
exclude("path/to/my/file/HelloWorld*")
})
limit {
minimum = "0.8".toBigDecimal()
}
}
}
}