I'm attempting to create some exclude patterns for a PMD task in Gradle.
My task is generated in the next way:
/* Allows generation of pmd config */
allprojects {
apply plugin: 'pmd'
}
gradle.projectsEvaluated {
subprojects.each() { project ->
if (project.hasProperty('android')) {
project.task("runPmd", type: Pmd) {
description "Run pmd"
group 'verification'
source = fileTree("${project.projectDir}/src/main/java")
ruleSetFiles = files("${project.rootDir}/build-tools/pmd.xml")
ignoreFailures = true
reports {
xml.enabled = true
html.enabled = true
}
}
}
}
}
And the ruleSet is:
<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="MyCompany ruleset"
xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">
<description>
MyCompany ruleset for Android PMD
</description>
<exclude-pattern>.*/org/jivesoftware/.*</exclude-pattern>
<exclude-pattern>.*/net/java/.*</exclude-pattern>
...rules...
</ruleset>
But in my reports, I'm getting:
Am I doing something wrong? Checking this answer seems that I'm defining the exclude-pattern right, but pmd is analyzing those files.
I was running into the same issue and adding an empty ruleSets [] property seemed to fix it for me.
Make sure to define the rules that you actually want to apply in your ruleset file - i.e. Move them from the ruleSet property block to the file (if you had any there).
This is what my task generation looks like:
// PMD
afterEvaluate {
def variants = plugins.hasPlugin('com.android.application') ?
android.applicationVariants : android.libraryVariants
variants.each { variant ->
def task = tasks.create("pmd${variant.name.capitalize()}", Pmd)
task.group = 'verification'
task.description = "Run PMD for the ${variant.description}."
task.ruleSetFiles = files("pmd-ruleset.xml")
task.ruleSets = []
task.reports {
xml.enabled = false
html.enabled = true
}
def variantCompile = variant.javaCompile
task.source = variantCompile.source
task.dependsOn(variantCompile)
tasks.getByName('check').dependsOn(task)
}
}
I got the hint from this thread:
http://sourceforge.net/p/pmd/discussion/188193/thread/6e9c6017/
Add below snippet in your build.gradle, you can exclude Classes and packages also
pmd {
sourceSets = [ project.sourceSets.main ]
ruleSetFiles = rootProject.files("codequality/pmd-ruleset.xml")
ruleSets = []
pmdMain {
excludes = [
'**/Application.*',
'**/jivesoftware/.*'
]
}
}
For my ruleset.xml for Gradle pmd plugin the following command works:
<ruleset name="Custom Rules">
<!-- Exclude generated open api classes -->
<exclude-pattern>.*/src/main/java/com/tbd/tbd/tbd/.*</exclude-pattern>
<exclude-pattern>.*/src/main/java/com/tbd/tbd/tbd/tbd/.*</exclude-pattern>
...
</ruleset>
Could you please try the command below?
...
<exclude-pattern>.*/src/main/java/net/java/.*</exclude-pattern>
...
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 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
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
}
}
I added to a project a set of sources that don't have tests and I don't want them spoil my test coverage statistics. I configured Jacoco in the next way :
test {
jacoco{
excludes = ['org/bla/**']
includes = ['com/bla/**']
append = false
}
}
jacocoTestReport {
dependsOn test
description = "Generate Jacoco coverage reports after running tests."
reports {
xml.enabled true
html.enabled true
}
classDirectories = fileTree(dir: 'build/classes/main', include: 'com/bla/**')
sourceDirectories = fileTree(dir: 'scr/main/java', include: 'com/bla/**')
}
But anyway, when generating the report, Jacoco also includes classes from org.bla
Can someone help me ?
EDIT
After some debugging, it appears that all default outputs are added to org.gradle.testing.jacoco.tasks.JacocoReport#classDirectories in a private method org.gradle.testing.jacoco.plugins.JacocoPlugin#addDefaultReportTasks
That's visible when using such code :
jacocoTestReport {
classDirectories = files('build/classes/main/com/bla')
println("-----------------------------------------------------------")
getAllClassDirs().each { File file ->
println(file.absolutePath)
}
println("-----------------------------------------------------------")
getAdditionalClassDirs().each{ File file ->
println(file.absolutePath)
}
}
jacocoTestReport << {
println("-----------------------------------------------------------")
getAllClassDirs().each { File file ->
println(file.absolutePath)
}
println("-----------------------------------------------------------")
getAdditionalClassDirs().each{ File file ->
println(file.absolutePath)
}
}
Output
-----------------------------------------------------------
<path_here>\build\classes\main\com\bla
-----------------------------------------------------------
....more text here
-----------------------------------------------------------
<path_here>\build\classes\main\com\bla
<path_here>\build\classes\main
<path_here>\build\resources\main
-----------------------------------------------------------
So - the question is : is it possible to override somehow org.gradle.testing.jacoco.plugins.JacocoPlugin#addDefaultReportTasks method, or override completely org.gradle.testing.jacoco.plugins.JacocoPlugin class ?
Ok, found a workaround :
jacocoTestReport.doFirst{
classDirectories = files('build/classes/main/com/bla')
}
This overrides classDirectories set by JacocoPlugin class.
If you have a task names something other than
test
that runs unit tests, you should be able to generate the report adding the following task to your build.gradle file:
jacocoTestReport {
group = "Reporting"
description = "Generates Jacoco coverage reports for unit tests"
executionData = files('build/jacoco/unitTest.exec')
reports {
html.destination = 'build/reports/coverage'
}
}
This needs to be called explicitly after the task to run unit tests, e.g:
gradle unitTest jacocoTestReport