gpt4 book ai didi

java - 默认情况下,Gradle Checkstyle 插件与 Google Checks 不兼容

转载 作者:搜寻专家 更新时间:2023-10-31 08:31:18 24 4
gpt4 key购买 nike

请注意:我创建了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/

我想知道为什么?!


编辑(针对 future 的 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

这是因为 Gradle 4.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 项目中都有几个关于这个问题的错误

关于java - 默认情况下,Gradle Checkstyle 插件与 Google Checks 不兼容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51256494/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com