loading...
Cover image for How to generate an aggregated code coverage report for all Gradle subprojects

How to generate an aggregated code coverage report for all Gradle subprojects

jakub_zalas profile image Jakub Zalas ・1 min read

When using Gradle's multi-project builds, test and coverage reports are generated separately in each build directory of every sub-project.

To make Azure Pipelines happy I needed to generate an index file for all coverage-reports:

// build.gradle.kts

plugins {
    base
    kotlin("jvm") version "1.3.72" apply false
}

allprojects {
    group = "com.kaffeinelabs"
    version = "1.0.0-SNAPSHOT"
    apply(plugin = "jacoco")
}
tasks.register<JacocoReport>("jacocoRootReport") {
    subprojects {
        this@subprojects.plugins.withType<JacocoPlugin>().configureEach {
            this@subprojects.tasks.matching {
                it.extensions.findByType<JacocoTaskExtension>() != null }
           .configureEach {
                sourceSets(this@subprojects.the<SourceSetContainer>().named("main").get())
                executionData(this)
            }
        }
    }

    reports {
        xml.isEnabled = true
        html.isEnabled = true
    }
}

Jacoco configuration for sub-projects should be done as per usual.

To generate the report the jacocoRootReport task needs to be added to the build:

./gradlew test jacocoRootReport

If you also need to aggregate test reports, I covered it in another article:

Posted on by:

jakub_zalas profile

Jakub Zalas

@jakub_zalas

Quality focussed software engineer, architect, and trainer. Friends call me Kuba.

Discussion

markdown guide