Gradle Checkstyle插件默认与Google Checks不兼容

hot*_*oup 5 java checkstyle gradle

请注意:我创建了这个GitHub项目来帮助您准确地解决问题.


Java 8和Gradle 4.6在这里.如果您通过gradle init --type java-libraryGradle Checkstyle插件创建一个新的Java Gradle项目,并将该插件配置为使用Google的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')
}
Run Code Online (Sandbox Code Playgroud)

使用该配置,运行./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/
Run Code Online (Sandbox Code Playgroud)

我想知道为什么?!


编辑(面向未来的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
Run Code Online (Sandbox Code Playgroud)

Boa*_*oaz 10

显然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
Run Code Online (Sandbox Code Playgroud)

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

checkstyle {
    config = rootProject.resources.text.fromFile('buildConfig/checkstyle/checkstyle.xml')
    toolVersion '8.11'
}
Run Code Online (Sandbox Code Playgroud)

结果是:

> 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
Run Code Online (Sandbox Code Playgroud)

在Gradle项目和CheckStyle项目中,这个问题都有几个漏洞