Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclusions not working with Jacoco Gradle plugin

I'm working on setting up the Jacoco Gradle plugin for my project. The plugin is executing fine and generating the coverage report but it's not excluding packages that I would excluded.

Classes I would like to include are in com/xxx/ws/hn/** and classes I would like excluded are in com/xxx/ws/enterprise/**.

Here's my Gradle script:

apply plugin: "jacoco"

jacoco {
    toolVersion = "0.6.2.201302030002"
    reportsDir = file("$buildDir/reports/jacoco")
}

test {
    jacoco{
        excludes = ["com/xx/ws/enterprise/**/*"]
        includes = ["com/xxx/ws/hn/**/*"]
        append = false
    }
}

jacocoTestReport {
    dependsOn test
    description = "Generate Jacoco coverage reports after running tests."
    executionData = files('build/jacoco/test.exec')
    reports {
        xml.enabled true
        html.enabled true
    }
}

Am I missing anything here? I've tried various patterns of exclusions including a '.' delimiter for packages instead of a '/' but nothing seems to work. Any help would be greatly appreciated.

like image 904
ntaylor2 Avatar asked Nov 05 '13 14:11

ntaylor2


2 Answers

I have not verified this but when I read the JaCoCo documentation, it states that wildcards can be used but all examples only have ONE *, not two as your example shows.

http://www.eclemma.org/jacoco/trunk/doc/agent.html

like image 176
Jocce Nilsson Avatar answered Nov 04 '22 09:11

Jocce Nilsson


To solidify (and verify) Joachim's answer, I came across this SO post and found that specifying canonical class names, classpath style (with slashes, not periods), with single wildcard characters actually worked well. For example, here is my exclude list that works in my build, as defined as a list in a separate gradle file:

def testExcl = [
    'android/*',
    'com/android/*',
    'com/nativelibs4java/*',
    'com/ochafik/*',
    'com/fasterxml/*',
    'com/esotericsoftware/*',
    'com/google/*',
    'com/lmax/*',
    'com/sun/*',
    'jdk/*',
    'mockit/*',
    'org/apache/*',
    'org/bridj/*',
    'org/gradle/*',
    'org/hamcrest/*',
    'org/junit/*',
    'org/slf4j/*',
    'sun/*',
    'worker/*',
    '*Test',
    '*TestIntegration',
    '*AllTests',
    '*Suite'
]

I tried probably 10 or 12 other ways of formatting the class names, and this is the only way that actually worked. I don't know why something like this that is so simple/fundamental is so ungoogleable and difficult to find a definitive answer on in the context of Gradle (i.e. outside JaCoCo's official documentation, where no documentation exists about Gradle integration, but Ant and Maven integration docs are there).

Hopefully this helps if others are flailing trying to get JaCoCo to exclude classes.

UPDATE: This changed in Gradle 7 (I went from 5.6.1 to 7.1.1, so not sure about Gradle 6) and no longer works. For Gradle 7 you need to replace / with .; here are my updated exclusions:

def testExcl = [
    'android.*',
    'com.android.*',
    'com.nativelibs4java.*',
    'com.ochafik.*',
    'com.fasterxml.*',
    'com.esotericsoftware.*',
    'com.google.*',
    'com.lmax.*',
    'com.sun.*',
    'jdk.*',
    'mockit.*',
    'org.apache.*',
    'org.bridj.*',
    'org.gcpud.common.*',
    'org.gcpud.testutil.*',
    'org.gradle.*',
    'org.hamcrest.*',
    'org.junit.*',
    'org.slf4j.*',
    'sun.*',
    'worker.*',
    '*Test',
    '*Test$*',
    '*TestIntegration',
    '*AllTests',
    '*Suite'
]

Also see the following SO post for other means of excluding classes from reporting (exclude from reporting, not coverage itself):

Filter JaCoCo coverage reports with Gradle

like image 21
jhyry-gcpud Avatar answered Nov 04 '22 08:11

jhyry-gcpud