8

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.

delor
  • 800
  • 1
  • 7
  • 19

2 Answers2

7

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") })
}
Antimonit
  • 2,846
  • 23
  • 34
-1

There is answer in official documentation https://docs.gradle.org/current/userguide/jacoco_plugin.html

tasks.jacocoTestReport {
    reports {
        xml.isEnabled = false
        csv.isEnabled = false
        html.destination = file("${buildDir}/jacocoHtml")
    }
}
teur
  • 7
  • 1
  • 5
    I need a task that aggregates test coverage results from subprojects. This solution does not do that. – delor Dec 03 '18 at 09:07