[英]How to compile project with Google Checkstyle rules with gradle?
我正在尝试使用 Google checkstyle 配置( https://github.com/checkstyle/checkstyle/blob/master/src/main/resources/google_checks.xml ),但我经常在gradle check
上gradle check
错误:
Unable to create a Checker: cannot initialize module TreeWalker - Unable to instantiate EmptyCatchBlock
我使用 Gradle 来构建项目。 下面是我的 gradle.build。
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'checkstyle'
sourceCompatibility = 1.8
version = '1.0'
checkstyle {
toolVersion = "6.3"
}
task "create-dirs" << {
sourceSets*.java.srcDirs*.each { it.mkdirs() }
sourceSets*.resources.srcDirs*.each { it.mkdirs() }
}
jar {
manifest {
attributes 'Implementation-Title': 'xyz',
'Implementation-Version': 0.01
}
}
repositories {
mavenCentral()
}
dependencies {
compile (
['org.apache.logging.log4j:log4j-api:2.2'],
['org.apache.logging.log4j:log4j-core:2.2']
)
testCompile(
['junit:junit:4.11'],
['org.mockito:mockito-core:1.+']
)
}
test {
systemProperties 'property': 'value'
}
uploadArchives {
repositories {
flatDir {
dirs 'repos'
}
}
}
此外,当我尝试将 XML 配置文件添加到 IDEA 中的 Checkstyle 插件时,我得到了类似的错误,但有一个堆栈跟踪:
org.infernus.idea.checkstyle.exception.CheckStylePluginException: <html><b>The CheckStyle rules file could not be loaded.</b><br>cannot initialize module TreeWalker - Unable to instantiate EmptyCatchBlock</html>
at org.infernus.idea.checkstyle.checker.CheckerFactory.blacklistAndShowMessage(CheckerFactory.java:234)
at org.infernus.idea.checkstyle.checker.CheckerFactory.createChecker(CheckerFactory.java:188)
at org.infernus.idea.checkstyle.checker.CheckerFactory.getOrCreateCachedChecker(CheckerFactory.java:98)
at org.infernus.idea.checkstyle.checker.CheckerFactory.getChecker(CheckerFactory.java:73)
at org.infernus.idea.checkstyle.checker.CheckerFactory.getChecker(CheckerFactory.java:41)
我无法弄清楚我做错了什么。 任何帮助,将不胜感激。 摇篮版本:2.2
您可以将此配置添加到 build.gradle 文件中:
configurations {
checkstyleOverride
}
dependencies {
checkstyleOverride('com.puppycrawl.tools:checkstyle:6.11.2')
}
tasks.withType(Checkstyle) {
checkstyleClasspath = project.configurations.checkstyleOverride
}
享受!
问题在于com.puppycrawl.tools.checkstyle.checks.blocks.EmptyCatchBlockCheck
确实被添加到 checkstyle 但对于 6.4-SNAPSHOT 版本。 正如在 checkstyle存储库(pom.xml 历史)中可以看到的,6.4-SNAPSHOT 版本是在EmptyCatchBlockCheck
年 2 月 2 日引入的, EmptyCatchBlockCheck
类是在EmptyCatchBlockCheck
年 2 月EmptyCatchBlockCheck
日创建的。
Gradle 仍然使用 6.3 版本,如以下日志摘录所示:
:checkstyleMain
Download https://repo1.maven.org/maven2/com/puppycrawl/tools/checkstyle/6.3/checkstyle-6.3.pom
所以根本就没有你想使用的类。
根据文档,可以使用checkstyleClasspath
属性指定 checkstyle 类路径 - 您可以尝试手动设置它。
我还准备了一个 6.4-SNAPSHOT 版本的演示,可以在这里找到。 Checkstyle jar 是用mvn clean package
构建的,源代码来自这个repo。
这是一种适用于(当前)最新版本的 Gradle 和 Checkstyle(Gradle 6.1.1 和 Checkstyle 8.29)的方法:
plugins {
id 'java'
id 'checkstyle'
}
configurations {
checkstyleConfig
}
dependencies {
checkstyleConfig("com.puppycrawl.tools:checkstyle:8.29") { transitive = false }
}
checkstyle {
toolVersion '8.29'
config = resources.text.fromArchiveEntry(configurations.checkstyleConfig, 'google_checks.xml')
}
注意Checkstyle依赖排除了传递依赖,否则resources.text.fromArchiveEntry
会失败,因为会出现多个JAR文件,无法选择一个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.