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
Related
Gradle Version: 6.1.1
Android Gradle Plugin: 4.0.0
When trying to run my jacoco coverage it does not appear to be taking into account the version. When I look at the HTML report it states "Created with JaCoCo 0.7.9.201702052155" despite me having toolVersion = "0.8.5" in my setup. The report also does not have fixes that I expect in 0.8.3 relating to Kotlin as a secondary confirmation this isn't working.
apply plugin: 'jacoco'
jacoco {
toolVersion = "0.8.5"
reportsDir = file("$buildDir/reports")
}
tasks.withType(Test) {
jacoco.includeNoLocationClasses = true
jacoco.excludes = ['jdk.internal.*']
}
project.afterEvaluate {
(android.hasProperty('applicationVariants')
? android.'applicationVariants'
: android.'libraryVariants').all { variant ->
def variantName = variant.name
def unitTestTask = "test${variantName.capitalize()}UnitTest"
def uiTestCoverageTask = "create${variantName.capitalize()}CoverageReport"
tasks.create(name: "${unitTestTask}Coverage", type: JacocoReport, dependsOn: [
"$unitTestTask",
"$uiTestCoverageTask"
]) {
group = "Reporting"
description = "Generate Jacoco coverage reports for the ${variantName.capitalize()} build"
reports {
html.enabled = true
xml.enabled = true
csv.enabled = false
}
def fileFilter = [
'**/R.class',
'**/R$*.class',
'**/BuildConfig.*',
'**/Manifest*.*',
'**/*Test*.*',
'**/com/example/databinding/*',
'**/com/example/generated/callback/*',
'**/android/databinding/*',
'**/androidx/databinding/*',
'**/di/module/*',
'**/*MapperImpl*.*',
'**/*$ViewInjector*.*',
'**/*$ViewBinder*.*',
'**/BuildConfig.*',
'**/*Component*.*',
'**/*BR*.*',
'**/Manifest*.*',
'**/*$Lambda$*.*',
'**/*Companion*.*',
'**/*Module.*',
'**/*Dagger*.*',
'**/*MembersInjector*.*',
'**/*_Factory*.*',
'**/*_Provide*Factory*.*',
'**/*Extensions*.*',
'**/*$Result.*', /* filtering `sealed` and `data` classes */
'**/*$Result$*.*'/* filtering `sealed` and `data` classes */
]
classDirectories.setFrom(files([
fileTree(dir: "${buildDir}/tmp/kotlin-classes/${variantName}", excludes: fileFilter)
]))
def coverageSourceDirs = [
"$project.rootDir/app/src/main/java",
"$project.projectDir/src/${variantName}/java"
]
additionalSourceDirs.setFrom(files(coverageSourceDirs))
sourceDirectories.setFrom(files(coverageSourceDirs))
def uiTestsData = fileTree(dir: "${buildDir}/outputs/code_coverage/${variantName}AndroidTest/connected/", includes: ["**/*.ec"])
executionData(files([
"$project.buildDir/jacoco/${unitTestTask}.exec",
uiTestsData
]))
}
}
}
As stated from #Bilgehan KALKAN here, as of Gradle 7, this seems to be resolved.
android {
//Other configurations...
testCoverage.jacocoVersion = "0.8.7"
}
For lower gradle version try adding this to your Project Gradle file.
subprojects {
configurations.all {
resolutionStrategy {
eachDependency { details ->
if ('org.jacoco' == details.requested.group) {
details.useVersion "0.8.6"
}
}
}
}
}
You don't need to override version as mentioned on #LethalMaus's answer. You only need to specify jacoco version under android as testCoverage.jacocoVersion, jacoco.version approach on #Alexy Nikitin's answer has been deprecated and replaced with testCoverage.jacocoVersion.
If you do not specify any version, it will use DEFAULT_VERSION under JacocoOptions class. Which is "0.8.3" for Android Gradle Plugin version 7.0.2.
You should declare jacoco version in the android gradle plugin too:
android {
jacoco {
version "<your jacoco version>"
}
}
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.
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
I have tried to get code coverage in a spring-gradle project using gradle jacoco plugin.
The build.gradle contains the following
apply plugin: "jacoco"
jacoco {
toolVersion = "0.7.1.201405082137"
reportsDir = file("$buildDir/customJacocoReportDir")
}
jacocoTestReport {
reports {
xml.enabled false
csv.enabled false
html.destination "${buildDir}/jacocoHtml"
}
}
I then ran
gradle test jacocoTestReport
Where after only the file test.exec is generated in build/reports folder.
Other than that nothing happens.
How can I get the HTML report?
You don't have to configure reportsDir/destinationFile
Because jacoco has default values for them.
build.gradle:
plugins {
id 'java'
id 'jacoco'
}
jacocoTestReport {
reports {
xml.enabled true
html.enabled true
csv.enabled true
}
}
repositories {
jcenter()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
}
Run gradle test jacocoTestReport
You can find the test report in ./build/reports/jacoco/test directory.
HTML output is in ./build/reports/jacoco/test/html directory.
Following helped . its in samples/testing/jacaco of gradle-2.3-all.zip from https://gradle.org/releases/
apply plugin: "java"
apply plugin: "jacoco"
jacoco {
toolVersion = "0.7.1.201405082137"
reportsDir = file("$buildDir/customJacocoReportDir")
}
repositories {
mavenCentral()
}
dependencies {
testCompile "junit:junit:4.+"
}
test {
jacoco {
append = false
destinationFile = file("$buildDir/jacoco/jacocoTest.exec")
classDumpFile = file("$buildDir/jacoco/classpathdumps")
}
}
jacocoTestReport {
reports {
xml.enabled false
csv.enabled false
html.destination "${buildDir}/jacocoHtml"
}
}
Unfortunately, none of these answers worked for me.
I had a similar issue.
Only different in not having the exec file generated.
And because of that ,
I found that the jacocoTestReport was simply "skipped".
I got it fixed by adding :
apply plugin: 'jacoco'
test {
useJUnitPlatform()
finalizedBy jacocoTestReport // report is always generated after tests run
}
jacocoTestReport {
...
...
...
...
}
That's because I'm using Junit5 with spring boot 2.X
subprojects {
apply(plugin: 'org.jetbrains.kotlin.jvm')
repositories {
jcenter()
mavenCentral()
}
}
task codeCoverageReport(type: JacocoReport) {
// Gather execution data from all subprojects
executionData fileTree(project.rootDir.absolutePath).include("**/build/jacoco/*.exec")
// Add all relevant sourcesets from the subprojects
subprojects.each {
sourceSets it.sourceSets.main
}
reports {
xml.enabled true
html.enabled true
csv.enabled false
}
}
// always run the tests before generating the report
codeCoverageReport.dependsOn {
subprojects*.test
}
sonarqube {
properties {
property "sonar.projectKey", "your_project_key"
property "sonar.verbose", true
property "sonar.projectName", "Your project name"
property "sonar.coverage.jacoco.xmlReportPaths", "${rootDir}/build/reports/jacoco/codeCoverageReport/codeCoverageReport.xml"
}
}
Command to run test with coverage:
./gradlew codeCoverageReport
./gradlew sonarqube -x test (test is excluded since already run and sonarqube by default executes test)
The second command can be ignored if sonarqube is not being used.
Two things to be noted that made it work:
To make available sourcesets of all modules, looping over subprojects and accumulating sourcesets worked. subprojects.sourceSets.main.allSource.srcDirs did not work.
sonar.jacoco.reportPaths is deprecated. We need to use sonar.coverage.jacoco.xmlReportPaths. Check the documentation here
I am trying to configure findbugs in my project with
findbugs {
ignoreFailures = true
reports {
html { enabled = true }
xml.enabled = !html.enabled
}
}
but an error appears
Could not find method reports() for arguments
[quality_4gppo4hjtn3ur86ac71a18pai6$_run_closure2_closure4#6651ccf]
on root project 'Project'.
This code was used in one of my previous projects with Gradle 1.7 and it was working.
You can use the reports method on a FindBugs task. The findbugs plugin creates one for every source set. So if you want to use FindBugs on your main classes, you would use
findbugsMain {
ignoreFailures = true
reports {
html { enabled = true }
xml.enabled = !html.enabled
}
}
If you want to configure all the findbugs tasks the same way, then you can simply apply the same configuration to all of them:
tasks.withType(FindBugs) {
ignoreFailures = true
reports {
html { enabled = true }
xml.enabled = !html.enabled
}
}