问题
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.
回答1:
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.
回答2:
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
来源:https://stackoverflow.com/questions/28069712/how-to-write-a-customized-gradle-task-to-not-to-ignore-findbugs-violations-but-f