185

Problem:

I have a project with and I want to be able to filter certain classes and/or packages.

Related Documentation:

I have read the following documentation:

Official site: http://www.eclemma.org/jacoco/index.html

Official docs for : https://gradle.org/docs/current/userguide/jacoco_plugin.html

Official Github issues, working on coverage: https://github.com/jacoco/jacoco/wiki/FilteringOptions https://github.com/jacoco/jacoco/issues/14

Related StackOverflow Links:

JaCoCo & Gradle - Filtering Options (No answer)

Exclude packages from Jacoco report using Sonarrunner and Gradle (Not using )

JaCoCo - exclude JSP from report (It seems to work for , I am using )

Maven Jacoco Configuration - Exclude classes/packages from report not working (It seems to work for , I am using )

JaCoCo gradle plugin exclude (Could not get this to work)

Gradle Jacoco - coverage reports includes classes excluded in configuration (Seems very close, it used doFirst, did not work for me)

Example of what I have tried:

apply plugin: 'java'
apply plugin: 'jacoco'

buildscript {
    repositories {
        mavenCentral()
        jcenter()
    }
}

repositories {
    jcenter()
}

jacocoTestReport {
    reports {
        xml {
            enabled true // coveralls plugin depends on xml format report
        }

        html {
            enabled true
        }
    }

    test {
        jacoco {
            destinationFile = file("$buildDir/jacoco/jacocoTest.exec")
            classDumpFile = file("$buildDir/jacoco/classpathdumps")
            excludes = ["projecteuler/**"] // <-- does not work
            // excludes = ["projecteuler"]
        }
    }
}

Question:

How can I exclude certain packages and classes when generating the coverage reports?

