java - 如何使用 gradle 使用 Google Checkstyle 规则编译项目?

标签 java gradle checkstyle

我正在尝试使用 Google checkstyle 配置 ( https://github.com/checkstyle/checkstyle/blob/master/src/main/resources/google_checks.xml ),但我在 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)

我不知道我做错了什么。任何帮助,将不胜感激。 Gradle 版本:2.2

最佳答案

您可以将此配置添加到您的 build.gradle 文件中:

configurations {
  checkstyleOverride
}

dependencies {
  checkstyleOverride('com.puppycrawl.tools:checkstyle:6.11.2')
}

tasks.withType(Checkstyle) {
  checkstyleClasspath = project.configurations.checkstyleOverride
}

尽情享受吧!

关于java - 如何使用 gradle 使用 Google Checkstyle 规则编译项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28749804/

相关文章:

java - 如何在 Java 中解析时间?

gradle - 在第一个失败的任务后不要停止Gradle构建

android - 构建工具 21.1.2 – 意外的顶级异常

java - 如何轻松修复 Checkstyle 错误?

java - mvn checkstyle :checkstyle uses wrong configuration when using reporting

java - 如何用resteasy开发认证?

java - 读取DataMatrix/QR码 zxing java

java - (已解决)序列化(写入)充满对象的Arraylist不断抛出 "NotSerializableException"

java - Gradle - 只有在测试通过时才创建 jar

java - Eclipse CheckStyle 插件中的实例错误