gradle checkstyle just output warn, but build sucess

11.9k views Asked by At

as you can see the checkstyle warn,but the build of this time is still success. why? I want it faild. please help.

D:\MyData\xxxx>gradle clean checkstyleMain
:clean
:compileJava
:processResources
:classes
:checkstyleMain
[ant:checkstyle] [WARN] D:\MyData\xxxx.java:218:18: Member name 'Aource' 
must match pattern '^[a-z][a-z0-9][a-zA-Z0-9]*$'. [MemberName]

BUILD SUCCESSFUL

Total time: 4.966 secs

```

see the img: https://i.stack.imgur.com/7IbRM.jpg

see the checkstyle.xml: https://i.stack.imgur.com/BE4An.jpg

3

There are 3 answers

8
Hrabosch On BEST ANSWER

EDIT: There is better solution since I wrote that answer -> use maxErrors=0 as xxSwordy mentioned here https://stackoverflow.com/a/57141028/3464596.

Here is PR: https://github.com/gradle/gradle/pull/3901

There is possible to do it for errors by:

ignoreFailures = false

For warnings, there IS NOT POSSIBLE to do that, see [this][1] ticket.

From their old Jira and new GitHub issue comments there is one workaround:

tasks.withType(Checkstyle).each { checkstyleTask ->
    checkstyleTask.doLast {
        reports.all { report ->
            def outputFile = report.destination
            if (outputFile.exists() && outputFile.text.contains("<error ")) {
                throw new GradleException("There were checkstyle warnings! For more info check $outputFile")
            }
        }
    }
}

So answer is: This is not possible by default and there is still opened ticket for that. If you really WANT IT, you can try to participate and try to fix it, Gradle is opensource and it depends on developers :) [1]: https://github.com/gradle/gradle/issues/881

1
Ugo Uchegbu On

The default severity level for checkstyle violations is a warning. If you want the build to fail change the severity value from warning to error in your checkstyle.xml configuration. Any subsequent build will now fail if a violation exists.

<property name="severity" value="error"/>
3
gargaroff On

Sorry for grave-digging but I just stumbled across this while trying to achieve the same thing. Turns out that this has now been implemented, but requires a tiny configuration change.

So for any future adventures finding their way here: in your build.gradle to the following:

checkstyle {
  ignoreFailures = false
  maxWarnings = 0
}

This will then cause your builds to fail on Checkstyle errors.