Gradle Checkstyle 插件与 Google 默认检查不兼容

Gradle Checkstyle plugin is incompatible with Google Checks by default

请注意: 我创建了 this GitHub project to help you exactly produce the issue


Java 8 和 Gradle 4.6 这里。如果您通过 gradle init --type java-library 创建一个新的 Java Gradle 项目,然后将 Gradle Checkstyle 插件添加到它,并将该插件配置为使用 Google's Checkstyle XML 它将会失败开箱即用:

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')
}

使用该配置,运行 ./gradle clean build 产生:

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/

我想知道为什么?!


编辑(面向未来的 Google 员工):

使用--stacktrace,实际误差为

cannot initialize module TreeWalker - Token "METHOD_REF" was not found in Acceptable tokens list in check com.puppycrawl.tools.checkstyle.checks.whitespace.SeparatorWrapCheck

显然 Gradle 使用了旧版本的 CheckStyle - 但有办法解决这个问题!

首先,我建议当您在构建中遇到问题时,使用 --stacktrace-S 来查看实际的失败,通过使用它您将准确地看到失败的原因:

cannot initialize module TreeWalker - Token "METHOD_REF" was not found in Acceptable tokens list in check com.puppycrawl.tools.checkstyle.checks.whitespace.SeparatorWrapCheck

这是因为Gradle4.6使用的CheckStyle 6.19,现在已经很老了(最新的是8.11) 升级配置以使用最新版本解决了这个问题:

checkstyle {
    config = rootProject.resources.text.fromFile('buildConfig/checkstyle/checkstyle.xml')
    toolVersion '8.11'
}

结果是:

> 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

在 Gradle 项目和 CheckStyle 项目

中都存在关于这个问题的几个错误