The place for apps & thoughts

Kotlin Gradle build set Jacoco to ignore a package/class

It took me some time to figure out how to do it so I thought it could be helpful to share it.

So, I finally moved to Gradle for building the JDK Applications (because they are less and less in pure Java, as I convince people at work to switch to Kotlin) I’m working on. I’m a big fan of Gradle, I always found dumb that as a programmer I can write code to do stuff but when it comes to build I have to just use declarative xml (I don’t think much better about CI/CD pipelines in yaml). Anyway, so I’m still discovering Gradle, and to make it even more fun, as Kotlin DSL become a first party citizen, I skipped learning the Groovy DSL and jumped full in Kotlin. The problem is that most examples you find are in Groovy and trying to adapt them to Kotlin is not that obvious. And to spike it up, Gradle 5 made some big changes so a lot of examples in Gradle 4 don’t work the same way.

The problem

I wanted to configure Jacoco (0.8.3) using Gradle (5.4) Kotlin DSL to not count a specific package in the coverage % count. I got some tools classes I don’t want to loose time to run tests on them as they’ll never end in production.

The solution

It’s close to the Groovy version except I had to figure out that the afterEvaluate must be called on project, outside the task (the versions I found it was inside the jacocoTestReport task, which does not work in this version of Gradle (or its Kotlin DSL)).

As Venkat says, I hope you found that useful.