如何编写定制的gradle任务以避免忽略Findbugs违规,但在分析完成后失败 [英] How to write a customized gradle task to not to ignore Findbugs violations but fail after the analysis is completed

查看:526
本文介绍了如何编写定制的gradle任务以避免忽略Findbugs违规,但在分析完成后失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写这样一个Gradle任务(使用FindBugs插件),如果发现任何 Findbugs 违规行为,但只有在完成分析后才会失败 STRONG>。如果我执行 ignoreFailures = true ,任务将不会失败,如果我将它设置为false,那么只要找到第一个问题,任务就会失败。我希望任务能够执行完整的分析,并且只有在发现任何违规时才会执行完毕。

,添加 ignoreFailures = true 将防止任务失败。因此,这个选项应该被使用,如果发现错误,应该在以后检查。



这个脚本可以完成这项工作:

  apply plugin:'java'
apply plugin:'findbugs'

repositories {
mavenCentral()
}

findbugs {
ignoreFailures = true
}

任务checkFindBugsReport<< {
def xmlReport = findbugsMain.reports.xml
def slurped = new XmlSlurper()。parse(xmlReport.destination)
def bugsFound = slurped.BugInstance.size()
if (bugsFound> 0){
抛出新的GradleException(发现$ bugsFound FindBugs违规行为,请参阅报告:$ xmlReport.destination)
}
}

findbugsMain.finalizedBy checkFindBugsReport

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


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.

解决方案

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.

这篇关于如何编写定制的gradle任务以避免忽略Findbugs违规,但在分析完成后失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