Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a customized gradle task to not to ignore Findbugs violations but fail after the analysis is completed

I want to write such a gradle task (using the Findbugs plugin) which fails if any Findbugs violations are found but only after completing the analysis. If I do ignoreFailures=true the task won't fail at all and if I make it false the task fails as soon as the first issue is found. I want the task to perform a complete analysis and fail only after it's done if any violations are found.

like image 831
Ankit Gupta Avatar asked Mar 17 '23 07:03

Ankit Gupta


1 Answers

You're right, adding ignoreFailures=true will prevent task from failing. Thus this option should be used and it should be checked later on if bugs were found.

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

Here complete and working example can be found. To see if it works remove incorrect.java file - then no bugs are found and - no exception is thrown.

like image 69
Opal Avatar answered Mar 21 '23 14:03

Opal