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

非 Y 不嫁゛ 提交于 2019-12-01 20:02:21

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.

You can also use Violations Gradle Plugin for this. Then you can also run checkstyle, or any other analysis, before the build is failed.

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
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!