How to exclude classes in findbugs gradle plugin without using XML - gradle

I am trying to exclude few classes from findbugs analysis in a gradle setup
apply plugin: 'application'
apply plugin: 'findbugs'
....
tasks.withType(FindBugs) {
def excludeProtobufFiles = 'com/turn/protobufkafka/'
classes = classes.filter {
!it.path.contains(excludeProtobufFiles)
}
// Generate HTML output for findbugs
reports {
xml.enabled = false
html.enabled = true
}
}
With this change gradle build works fine but gradle check fails:
:findbugsMain FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':findbugsMain'.
> No classes configured for FindBugs analysis.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 1.58 secs
gradle check works, however, if I run gradle build first. Is there a way to exclude files in gradle for findbugs such that gradle check works without failure.
Without the excluded classes both gradle check and gradle build works without any issues.

Related

Gradle: Invalid publication 'maven': artifact file does not exist

Java 11 and Gradle 7.2 here. I am trying to build a reusable library that I can (eventually) publish to a Maven repository and pull into other projects. But first I just want to get it publishing to my local m2 repo.
The project directory structure looks like:
mylib/
lib/
src/
build.gradle
Where build.gradle is:
plugins {
id 'java-library'
id 'maven-publish'
id 'io.freefair.lombok' version "6.5.0-rc1"
}
sourceCompatibility = 1.11
targetCompatibility = 1.11
archivesBaseName = 'mylib'
version = '1.0.0-RC1'
group = 'org.example'
repositories {
mavenCentral()
}
dependencies {
// omitted for brevity
)
testImplementation 'org.junit.jupiter:junit-jupiter:5.7.2'
}
publishing {
publications {
maven(MavenPublication) {
artifact("build/libs/${archivesBaseName}-${version}.jar") {
extension 'jar'
}
}
}
}
tasks.named('test') {
useJUnitPlatform()
}
publishToMavenLocal.configure {
mustRunAfter build
}
When I run gradle publishToMavenLocal I get:
% ./gradlew clean build publishToMavenLocal
> Task :lib:publishMavenPublicationToMavenLocal FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':lib:publishMavenPublicationToMavenLocal'.
> Failed to publish publication 'maven' to repository 'mavenLocal'
> Invalid publication 'maven': artifact file does not exist: '/Users/myuser/workspace/mylib/lib/build/libs/mylib-1.0.0-RC1.jar'
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 833ms
6 actionable tasks: 6 executed
So it looks like even though I'm specifying (on the command line) to run clean then build then publishToMavenLocal, and even though I'm even specifying (in build.gradle) that publishToMavenLocal must run after build, it seems what is happening is:
publishToMavenLocal insists on running first (before clean or build)
Since the JAR hasn't been built yet, no JAR file exists at the location specified ("build/libs/${archivesBaseName}-${version}.jar")
The build fails because the artifact doesn't exist
So I think I just need to get build to run before publishToMavenLocal but I'm out of ideas.
You're mixing the plugins DSL (plugins { }) with legacy plugin application (apply plugin). That's not a big deal, but you should go with #sean's answer and use the plugins DSL instead which will solve your issue.
To your problem at hand
Could not get unknown property 'plugin'
That happens because you missed the : in apply plugin
apply plugin: 'maven-publish'
Try placing your plugins in this way. Not sure if that resolves your issue though.
plugins {
id 'java-library'
id 'maven-publish'
}

PMD/Checkstyle reports during compilation Gradle

