如何使用Gradle编译Google Checkstyle规则的项目? [英] How to compile project with Google Checkstyle rules with gradle?

查看:674
本文介绍了如何使用Gradle编译Google Checkstyle规则的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用Google checkstyle配置( https ://github.com/checkstyle/checkstyle/blob/master/src/main/resources/google_checks.xml ),但我经常在 gradle check

 无法创建检查器:无法初始化模块TreeWalker  - 无法实例化EmptyCatchBlock 

我使用Gradle构建项目。以下是我的gradle.build。

  apply plugin:'java'
apply plugin:'idea'
apply plugin:'checkstyle'

sourceCompatibility = 1.8
version ='1.0'

checkstyle {
toolVersion =6.3
}

任务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()
}

依赖项{
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 I中的Checkstyle插件相似错误但带有堆栈跟踪:

  org.infernus.idea.checkstyle.exception.CheckStylePluginException:< html>< b> ;无法加载CheckStyle规则文件< / b>< br>无法初始化模块TreeWalker  - 无法实例化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

解决方案

问题在于 com.puppycrawl。 tools.checkstyle.checks.blocks.EmptyCatchBlockCheck 确实已添加到checkstyle,但用于版本6.4-SNAPSHOT。正如在checkstyle (pom.xml历史)中可以看到的那样,版本6.4-SNAPSHOT在02.02上引入.2015和 EmptyCatchBlockCheck class于18.02.2015创建。



Gradle仍然使用版本6.3,如下面的日志摘录所示:

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

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

根据 checkstyleClasspath 属性指定checkstyle classpath,您可以尝试手动设置它。



我也准备了一个6.4-SNAPSHOT版本的演示,它可以在此处。 Checkstyle jar是用 mvn clean package 构建的,源代码来自 回购。


I am trying to use Google checkstyle configuration (https://github.com/checkstyle/checkstyle/blob/master/src/main/resources/google_checks.xml) but I am constantly getting an error on gradle check:

Unable to create a Checker: cannot initialize module TreeWalker - Unable to instantiate EmptyCatchBlock

I used Gradle to build the project. Below is my 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'
       }
    }
}

Also, when I try to add XML config file to Checkstyle plugin in IDEA I get similar error but with a stack trace:

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)

I cannot figure out what am I doing wrong. Any help would be appreciated. Gradle version: 2.2

解决方案

The problem lies in the fact that com.puppycrawl.tools.checkstyle.checks.blocks.EmptyCatchBlockCheck was indeed added to checkstyle but for version 6.4-SNAPSHOT. As it can be seen in checkstyle repository (pom.xml history) version 6.4-SNAPSHOT was introduced on the 02.02.2015 and EmptyCatchBlockCheck class was created on 18.02.2015.

Gradle still uses version 6.3 as in the following log extract:

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

So there's simply no class You'd like to use.

According to the docs checkstyle classpath can be specified with checkstyleClasspath property - you can try to set it up manually.

I've also prepared a demo with 6.4-SNAPSHOT version, it can be found here. Checkstyle jar was built with mvn clean package with source taken from this repo.

这篇关于如何使用Gradle编译Google Checkstyle规则的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