How to write a custom gradle task so as not to ignore Findbugs violations, but to fail after analysis is complete

I want to write a gradle task (using the Findbugs plugin) that does not work if violations are found in Findbugs , but only after the analysis is completed . If I execute ignoreFailures=true , the task will not fail at all, and if I make it false, the task will fail as soon as the first problem is found. I want the task to perform a full analysis and fail only after it has been done, if any violations have been detected.

+4
gradle findbugs
source share
2 answers

You are correct by adding ignoreFailures=true to prevent the task from crashing. Therefore, this parameter should be used and should be checked later if errors are detected.

This script does the job:

 apply plugin: 'java' apply plugin: 'findbugs' repositories { mavenCentral() } findbugs { ignoreFailures = true } task checkFindBugsReport << { def xmlReport = findbugsMain.reports.xml def slurped = new XmlSlurper().parse(xmlReport.destination) def bugsFound = slurped.BugInstance.size() if (bugsFound > 0) { throw new GradleException("$bugsFound FindBugs rule violations were found. See the report at: $xmlReport.destination") } } findbugsMain.finalizedBy checkFindBugsReport 

You can find a complete and working example here. To make sure that it works, delete the incorrect.java file - then no errors were found and - no exception was thrown.

+7
source share

You can also use the Violations Gradle Plugin . Then you can also run checkstyle or any other analysis until the build fails.

 task violations(type: se.bjurr.violations.gradle.plugin.ViolationsTask) { minSeverity = 'INFO' detailLevel = 'VERBOSE' // PER_FILE_COMPACT, COMPACT or VERBOSE maxViolations = 0 // Many more formats available, see: https://github.com/tomasbjerre/violations-lib violations = [ ["FINDBUGS", ".", ".*/findbugs/.*\\.xml\$", "Findbugs"] ] } check.finalizedBy violations 
0
source share

All Articles