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

I need to check the minimum coverage with the new jacoco task
jacocoTestCoverageVerification
This task is available with in the 3.4.1 gradle release and with the jacoco plugin >= 0.6.3
I could run another task that generates an html report with the branch coverage but now I want to use that number to make the build fail.
This is my code
buildscript {
ext {
....
}
repositories {
mavenCentral()
maven {
....
}
}
dependencies {
.....
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'jacoco'
jar {
baseName = "coverage-test"
}
dependencies {
// my dependencies
}
eclipse {
classpath {
containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER')
containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8'
}
}
wrapper {
gradleVersion = '3.4.1'
}
jacoco {
toolVersion = '0.7.9'
}
jacocoTestReport {
reports {
xml.enabled false
csv.enabled false
}
group = "Reporting"
description = "Generate Jacoco coverage reports after running tests."
additionalSourceDirs = files(sourceSets.main.allJava.srcDirs)
afterEvaluate {
classDirectories = files(classDirectories.files.collect {
fileTree(
dir: it,
excludes:
[
'com/jacoco/dto/**',
'com/jacoco/configs/**',
//and others
])
})
}
}
jacocoTestCoverageVerification {
//I tried this and it didn't work
// classDirectories = files(classDirectories.files.collect {
// fileTree(
// dir: it,
// excludes:
// [
// 'com/jacoco/dto/**',
// 'com/jacoco/configs/**',
// //and others
// ])
// })
violationRules {
rule {
//Also tried this and it didn't work
// excludes = ['com/jacoco/dto/**', ...]
limit {
counter = 'BRANCH'
minimum = 0.8
}
}
}
}
check.dependsOn jacocoTestCoverageVerification
With classDirectories I get the following error Cannot get property 'files' on null object. And with the second option (only excludes), the build run smoothly but It doesn't exclude any class.

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

Related

jacocoRootReport only shows coverage from last project of multi-project gradle build

I'm upgrading my Gradle 4 multi-project setup to Gradle 6. I've followed instructions here:
https://stackoverflow.com/a/56181389
and I've bumped the jacoco version to 0.8.5. Right now the only problem is that the human-readable coverage report seems to be missing most of the coverage data that it was showing under the old version. It seems that the coverage report is only reflecting the last (most recently tested) project. It used to work fine under Gradle 4. I'm using Java 8.
I ran the gradle build using --debug, and I notice that the test.exec file is deleted repeatedly, once for each subproject that has tests. I think this is the problem, but I don't know how to prevent deletion of this file.
2020-04-16T09:16:21.048-0600 [DEBUG] [org.gradle.internal.file.impl.DefaultDeleter] Deleting /Users/bishop/dev/aep/edge-profile-lookup-target/build/jacoco/test.exec
Can someone please help me fix this so that all of the coverage (from each of the tests which ran against each of the sub projects) appear in a single coverage report?
Here are the parts of the main build.gradle file that seem relevant:
buildscript {
ext {
jacocoVersion = '0.8.5'
...
}
...
}
allprojects {
...
apply plugin: 'jacoco'
...
}
subprojects {
tasks.withType(Test) {
// redirect all coverage data to one file
jacoco {
destinationFile = file("$rootProject.buildDir/jacoco/test.exec")
}
}
jacoco {
toolVersion = jacocoVersion
}
jacocoTestReport {
additionalSourceDirs.from = files(sourceSets.main.allSource.srcDirs)
sourceDirectories.from = files(sourceSets.main.allSource.srcDirs)
classDirectories.from = files(sourceSets.main.output.collect {
fileTree(dir: it, exclude: project.properties['BUILD_COVERAGE_EXCLUSIONS'].tokenize(','))
})
reports {
html.enabled = true
xml.enabled = true
csv.enabled = false
}
}
}
task jacocoRootReport(type: JacocoReport) {
dependsOn = subprojects.test
additionalSourceDirs.from = files(subprojects.sourceSets.main.allSource.srcDirs)
sourceDirectories.from = files(subprojects.sourceSets.main.allSource.srcDirs)
classDirectories.from = files(subprojects.jacocoTestReport.classDirectories)
executionData.from = files(subprojects.jacocoTestReport.executionData)
reports {
html.enabled = true
xml.enabled = true
csv.enabled = false
}
onlyIf = {
true
}
doFirst {
executionData.from = files(executionData.findAll {
it.exists()
})
}
}
...
apply plugin: 'jacoco'
configurations.create("jacoco")
configurations {
jacoco
}
dependencies {
jacocoAnt group: 'org.jacoco', name: 'org.jacoco.ant', version: jacocoVersion
jacocoAnt group: 'org.ow2.asm', name: 'asm', version: asmVersion
}
task copyRuntimeLibs(type: Copy) {
from configurations.jacoco
into "$rootProject.buildDir/libs"
}
build.dependsOn copyRuntimeLibs
The following:
jacoco {
destinationFile = file("$rootProject.buildDir/jacoco/test.exec")
}
configures Jacoco to always use the same output file.
So the issue is most likely that more recent Gradle versions work on separating colliding outputs.
I would recommend looking at the recently published sample on how to setup Jacoco in a multi project instead of attempting to rely on colliding outputs.

Gradle Multi-Project Integration Tests with Junit5

How can I create integrations tests using Gradle 5.2 and JUnit 5.3 in a multi-project build file?
This is my current build file:
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "gradle.plugin.com.github.spotbugs:spotbugs-gradle-plugin:1.6.10"
classpath 'org.owasp:dependency-check-gradle:5.0.0-M2'
}
}
allprojects {
defaultTasks 'clean', 'build', 'publish', 'installDist'
group = 'coyote'
version = '0.1.0'
}
subprojects {
apply plugin: 'java'
apply plugin: 'jacoco'
apply plugin: "com.github.spotbugs"
apply plugin: 'org.owasp.dependencycheck'
apply plugin: 'maven-publish'
apply plugin: 'application'
apply plugin: 'eclipse'
apply plugin: 'idea'
repositories {
jcenter()
mavenCentral()
}
dependencies {
testImplementation ("org.junit.jupiter:junit-jupiter-api:5.3.2")
testRuntimeOnly ("org.junit.jupiter:junit-jupiter-engine:5.3.2")
spotbugsPlugins ("com.h3xstream.findsecbugs:findsecbugs-plugin:1.8.0")
}
tasks.withType(Test) {
useJUnitPlatform()
}
apply from: "$rootDir/integration-test.gradle"
check.dependsOn jacocoTestCoverageVerification
check.dependsOn dependencyCheckAnalyze
spotbugs {
effort = "max"
reportLevel = "low"
ignoreFailures = true
showProgress = true
}
jacocoTestReport {
reports {
xml.enabled true
html.enabled true
}
}
check.dependsOn jacocoTestReport
tasks.withType(com.github.spotbugs.SpotBugsTask) {
reports {
xml.enabled false
html.enabled true
}
}
}
The integration-test.gradle file I am applying on line 46 contains the following:
sourceSets {
itest {
compileClasspath += sourceSets.main.output + configurations.testCompile
runtimeClasspath += output + compileClasspath + configurations.testRuntime
}
}
idea {
module {
testSourceDirs += sourceSets.itest.java.srcDirs
testResourceDirs += sourceSets.itest.resources.srcDirs
scopes.TEST.plus += [configurations.itestCompile]
}
}
task itest(type: Test) {
description = 'Runs the integration tests.'
group = 'verification'
testClassesDirs = sourceSets.itest.output.classesDirs
classpath = sourceSets.itest.runtimeClasspath
outputs.upToDateWhen { false }
}
Everything seems to work well within Intellij, but JUnit5 is not being added to the classpath, so any tests in the itest directories cannot find the JUnit libraries.
Running gradlew itest fails with similar results with the JUnit classes not being found.
I have tried to add use useJUnitPlatform() directly in the itest task, but with no success. I have also tried placing everything in the build.gradle file with no success.
Eventually, I'd like to use the same pattern to define load and security testing, running them separately as part of a CI/CD pipeline so placing everything neatly in their own directories under their respective projects is preferred to mixing everything in one test directory and using tags.
This is also helping other teams model CI/CD practices, so using Gradle 5 and JUnit 5 as designed is preferred as opposed to work-arounds or hacks to make things work. It's acceptable to learn that these versions don't work together or that there are currently defects\issues preventing this approach. Hopefully this is not the issue and I'm just missing something simple.
The solution is I needed to include the JUnit 5 dependencies myself. The following is the correct integration-test.gradle file:
sourceSets {
itest {
compileClasspath += sourceSets.main.output + configurations.testCompile
runtimeClasspath += output + compileClasspath + configurations.testRuntime
}
}
idea {
module {
testSourceDirs += sourceSets.itest.java.srcDirs
testResourceDirs += sourceSets.itest.resources.srcDirs
scopes.TEST.plus += [configurations.itestCompile]
}
}
dependencies {
itestImplementation ("org.junit.jupiter:junit-jupiter-api:5.3.2")
itestRuntimeOnly ("org.junit.jupiter:junit-jupiter-engine:5.3.2")
}
task itest(type: Test) {
description = 'Runs the integration tests.'
group = 'verification'
testClassesDirs = sourceSets.itest.output.classesDirs
classpath = sourceSets.itest.runtimeClasspath
outputs.upToDateWhen { false }
}

