How can I verify the minimum coverage with some excluded classes and with the jacoco plugin?

In my case I did wanted to use the BUNDLE scope to set a threshold for the whole while excluding certain packages and files.

What worked for me in the end was adding the classDirectories exclude, as suggested in the original question, but inside afterEvaluate like this:

afterEvaluate {
        classDirectories = files(classDirectories.files.collect {
            fileTree(dir: it, exclude:  [
                    'com/example/my/package/*',
                    'com/example/service/MyApplication.class',
                    'com/google/protobuf/*'
            ])
        })
    }

For reference the complete build.gradle looks like this:

apply plugin: "jacoco”

jacocoTestCoverageVerification {
    afterEvaluate {
        getClassDirectories().setFrom(classDirectories.files.collect {
            fileTree(dir: it, exclude:  [
                    'com/example/my/package/*',
                    'com/example/service/MyApplication.class',
                    'com/google/protobuf/*'
            ])
        })
    }

    violationRules {
        rule {
            limit {
                minimum = 0.79
            }
        }
    }
}


// to run coverage verification during the build (and fail when appropriate)
check.dependsOn jacocoTestCoverageVerification  

You can find more details in my blog: http://jivimberg.io/blog/2018/04/26/gradle-verify-coverage-with-exclusions/


You are measuring a different thing that you are excluding. The default JaCoCo scope is "BUNDLE" which I believe means the whole code. I've never used that. I always measure only "CLASS" scope. And it looks like you are trying to do the same.

The excludes are relative to the elements in the scope. Not sure what it means for "BUNDLE", but I am almost inclined to think it's either all or nothing. Also the excludes use different type of wildcard. Try changing your configuration to use element "CLASS" (or "PACKAGE").

violationRules {
    rule {
        element = 'CLASS'
        excludes = ['com.jacoco.dto.*']
        limit {
            counter = 'BRANCH'
            minimum = 0.8
        }
    }
}

check.dependsOn jacocoTestCoverageVerification

Tags:

Gradle

Jacoco