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

时间:2018-07-10 02:33:42

标签: java gradle checkstyle

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


Java 8和Gradle 4.6在这里。如果您通过func fetchUsers( complete: @escaping ( _ success: Bool, _ users: [User], _ error: Error? )->()) { self.users = [] let circleId = UserDefaults.standard.string(forKey: "circleId") ?? "" DataService.call.REF_CIRCLES.document(circleId).collection("insiders").order(by: "position", descending: false).addSnapshotListener { querySnapshot, error in guard let snapshot = querySnapshot else { print("Error fetching snapshots: \(error!)") return } snapshot.documents.forEach { diff in let data = diff.data() let id = diff.documentID let user = User(key: id, data: data) self.users.append(user) complete(true, self.users, nil) } } } 创建了一个新的Java Gradle项目,然后通过Gradle Checkstyle插件对其进行了配置,并且将该插件配置为使用Google's Checkstyle XML,则它将立即失效:

gradle init --type java-library

通过该配置,运行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

我想知道为什么吗?!


编辑(供将来的Google员工使用)

使用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/ ,实际错误是

--stacktrace

1 个答案:

答案 0 :(得分:9)

显然Gradle使用了旧版本的CheckStyle-但是有一种解决方法!

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

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,而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项目中都存在一些针对此问题的错误