# Jacoco exclude generated from coverage

**URL:** <https://discuss.gradle.org/t/jacoco-exclude-generated-from-coverage/46374>\
**Category:** Help/Discuss\
**Created:** [August 29, 2023, 6:14pm UTC](https://discuss.gradle.org/t/jacoco-exclude-generated-from-coverage/46374 "2023-08-29T18:14:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dmitrizamysloff](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@dmitrizamysloff](https://discuss.gradle.org/u/dmitrizamysloff)\
**Post date:** [August 29, 2023, 6:14pm UTC](https://discuss.gradle.org/t/jacoco-exclude-generated-from-coverage/46374/1 "2023-08-29T18:14:30Z")

</div>

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:

```plaintext
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) {
                            exclude(
                                " **/build/**",
                                "build/**",
                                " **/generated/**"
                            )
                        }
                    })
                }
            }

```

but I still see generated classes in jacoco report.

---

<div class="post-metadata">

**Author:** ![Vampire](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/vampire/32/9082_2.png) [@Vampire](https://discuss.gradle.org/u/Vampire)\
**Post date:** [August 29, 2023, 8:01pm UTC](https://discuss.gradle.org/t/jacoco-exclude-generated-from-coverage/46374/2 "2023-08-29T20:01:33Z")

</div>

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](https://github.com/gradle/gradle/issues/13013)