Could not find method jacocoTestCoverageVerification()

I want to make jacoco plugin to fail when test coverage is not enough. I use example from gradle page :
https://docs.gradle.org/current/userguide/jacoco_plugin.html
apply plugin: 'jacoco'
jacoco {
toolVersion = "0.7.6.201602180812"
reportsDir = file("output/jacoco/customJacocoReportDir")
}
jacocoTestReport {
reports {
xml.enabled false
csv.enabled false
html.enabled true
html.destination file("output/jacoco/jacocoHtml")
}
}
jacocoTestCoverageVerification {
violationRules {
rule {
limit {
minimum = 0.5
}
}
rule {
enabled = false
element = 'CLASS'
includes = ['org.gradle.*']
limit {
counter = 'LINE'
value = 'TOTALCOUNT'
maximum = 0.3
}
}
}
}
However, I get error:
Could not find method jacocoTestCoverageVerification() for arguments [build_4v41fim1xdl76q49oxk7mnylv$_run_closure6#18601994] on root project 'demo' of type org.gradle.api.Project.
Can anyone advise?
According to the the docs for Gradle version 3.4 or higher :
For projects that also apply the Java Plugin, The JaCoCo plugin automatically adds the following tasks:
jacocoTestReport [...]
jacocoTestCoverageVerification [...]
If the code block in your question shows your full build.gradle file, this would mean that you need to add the line that applies the Java Plugin (apply plugin: 'java').
Of course, you could also add a task of type JacocoCoverageVerification called jacocoTestCoverageVerification to your build file on your own:
task jacocoTestCoverageVerification(type: JacocoCoverageVerification) {
// configuration
}
Try using latest version of gradle. This may solve your problem

Excluding test classes for all analysis from gradle

I'm invoking sonar using sonar-runner plugin from gradle. I'm also using the reuse reports flag.
How can I exclude all test classes from all analyses (Checkstyle, Findbugs, Coverage)?
I'm currently using the following plugin configuration:
sonarRunner {
sonarProperties {
property "sonar.host.url", "<HOST>"
property "sonar.scm.disabled", "true"
property "sonar.login", "<USER>"
property "sonar.password", "<password>"
property "sonar.sources", "src"
property "sonar.exclusions", "**/test/**/*.java"
property "sonar.projectVersion", project.releaseDisplayName
// these should not change anything as sonar uses the defaults set for gradle
//property "sonar.tests", "test"
}
My source sets are as follows:
sourceSets {
main {
java {
srcDir 'src'
srcDir 'src-gen'
}
}
test {
java { srcDir 'test' }
}
Thanks
Try this:
jacocoTestReport {
afterEvaluate {
sourceDirectories = files(sourceDirectories.files.collect {
fileTree(dir: it, exclude: [ 'com/path/to/package/that/I/want/to/exclude/are/inside/thisfolder_or_dto/**' ])
})
classDirectories = files(classDirectories.files.collect {
fileTree(dir: it, exclude: [ 'com/path/to/package/that/I/want/to/exclude/are/inside/thisfolder_or_dto/**' ])
})
}
}
sonarRunner {
sonarProperties {
property "sonar.exclusions", "com/path/to/package/that/I/want/to/exclude/are/inside/thisfolder_or_dto/**"
}
}
//Required with Gradle 2.0+ -- 2.0+ -- 2.3
pmd {
ruleSets = ["java-basic", "java-braces", "java-design" ]
ignoreFailures = true
}
codenarc {
ignoreFailures = true
//The following file should exist or build will fail, you can find one online a sample version
configFile = file("config/codenarc/codenarc.xml")
}
checkstyle {
configFile = new File(rootDir, "config/checkstyle.xml")
ignoreFailures = true
//sourceSets = [sourceSets.main, sourceSets.test, sourceSets.integrationTest]
//Just run checkstyle only on main source code
sourceSets = [sourceSets.main]
}
findbugs {
ignoreFailures = true
//Just run findbugs only on main source code
sourceSets = [sourceSets.main]
//You can use if statement in groovy to set which toolVersion 2.0.3 or 3.0.1 depending upon JAVA version used in the project
toolVersion = "3.0.1"
}
Similarly, you can use excludes property within test or test task's jacoco section directly.
def generatedSources = ['com/yahoo/**', 'com/amazon/**']
test {
jacoco {
excludes = generatedSources
}
}
jacocoTestReport {
doFirst {
classDirectories = fileTree(dir: "${buildDir}/classes/main/").exclude(generatedSources)
}
reports {
xml.enabled true
}
}
While publishing to SonarQube (sonar.exclusions=value should be relative from your WORKSPACE i.e. src/java/com/.../...)
sonarRunner plugin is deprecated. Please switch to the official SonarQube plugin: https://plugins.gradle.org/plugin/org.sonarqube

QueryDSL, spring-boot & Gradle

I was hoping to bring querydsl into my spring-boot project via gradle. Despite finding a couple of examples online, none of them actually work for me because of issues with dependencies (I think). According to the QueryDSL support forum, gradle is not supported yet. But I was wondering with all the gradle & spring-boot being created if someone has managed to make it work yet?
Here is my build.gradle:
apply plugin: 'java'
apply plugin: 'groovy'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'jacoco'
apply plugin: 'war'
buildscript {
repositories {
maven { url "http://repo.spring.io/libs-snapshot" }
mavenLocal()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.0.0.RC4")
}
}
repositories {
mavenCentral()
maven { url: "http://repo.spring.io/libs-snapshot" }
// maven { url: "http://repo.spring.io/milestone" }
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-web:1.0.0.RC5")
compile("org.springframework.boot:spring-boot-starter-data-jpa:1.0.0.RC5")
compile("org.springframework:spring-orm:4.0.0.RC1")
compile("org.hibernate:hibernate-entitymanager:4.2.1.Final")
compile("com.h2database:h2:1.3.172")
compile("joda-time:joda-time:2.3")
compile("org.thymeleaf:thymeleaf-spring4")
compile("org.codehaus.groovy.modules.http-builder:http-builder:0.7.1")
compile('org.codehaus.groovy:groovy-all:2.2.1')
compile('org.jadira.usertype:usertype.jodatime:2.0.1')
// this line fails
querydslapt "com.mysema.querydsl:querydsl-apt:3.3.2"
testCompile('org.spockframework:spock-core:0.7-groovy-2.0') {
exclude group: 'org.codehaus.groovy', module: 'groovy-all'
}
testCompile('org.codehaus.groovy.modules.http-builder:http-builder:0.7+')
testCompile("junit:junit")
}
jacocoTestReport {
group = "Reporting"
description = "Generate Jacoco coverage reports after running tests."
}
task wrapper(type: Wrapper) {
gradleVersion = '1.11'
}
sourceSets {
main {
generated {
java {
srcDirs = ['src/main/generated']
}
}
java {
srcDirs = []
}
groovy {
srcDirs = ['src/main/groovy', 'src/main/java']
}
resources {
srcDirs = ['src/main/resources']
}
output.resourcesDir = "build/classes/main"
}
test {
java {
srcDirs = []
}
groovy {
srcDirs = ['src/test/groovy', 'src/test/java']
}
resources {
srcDirs = ['src/test/resources']
}
output.resourcesDir = "build/classes/test"
}
}
configurations {
// not really sure what this is, I see it in examples but not in documentation
querydslapt
}
task generateQueryDSL(type: JavaCompile, group: 'build', description: 'Generates the QueryDSL query types') {
source = sourceSets.main.java
classpath = configurations.compile + configurations.querydslapt
options.compilerArgs = [
"-proc:only",
"-processor", "com.mysema.query.apt.jpa.JPAAnnotationProcessor"
]
destinationDir = sourceSets.generated.java.srcDirs.iterator().next()
}
compileJava {
dependsOn generateQueryDSL
source generateQueryDSL.destinationDir
}
compileGeneratedJava {
dependsOn generateQueryDSL
options.warnings = false
classpath += sourceSets.main.runtimeClasspath
}
clean {
delete sourceSets.generated.java.srcDirs
}
idea {
module {
sourceDirs += file('src/main/generated')
}
}
But gradle fails with:
Could not find method querydslapt() for arguments [com.mysema.querydsl:querydsl-apt:3.3.2]
I have tried changing the querydsl-apt version to earlier ones but I get the same error.
Working configuration for Spring Boot 1.3.5 and supported QueryDSL, tested with gradle 2.14.
ext {
queryDslVersion = '3.6.3'
javaGeneratedSources = file("$buildDir/generated-sources/java")
}
compileJava {
doFirst {
javaGeneratedSources.mkdirs()
}
options.compilerArgs += [
'-parameters', '-s', javaGeneratedSources
]
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile "com.mysema.querydsl:querydsl-jpa:$queryDslVersion"
compileOnly "com.mysema.querydsl:querydsl-apt:$queryDslVersion:jpa"
}
Complete project source code: spring-boot-querydsl
You probably need to do at least 2 things:
Declare the "querydslapt" configuration before you use it
Add querydsl-jpa (or whatever flavours you need) to your "compile" configuration.
Then you will have the classpath set up, but the apt bit will not do anything without some more configuration (as you found no doubt from the querydsl support forum). The apt but is used to generate some code that you then need to compile and use in your application code (the "Q*" classes corresponding to your domain objects). You could drive that from a build task in gradle I imagine (it only has to run once for every change in the domain objects).

Resources