Filter JaCoCo coverage reports with Gradle
Thanks to, Yannick Welsch
:
After searching Google, reading the Gradle docs and going through older StackOverflow posts, I found this answer on the Official gradle 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 useclassDirectories = 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
JaCoCo gradle plugin exclude
Try something like this:
excludes: ['**/Dao*.class']
But as I understand, this will exclude the class from jacoco but the Report that Jacoco creates will show you "0% of coverage":
Gradle issue: https://issues.gradle.org/browse/GRADLE-2955
JaCoCo Gradle - Exclude classes & override Includes
You can use filter
on fileTree
.
For example for
src/main/java/View.java
class View {}
src/main/java/ViewModel.java
class ViewModel {}
src/main/java/Example.java
class Example {}
src/test/java/ExampleTest.java
import org.junit.Test;
public class ExampleTest {
@Test
public final void test() {
}
}
and build.gradle
apply plugin: 'java'
apply plugin: 'jacoco'
repositories {
mavenCentral()
}
dependencies {
testImplementation 'junit:junit:4.13'
}
jacocoTestReport {
classDirectories.setFrom(
fileTree(dir: "build/classes/java/main")
.filter({file -> !file.name.contains('View') || file.name.contains('ViewModel')})
)
}
Execution of
gradle clean test jacocoTestReport
using Gradle 6.2.1 will generate the following report in directory build/reports/jacoco/test/html/
that doesn't contain View
, but contains ViewModel
and Example
:
Related Topics
How to Calculate a Time Span in Java and Format the Output
How to Setsize of Image Using Rescaleop
When to Use Entitymanager.Find() VS Entitymanager.Getreference() with JPA
How Is Hashcode() Calculated in Java
Efficient Swapping of Elements of an Array in Java
How to Get Milliseconds from Localdatetime in Java 8
How to Refer Environment Variable in Pom.Xml
How to Move a File from One Location to Another in Java
Read Whole Text Files from a Compression in Spark
Jprogressbar Isn't Progressing
Compile-Time Constants and Variables
Why Do Many Collection Classes in Java Extend the Abstract Class and Implement the Interface as Well
How to Simulate Keyboard Presses in Java
The Method Getdispatchertype() Is Undefined for the Type Httpservletrequest
How to Customize Parameter Names When Binding Spring MVC Command Objects