Jacoco exclude generated from coverage

I am writing gradle plugin using kotlin. I have test project which uses this plugin. The test project includes generation of java classes from openapi generation. The directory of generated classes (/build/generated/swagger) will be added to main source set. If I use my plugin, which defines, jacoco plugin it includes these sources into coverage calculation and sonar then fails. So my idea to exclude any sources from build directory already in plugin.

I tried something like this:

project.afterEvaluate {
                project.tasks.withType<JacocoReport> {

                    val jacocoDir = project.layout.buildDirectory.dir("jacoco").get()
                    executionData.from(jacocoDir.file("test.exec"), jacocoDir.file("integrationTest.exec"))

                    classDirectories.setFrom(classDirectories.files.map {
                        project.fileTree(it) {

but I still see generated classes in jacoco report.

The best and easiest solution is to make sure all the generated classes have any annotation that is called Generated and has at least retention CLASS, then JaCoCo automatically excludes those classes from the report. Also find here the issue to watch and thumbs-up for an improvement in that field: Easier includes / excludes for JacocoReportBase · Issue #13013 · gradle/gradle · GitHub