Please note: I created this GitHub project to help you exactly produce the issue.
Java 8 and Gradle 4.6 here. If you create a new Java Gradle project via gradle init --type java-library
and then the Gradle Checkstyle plugin to it, and configure that plugin to use Google's Checkstyle XML it will fail right out of the box:
plugins {
id 'java-library'
}
apply plugin: 'checkstyle'
dependencies {
testCompile(
'junit:junit:4.12'
)
}
repositories {
jcenter()
mavenCentral()
}
checkstyle {
// Go to the Google Checks link above and paste its
// contents into checkstyle.xml
config = rootProject.resources.text.fromFile('buildConfig/checkstyle/checkstyle.xml')
}
With that config, running ./gradle clean build
produces:
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':checkstyleMain'.
> Unable to create a Checker: configLocation {/Users/myuser/workspace/test-gradle-checkstyle/buildConfig/checkstyle/checkstyle.xml}, classpath {/Users/myuser/workspace/test-gradle-checkstyle/build/classes/java/main:/Users/myuser/workspace/test-gradle-checkstyle/build/resources/main}.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 1s
4 actionable tasks: 4 executed
$ pwd
/Users/myuser/workspace/test-gradle-checkstyle
$ git init
Initialized empty Git repository in /Users/myuser/workspace/test-gradle-checkstyle/.git/
I'm wondering why?!
Edit (for future Googlers):
Using --stacktrace
, the actual error is
cannot initialize module TreeWalker - Token "METHOD_REF" was not found in Acceptable tokens list in check com.puppycrawl.tools.checkstyle.checks.whitespace.SeparatorWrapCheck
To use checkstyle in Gradle you have add the plug-in to your build. gradle and provide a config\checkstyle\checkstyle. xml checkstyle configuration file. A detailed example is given in the checkstyle with Gradle exercise.
Apparently Gradle uses an old version of CheckStyle - but there is a way to solve this!
First, I'd suggest that when you encounter issues in your build, use the --stacktrace
or -S
to see the actual failure, by using it you'll see exactly what is failing:
cannot initialize module TreeWalker - Token "METHOD_REF" was not found in Acceptable tokens list in check com.puppycrawl.tools.checkstyle.checks.whitespace.SeparatorWrapCheck
This is because Gradle 4.6 uses CheckStyle 6.19, which is quite old by now (latest is 8.11) Upgrading the configuration to use latest solves this issue:
checkstyle {
config = rootProject.resources.text.fromFile('buildConfig/checkstyle/checkstyle.xml')
toolVersion '8.11'
}
The result is:
> Task :checkstyleMain
[ant:checkstyle] [WARN] /.../test-gradle-checkstyle/src/main/java/Library.java:5: 'method def modifier' has incorrect indentation level 4, expected level should be 2. [Indentation]
[ant:checkstyle] [WARN] /.../test-gradle-checkstyle/src/main/java/Library.java:6: 'method def' child has incorrect indentation level 8, expected level should be 4. [Indentation]
[ant:checkstyle] [WARN] /.../test-gradle-checkstyle/src/main/java/Library.java:7: 'method def rcurly' has incorrect indentation level 4, expected level should be 2. [Indentation]
> Task :checkstyleTest
[ant:checkstyle] [WARN] /.../test-gradle-checkstyle/src/test/java/LibraryTest.java:5: 'import' should be separated from previous statement. [EmptyLineSeparator]
[ant:checkstyle] [WARN] /.../test-gradle-checkstyle/src/test/java/LibraryTest.java:5: Import statement for 'org.junit.Assert.*' is in the wrong order. Should be in the 'STATIC' group, expecting not assigned imports on this line. [CustomImportOrder]
[ant:checkstyle] [WARN] /.../test-gradle-checkstyle/src/test/java/LibraryTest.java:5: Using the '.*' form of import should be avoided - org.junit.Assert.*. [AvoidStarImport]
[ant:checkstyle] [WARN] /.../test-gradle-checkstyle/src/test/java/LibraryTest.java:8: 'method def modifier' has incorrect indentation level 4, expected level should be 2. [Indentation]
[ant:checkstyle] [WARN] /.../test-gradle-checkstyle/src/test/java/LibraryTest.java:9: 'method def' child has incorrect indentation level 8, expected level should be 4. [Indentation]
[ant:checkstyle] [WARN] /.../test-gradle-checkstyle/src/test/java/LibraryTest.java:10: 'method def' child has incorrect indentation level 8, expected level should be 4. [Indentation]
[ant:checkstyle] [WARN] /.../test-gradle-checkstyle/src/test/java/LibraryTest.java:11: 'method def rcurly' has incorrect indentation level 4, expected level should be 2. [Indentation]
BUILD SUCCESSFUL in 9s
7 actionable tasks: 7 executed
There are several bugs open on this issue both in the Gradle project and the CheckStyle project
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With