I am trying to generate PMD and Checkstyle reports during the compileJava and compileTestJava tasks in my gradle build. By default, the PMD and Checkstyle plugins only report bugs during the check and build phases of my project build. I cannot find the code to generate a report inside a task:
compileJava { // generate.pmd.report // generate.checkstyle.report}
compileTestJava {// generate.pmd.report // generate.checkstyle.report}
For pmd the solution is to use a tasks finalizedBy:
tasks.findByName('compileJava').finalizedBy(tasks.findByName('pmdMain')
tasks.findByName('compileTestJava').finalizedBy(tasks.findByName('pmdTest')
combined with pmd config to enable console output:
pmd {
consoleOutput = true
ignoreFailures = true // controls if the build should fail on detecting issues
}

gradle dependencyUpdates failure

I have "installed" or included ben manes - gradle versions plugin:
https://github.com/ben-manes/gradle-versions-plugin
The code below is the build.gradle (Project: rewards)
apply plugin: "com.github.ben-manes.versions"
buildscript {
ext.kotlin_version = '1.3.10'
repositories {
google()
jcenter()
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "com.github.ben-manes:gradle-versions-plugin:0.20.0"
}
}
When I try to run the following gradle Task: gradle dependencyUpdates.
I get the following :
FAILURE: Build failed with an exception.
What went wrong:
Task 'gradle' not found in project ':app'. Some candidates are: 'bundle'.
Try:
Run gradle tasks to get a list of available tasks. Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
Get more help at https://help.gradle.org
BUILD FAILED in 0s
11:23:53: Tasks execution finished 'gradle dependencyUpdates'.
After further testing, I realised that the task that I need to run should not have the word gradle in front. The command line instruction should just be dependencyUpdates. Then click on okay and it will export the report.

sonarqube gradle plugin excluding jacoco integration tests

I'm trying to integrate the sonarqube gradle plugin with the jacoco plugin:
classpath 'org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.1'
apply plugin: 'org.sonarqube'
apply plugin: 'jacoco'
My build/jacoco folder contains:
integrationTest.exec
test.exec
However, the sonarqube plugin only recognises the 'test.exec' file:
18:20:45.130 INFO - JaCoCoItSensor: JaCoCo IT report not found: C:\Users\abc3\Downloads\sme-letter\target\jacoco-it.exec
:
18:05:55.609 INFO - Analysing C:\Users\abc3\Downloads\sme-letter\build\jacoco\test.exec
How do I get the sonarqube plugin to recognise 'integrationTest.exec'
Thanks
Mark
I'm not really sure, whether this will work for Gradle plugun, but you may try.
Sonar has a property to specify the name of the integration tests JaCoCo report. This property is called sonar.jacoco.itReportPath (sonar.jacoco.reportPath for unit tests report).
And as far as I know, gradle sonar plugin let you add custom properties to it. So you can change IT report name via properties as follows:
sonarqube {
properties {
property "sonar.jacoco.itReportPath", "build/jacoco/ integrationTest.exec"
}
}
It works for me, but only after merging all jacoco reports into one file AND (important) deleting all other reports
property "sonar.java.coveragePlugin", "jacoco"
property "sonar.junit.reportsPath", "$projectDir/build/test-results"
property 'sonar.jacoco.reportPaths', "${rootProject.buildDir}/jacoco/mergeJacoco.exec"
And you need a merge jacoco task
Task mergeJacoco = tasks.create('mergeJacoco', JacocoMerge, {
doFirst {
delete "$buildDir/jacoco/mergeJacoco.exec"
}
destinationFile(new File(buildDir, 'jacoco/mergeJacoco.exec'))
executionData fileTree('./').include('**/*.exec-partial')
doLast {
delete fileTree('./').include('**/test.exec-partial')
}
})
mergeJacoco.outputs.upToDateWhen { false }
project.tasks["sonarqube"].dependsOn mergeJacoco
mergeJacoco.mustRunAfter ":myProject:test"
And setup jacoco to use those "partial" files
subprojects {
....
jacoco {
destinationFile file("$buildDir/jacoco/test.exec-partial")
append = false
includes = ['com.mypackage.*']
}
}
You'll get unit and IT reports mixed in one, but that's the best I could get

gradle - add clover params to each subproject

I have a gradle based project with numerous subprojects. I want to run clover on all of these but my config seems insistent on having the clover.license file in every subproject folder and a specific clover section in every subproject build file.
How would I specify the clover parama in the main project and have these applied to each subproject?
snip from build.gradle:
apply plugin: 'com.bmuschko.clover'
clover {
licenseLocation = new File(rootDir, 'clover.license')
report {
html = true
}
}
dependencies {
clover 'com.cenqua.clover:clover:3.2.0'
}
Error message from ./gradlew cloverGenerateReport:
service_name:cloverAggregateDatabases FAILED
FAILURE: Build failed with an exception.
What went wrong:
Execution failed for task ':service_name:cloverAggregateDatabases'.
java.lang.RuntimeException: Invalid or missing License.. Please visit http://my.atlassian.com to obtain a valid license.
Include this in the buildscript section of the subproject build.gradle files:
dependencies {
classpath 'com.bmuschko:gradle-clover-plugin:2.0.1'
}

Resources