5
  • 1
    A third party option (FD I'm founder of): If you upload reports to Codecov you can ignore any files you like after the fact in the features section of the product. Thanks.
    – Steve Peak
    May 4, 2015 at 19:31
  • @StevePeak So you can filter by packages online using Codecov? Also, I saw the Github, what about Android support, I saw Java. I should still have to send you all of the reports then filter after vs filtering before. May 6, 2015 at 17:02
  • You can filter based on a regexp method of any filed you do not want to include. All java is supported via Jacoco reports. Just filtering after the fact on Codecov works. It will remember your filters and apply it to all future reports. Thanks!
    – Steve Peak
    May 6, 2015 at 17:47
  • 7
    I'm curious; what does the excludes from the official documentation actually do then? Is it pretty much useless? Oct 30, 2015 at 19:36
  • That excludes is not on the coverage task, but on the test task. It excludes files from being instrumented by JaCoCo and thus coverage being recorded. You can use this if you don't want to record coverage for some classes, if you cannot because of some conflict with another instrumenting agent, or because you pre-instrumented classes. This will not exclude a class from the report, especially in the last case mentioned, this would be a horrible idea.
    – Vampire
    Sep 25, 2019 at 8:14

17 Answers 17

163

Thanks to, Yannick Welsch:

After searching Google, reading the Gradle docs and going through older StackOverflow posts, I found this answer on the Official forums!

jacocoTestReport {
    afterEvaluate {
        classDirectories.setFrom(files(classDirectories.files.collect {
            fileTree(dir: it, exclude: 'com/blah/**')
        }))
    }
}

Source: https://issues.gradle.org/browse/GRADLE-2955

For older gradle versions < 5.x may need to use classDirectories = files(classDirectories.files.collect { instead of classDirectories.setFrom

Solution to my build.gradle for Java/Groovy projects:

apply plugin: 'java'
apply plugin: 'jacoco'

jacocoTestReport {
    reports {
        xml {
            enabled true // coveralls plugin depends on xml format report
        }

        html {
            enabled true
        }
    }

    afterEvaluate {
        classDirectories = files(classDirectories.files.collect {
            fileTree(dir: it,
                    exclude: ['codeeval/**',
                              'crackingthecode/part3knowledgebased/**',
                              '**/Chapter7ObjectOrientedDesign**',
                              '**/Chapter11Testing**',
                              '**/Chapter12SystemDesignAndMemoryLimits**',
                              'projecteuler/**'])
        })
    }
}

As you can see, I was successfully able to add more to exclude: in order to filter a few packages.

Source: https://github.com/jaredsburrows/CS-Interview-Questions/blob/master/build.gradle

Custom tasks for other projects such as Android:

apply plugin: 'jacoco'

task jacocoReport(type: JacocoReport) {
    reports {
        xml {
            enabled true // coveralls plugin depends on xml format report
        }

        html {
            enabled true
        }
    }

    afterEvaluate {
        classDirectories = files(classDirectories.files.collect {
            fileTree(dir: it,
                    exclude: ['codeeval/**',
                              'crackingthecode/part3knowledgebased/**',
                              '**/Chapter7ObjectOrientedDesign**',
                              '**/Chapter11Testing**',
                              '**/Chapter12SystemDesignAndMemoryLimits**',
                              'projecteuler/**'])
        })
    }
}

Source: https://github.com/jaredsburrows/android-gradle-java-app-template/blob/master/gradle/quality.gradle#L59

16
  • 2
    @BradPitcher No problem! It took me a while to find the right answer. It just seems so "hackish". I hope they come up with a nicer way. May 27, 2015 at 3:45
  • 2
    Something like: exclude: ['**/*Test*.*']) Oct 20, 2015 at 18:36
  • 2
    jacocoTestReport only works with both jacoco and java plugins. This is not for Android. Please see my repo here for android: github.com/jaredsburrows/android-gradle-java-template/blob/… Jan 25, 2016 at 3:53
  • 21
    The classDirectories = results in this warning. The JacocoReportBase.setClassDirectories(FileCollection) method has been deprecated. This is scheduled to be removed in Gradle 6.0. Use getClassDirectories().from(...). It would be great to show a solution compatible with Gradle 6.0. Aug 16, 2019 at 20:57
  • 6
    getting Cannot set the value of read-only property 'classDirectories' for task ':jacocoTestReport' of type org.gradle.testing.jacoco.tasks.JacocoReport using this configuration. Using gradle 5.6 and Java 8 Nov 6, 2020 at 20:31
99

For Gradle version 5.x, the classDirectories = files(...) gives a deprecation warning and does not work at all starting from Gradle 6.0 This is the nondeprecated way of excluding classes:

jacocoTestReport {
    afterEvaluate {
        classDirectories.setFrom(files(classDirectories.files.collect {
            fileTree(dir: it, exclude: 'com/exclude/**')
        }))
    }
}
8
  • 4
    Or simply use classDirectories.from (to append to the list instead of overriding list) Jul 2, 2019 at 11:12
  • 3
    You will add [] after exclude: to include several paths.
    – WesternGun
    Oct 29, 2019 at 8:18
  • Excellent, I use gradle 6.0.1 and this solution worked for me.
    – Dargenn
    Apr 24, 2020 at 8:07
  • this worked, can someone please explain what exactly is happening Dec 1, 2020 at 17:22
  • I have changed the acccepted answer to reflect this change
    – tkruse
    Jan 8, 2021 at 2:13
24

for me, it's fine working with

test {
  jacoco {
    excludes += ['codeeval/**',
                 'crackingthecode/part3knowledgebased/**',
                 '**/Chapter7ObjectOrientedDesign**',
                 '**/Chapter11Testing**',
                 '**/Chapter12SystemDesignAndMemoryLimits**',
                 'projecteuler/**']
  }
}

as stated out in documentation https://docs.gradle.org/current/userguide/jacoco_plugin.html#N16E62 and initally asked so the answer is:

so if you ask me: it's not a question of

excludes = ["projecteuler/**"]

or

excludes += ["projecteuler/**"]

but

excludes = ["**/projecteuler/**"]

to exclude a package *.projecteuler.*

and test {} on project level, not nested in jacocoTestReport

2
  • 3
    This seems to give the classes 0% coverage rather than omit them altogether. I'm using JaCoCi 0.8.5 and Gradle 6.0
    – tschumann
    Oct 29, 2019 at 4:08
  • 1
    This the right way to tell jacoco to not meddle with some classes, other approaches only affect the reporting part.
    – bric3
    Jun 12, 2020 at 13:12
15

For Gradle6 Use something like below, because they made classDirectories as final, we cannot re-assign it, but a setter method exists classDirectories.setFrom which can be utilized

    jacocoTestReport {
    reports {
        xml.enabled true
        html.enabled true
        html.destination file("$buildDir/reports/jacoco")
    }

    afterEvaluate {
        classDirectories.setFrom(files(classDirectories.files.collect {
            fileTree(dir: it,
                    exclude: ['**/server/**',
                              '**/model/**',
                              '**/command/**'
                    ]
            )
        }))
    }
}
1
  • 1
    I am trying to use the below snippet in build.gradle to exclude my dtos but it is not working. I am using gradle 6.6.1 version. Let me know if any changes jacocoTestReport { reports { xml.enabled true html.enabled true html.destination file("$buildDir/reports/jacoco") } afterEvaluate { classDirectories.setFrom(files(classDirectories.files.collect { fileTree(dir: it, exclude: ['/dto/' ] ) })) } } Apr 22, 2021 at 9:41
14

In order to filter in jacoco report, exclusion need to be done in two task jacocoTestReport and jacocoTestCoverageVerification.

sample code

    def jacocoExclude = ['**/example/**', '**/*Module*.class']

    jacocoTestReport {
        afterEvaluate {
            getClassDirectories().setFrom(classDirectories.files.collect {
                fileTree(dir: it, exclude: jacocoExclude)
            })
        }
    }

    jacocoTestCoverageVerification {
        afterEvaluate {
            getClassDirectories().setFrom(classDirectories.files.collect {
                fileTree(dir: it, exclude: jacocoExclude)
            })
        }

        ...
    }



2
  • actually (at least with Gradle 7.x) you don't need to set the exclusion for jacocoTestCoverageVerification Dec 26, 2022 at 15:35
  • Variable can be avoided if we say classDirectories.setFrom(tasks.jacocoTestReport.classDirectories)
    – darw
    Jan 18 at 14:45
13

for Kotlin users, here is what I used (gradle 6.7)

build.gradle.kts

tasks.jacocoTestReport {
    // tests are required to run before generating the report
    dependsOn(tasks.test) 
    // print the report url for easier access
    doLast {
        println("file://${project.rootDir}/build/reports/jacoco/test/html/index.html")
    }
    classDirectories.setFrom(
        files(classDirectories.files.map {
            fileTree(it) {
                exclude("**/generated/**", "**/other-excluded/**")
            }
        })
    )
}

as suggested here : https://github.com/gradle/kotlin-dsl-samples/issues/1176#issuecomment-610643709

8
+100

Here is a solution for this problem in ANT. This can be adapted to gradle by adding the following under the jacocoTestReport task. Although this isn't really documented by jacoco, it seems like the only way to filter the test results for now.

afterEvaluate {
    classDirectories = files(classDirectories.files.collect {
        fileTree(dir: it, exclude: 'excluded/files/**')
    })
}
3
  • Alright, thank you for confirming that my findings are correct! I hope they make it easier to filter in the future or simply document how to filter using Gradle. May 7, 2015 at 14:43
  • Is there any way to exclude a source set from inclusion in jacoco report? I want to exclude all source files which are located in generated/java/ instead of main/java. Mar 7, 2018 at 7:53
  • This won't work for a clean build. afterEvaluate runs in Gradle's configuration phase which is before any tasks have executed and before any classes have been compiled. So the classes dirs will be empty
    – lance-java
    Sep 26, 2020 at 8:27
7

For those who still scratching their heads to filter certain packages from the coverage report, here is the configuration that works for me using the most recent libraries.

   Build tool: Gradle 6.5 (also tried for 6.7)
   Coverage Tool: Jacoco 0.8.5

Things to consider/Justifications

  • afterScript is not required
  • Need to exclude it twice, one for report generation and coverage verification
  • The intellij IDE recommends to use excludes param instead of exclude. Either of which just works fine
  • While providing the regex, be sure to provide the .class files and not the .java files.
  • Post Gradle 5, classDirectories is a read-only field, therefore, use classDirectories.setFrom
jacocoTestReport {
    doLast {
        println("See report file://${project.rootDir}/build/reports/jacoco/test/html/index.html")
    }
    excludedClassFilesForReport(classDirectories)
}

jacocoTestCoverageVerification {
    excludedClassFilesForReport(classDirectories)
    violationRules {
        rule {
            limit {
                minimum = 0.55
            }
        }
    }
}

private excludedClassFilesForReport(classDirectories) {
    classDirectories.setFrom(files(classDirectories.files.collect {
        fileTree(dir: it, exclude: [
                '**/common/apigateway/*.class',
                '**/common/*/client/*Client*.class',
                '**/configuration/*ClientConfiguration.class',
                '**/model/search/*SearchService.class'
        ])
    }))
}

check.dependsOn jacocoTestCoverageVerification
1
  • Private method can be avoided via classDirectories.setFrom(tasks.jacocoTestReport.classDirectories)
    – darw
    Jan 18 at 14:42
5

This has been out for a while, but I just ran across this. I was struggling with all the exclusions needed. I found it was something much more simple for me. If you follow the Maven project layout style /src/main/java and /src/test/java, you simply need to put buildDir/classes/main in your classDirectories config like so:

afterEvaluate {
    jacocoTestReport {
        def coverageSourceDirs = ['src/main/java']
        reports {
            xml.enabled false
            csv.enabled false
            html.destination "${buildDir}/reports/jacocoHtml"
        }
        sourceDirectories = files(coverageSourceDirs)
        classDirectories = fileTree(
                dir: "${project.buildDir}/classes/main",
                excludes: [
                      //whatever here like JavaConfig etc. in /src/main/java
                     ]
        )
    }
}
1
  • Just saw this again. This was taken directly from a working config. Perhaps your version of Gradle and Jacoco were different than mine. Sorry.
    – Randy
    Oct 9, 2018 at 20:12
5

The code below excludes classes from coverage verification as well:

jacocoTestCoverageVerification {
    afterEvaluate {
        classDirectories = files(classDirectories.files.collect {
            fileTree(dir: "${project.buildDir}/classes/main",
                    exclude: ['**/packagename/**'])
        })
    }
}
1
  • Hey, your version throws the following exception message: "Cannot set the value of read-only property 'classDirectories'". Fixed with change classDirectories -> classDirectories.from .But unfortunately my package still exists inside of the report
    – Oleksandr
    Dec 1, 2021 at 10:47
5

For anyone going out of their mind looking for this answer in Kotlin DSL, here it is:

val jacocoExclude = listOf("**/entities/**", "**/dtos/**")
jacocoTestReport {
    reports {
        xml.isEnabled = true
        csv.isEnabled = false
    }
    classDirectories.setFrom(classDirectories.files.map {
        fileTree(it).matching {
            exclude(jacocoExclude)
        }
        })
}
test {
    useJUnitPlatform()
    systemProperty("gradle.build.dir", project.buildDir)
    finalizedBy(jacocoTestReport)
    extensions.configure(JacocoTaskExtension::class) {
        excludes = jacocoExclude
    }
}
3

For me, i have to do exclude in 2 places

jacocoTestReport {
    dependsOn test // tests are required to run before generating the report


    afterEvaluate {
        excludedClassFilesForReport(classDirectories)
    }
}

jacocoTestCoverageVerification {

    afterEvaluate {
        excludedClassFilesForReport(classDirectories)
    }
}

private excludedClassFilesForReport(classDirectories) {
    classDirectories.setFrom(files(classDirectories.files.collect {
        fileTree(dir: it,
            exclude: [
                    'com/test/a/config/**',
                    'com/test/b/constant/**',
                    'com/test/c/MainApp.class'
            ]
    )
}))
}
2

some comments mentioned the deprecation warning. to solve just use the getter:

afterEvaluate {
    getClassDirectories().from(files(classDirectories.files.collect {
        fileTree(dir: it, exclude: 'com/blah/**')
    }))
}
1

Here is my working config in Gradle with Jacoco 0.8.5 :

def jacocoExclusions = [
        '**/config/*Config.class',
        '**/config/*Advice.class',
        '**/security/*AuthorityRoles.class',
        '**/*Application.class'
];

jacocoTestReport {
  reports {
    xml.enabled false
    csv.enabled false
    html.destination file("${buildDir}/reports/jacocoHtml")
  }
  afterEvaluate {
    classDirectories.setFrom(files(classDirectories.files.collect {
      fileTree(dir: it,
              exclude: jacocoExclusions
      )
    }))
  }
}

jacocoTestCoverageVerification {
  afterEvaluate {
    classDirectories.setFrom(files(classDirectories.files.collect {
      fileTree(dir: it,
              exclude: jacocoExclusions
      )
    }))
  }
  violationRules {
    rule {
      excludes = jacocoExclusions
      limit {
        minimum = 0.90
      }
    }
  }
}
0

add below config in gradle.properties file

coverageExcludeClasses=["com.example.package.elasticsearch.*", "com.example.package2.*",]

1
  • 1
    coverageExcludeClasses doesn't appear to be a thing in JaCoCo/Gradle
    – Gus
    Jul 9, 2020 at 16:48
0

Gradle 6.8.3 thrown an exception. Cannot set the value of read-only property 'classDirectories' for task ':jacocoTestReport' of type org.gradle.testing.jacoco.tasks.JacocoReport.

so I found a way to fix the issue by using

classDirectories.setFrom(
            fileTree(dir: "build/classes/java/main")
                    .filter({file -> !file.path.contains('/dir1')})
                    .filter({file -> !file.path.contains('/dir2')})
                    .filter({file -> !file.path.contains('/dir3')})
    )
0

For Gradle 7.6.1 in Kotlin syntax this works for me (using setExcludes instead of exclude)

tasks.jacocoTestReport {
    reports {
        html.required.set(true)
        xml.required.set(true)
    }
    classDirectories.setFrom(files(classDirectories.files.map {
        fileTree(it) {
            setExcludes(listOf(
                    "**/first/path/to/skip/*.class",
                    "**/second/path/to/skip/*.class"
            ))
        }
    }))
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.