如何用gradle编译带有Google Checkstyle规则的项目?

时间:2015-02-26 18:15:09

标签: 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

3 个答案:

答案 0 :(得分:4)

问题在于com.puppycrawl.tools.checkstyle.checks.blocks.EmptyCatchBlockCheck确实添加到checkstyle但是版本6.4-SNAPSHOT。正如在checkstyle repository(pom.xml历史)中可以看到的那样,版本6.4-SNAPSHOT于02.02.2015推出,EmptyCatchBlockCheck类于2015年2月18日创建。

Gradle仍然使用版本6.3,如以下日志提取中所示:

:checkstyleMain
Download https://repo1.maven.org/maven2/com/puppycrawl/tools/checkstyle/6.3/checkstyle-6.3.pom

所以根本没有你想要使用的课程。

根据docs checkstyle类路径可以使用checkstyleClasspath属性指定 - 您可以尝试手动设置它。

我还准备了一个6.4-SNAPSHOT版本的演示,可以找到它here。 Checkstyle jar使用mvn clean package构建,源代码来自this repo。

答案 1 :(得分:3)

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

configurations {
  checkstyleOverride
}

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

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

享受!

答案 2 :(得分:1)

这是一种与(当前)最新版本的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文件,并且将无法选择单个文件。