Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define Jacoco report aggregation in Gradle Kotlin DSL?

In Gradle Groovy I was using

task jacocoRootReport(type: JacocoReport) {
  dependsOn = subprojects.test

  subprojects.each {
    sourceSets it.sourceSets.main
  }

  executionData.from fileTree(project.rootDir.absolutePath).include("**/build/jacoco/*.exec")

  reports {
    html.enabled = true
    xml.enabled = true
    csv.enabled = false
  }
}

but I have no idea how to translate it to Kotlin DSL so that Jacoco results from subprojects would get aggregated into one report in root project.

like image 731
delor Avatar asked Nov 28 '18 14:11

delor


1 Answers

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") })
}
like image 197
Antimonit Avatar answered Nov 01 '22 10:11

Antimonit