如何编写自定义的gradle任务以不忽略Findbugs违规但在分析完成后失败

Ank*_*pta 4 findbugs gradle

我想编写这样一个gradle任务(使用Findbugs插件),如果发现任何Findbugs违规,但只有在完成分析后才会失败.如果我这样做ignoreFailures=true,任务将不会失败,如果我将其设为false,则一旦找到第一个问题,任务就会失败.我希望任务执行完整分析,并且只有在完成任何违规后才会失败.

Opa*_*pal 7

你是对的,添加ignoreFailures=true会阻止任务失败.因此,应该使用此选项,如果发现错误,应在以后检查.

这个脚本完成了这项工作:

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
Run Code Online (Sandbox Code Playgroud)

在这里可以找到完整的工作示例.要查看它是否有效删除incorrect.java文件 - 然后找不到错误 - 并且不会抛出任何异常.