0

I have a multi-module gradle project configured with convention plugins. I'd like to be able to exclude classes from the code coverage report both in the convention plugin and in the module level build script.

Here are the relevant files from the project:

buildSrc/src/main/kotlin/mapstruct-conventions.gradle.kts

// ...other configurations here

tasks.jacocoTestReport {
    classDirectories.setFrom(
        files(
            classDirectories.files.map {
                fileTree(it).apply {
                    exclude("**/*MapperImpl*")
                }
            }
        )
    )
}

database-jooq/build.gradle.kts

plugins {
    id("mapstruct-conventions")
}

tasks.jacocoTestReport {
    classDirectories.setFrom(
        files(
            classDirectories.files.map {
                fileTree(it).apply {
                    exclude("**/jooq/generated/**")
                }
            }
        )
    )
}

For some reason the two exclusions seem to override each other. If both are present, then only the one in the convention plugin will be applied. If I remove the exclusion from the convention plugin script, then the exclusion in the database-jooq module works fine.

I tried to define them in an afterEvaluate block as suggested in some answers but I get the following exception:

Caused by: org.gradle.api.internal.AbstractMutationGuard$IllegalMutationException: Project#afterEvaluate(Action) on project ':database-jooq' cannot be executed in the current context.

Is there a way to define JaCoCo exclusions in different places and apply them together?

Martin Tarjányi
  • 8,863
  • 2
  • 31
  • 49
  • [`setsFrom()`](https://docs.gradle.org/current/javadoc/org/gradle/api/file/ConfigurableFileCollection.html#setFrom-java.lang.Iterable-) will override the existing directories. You probably want [`from()`](https://docs.gradle.org/current/javadoc/org/gradle/api/file/ConfigurableFileCollection.html#from-java.lang.Object...-), which will add new directories and retain any existing ones. – aSemy Dec 16 '22 at 22:49
  • @aSemy That would work if I wanted to **include** additional directories but I want the opposite, I want to **exclude** more. – Martin Tarjányi Dec 17 '22 at 07:07
  • Ah I see, thanks for clarifying. Can you using `excludes` in the [JaCoCo Task Extension](https://docs.gradle.org/current/userguide/jacoco_plugin.html#sec:jacoco_specific_task_configuration) instead? `tasks.withType().configureEach { configure { ... } }` – aSemy Dec 17 '22 at 10:57
  • @aSemy that works for excluding the class from collecting execution data but the class still shows up in the HTML report unfortunately – Martin Tarjányi Dec 17 '22 at 17:23

0 Answers0