3

I'm trying to exclude from jacoco coverage an entire package, but it's not working.
I've followed the instructions reported in this answer Filter JaCoCo coverage reports with Gradle , but it seems that it is not working for me. This is the code:

jacocoTestReport {
  reports {
    xml.enabled false
    csv.enabled false
    html.destination "${buildDir}/jacocoHtml"
  }
  afterEvaluate {
    classDirectories = files(classDirectories.files.collect {
        fileTree(dir: it,
                exclude: ['it.unibo.distributedBLS.main/**'])
    })
  }
}

I've also tried variations like excludes +=, writing

'**/it.unibo.distributedBLS.main/**'

and also by adding

test {
  jacoco {
    excludes += ['it.unibo.distributedBLS.main/**',     
  }
}

but still not working.
One thing not clear to me it's what "dir: it" stands for: what should I put there? Could it be that the error?

Community
  • 1
  • 1
Usr
  • 2,628
  • 10
  • 51
  • 91
  • No, `dir: it` is fine. It means that each collection directory is passed as the first parameter to fileTree. – Paul Hicks Nov 08 '16 at 01:25
  • Are you getting results for the not-excluded packages? Or are they all 0%? The answer you linked to suggests that `afterEvaluate { classDirectories = files(classDirectories.files.collect { fileTree(dir: it, exclude: ['**/it.unibo.distributedBLS.main/**']) })}` should work for removing the results from the html, and `jacoco { excludes += ['**/it.unibo.distributedBLS.main/**' ]}` should prevent the tests from being run. You need both, and you need the pattern `**/it.unibo.distributedBLS.main/**` – Paul Hicks Nov 08 '16 at 01:35

2 Answers2

3

Here is what worked for me (at least for the html, not for the xml report). I came across it by accident while making little tweaks and re-running my build. It kind of makes sense now but not very intuitive.

Pay attention to how I define my packages in test.jacoco vs excluding them from the actual report in the jacocoTestReport.afterEvaluate

test {
    finalizedBy jacocoTestReport
    jacoco {
        excludes += ["**/com.mypackage.things/**","**/com.mypackage.entity/**"]
    }
}

jacocoTestReport {
    reports {
        xml.enabled true
        csv.enabled false
        html.enabled false
        xml.destination "${buildDir}/reports/jacoco"
    }

    afterEvaluate {
        classDirectories = files(classDirectories.files.collect {
            fileTree(dir: it,
                exclude: ['**/com/mypackage/things/**',
                          '**/com/mypackage/entity/**'])
        })
    }
}

I generate XML for Sonarqube but I switched it temporarily to HTML as I was re-running to see if the packages were removed. At some point I noticed the com.mypackage.entity was being excluded but the com.mypackage.things wasn't. That's when I realized I had defined the entity package in the report with with /'s but was using .'s for the things package.

For the test section it makes more sense to use the dot notation where as in the report section, since the report is probably generated in some sort of directory structure for html it makes sense to use the slash notation. Not sure how to fix it yet for XML. I guess some kind of other notation???

Justin
  • 859
  • 4
  • 15
  • 30
  • Follow up: it appears the packages are excluded in the XML. So I guess it's Sonar that needs to know where to find that information. Guess it works for both! – Justin Jun 30 '17 at 18:19
3

if you are getting the following error

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

use this one

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')})
    )
saif
  • 1,182
  • 14
  • 27