I'd suggest to configure and use an existing task jacocoTestReport
as it already has source sets predefined.
The minimal changes I had to do was to add:
tasks.jacocoTestReport {
reports {
xml.isEnabled = true
}
dependsOn(allprojects.map { it.tasks.named<Test>("test") })
}
and the report was generated in build\reports\jacoco\test\jacocoTestReport.xml
.
If you really need to define your own task, you can aggregate source sets the same way the jacocoTestReport
task does:
sourceSets(project.extensions.getByType(SourceSetContainer::class.java).getByName("main"))
(from gradle-6.2\src\jacoco\org\gradle\testing\jacoco\plugins\JacocoPlugin.java#addDefaultReportTask
)
The final code may look like this:
tasks.register<JacocoReport>("codeCoverageReport") {
executionData(fileTree(project.rootDir.absolutePath).include("**/build/jacoco/*.exec"))
sourceSets(project.extensions.getByType(SourceSetContainer::class.java).getByName("main"))
reports {
xml.isEnabled = true
xml.destination = File("${buildDir}/reports/jacoco/report.xml")
html.isEnabled = false
csv.isEnabled = false
}
dependsOn(allprojects.map { it.tasks.named<Test>("test") })
}