2

我已将 Gradle 构建脚本配置为使用 checkstyle 以及添加的 sventu checkstyle 检查,但是当我执行 checkstyleMain 任务时,构建失败并出现以下错误:

* What went wrong:
Execution failed for task ':checkstyleMain'.
> Unable to create a Checker: cannot initialize module TreeWalker - Unable to instantiate AvoidConstantAsFirstOperandInConditionCheck

即使我在构建中包含了 checkstyle jar,也会发生这种情况。以下是我的构建脚本的相关部分:

repositories {
      mavenCentral()
      maven {
          url "http://sevntu-checkstyle.github.com/sevntu.checkstyle/maven2"
      }
   }

checkstyle {
    configFile = new File("etc/config/dev.xml");
    toolVersion = "6.8"
}

configurations {
    checkstyle
}

dependencies {  
    checkstyle "com.github.sevntu.checkstyle:sevntu-checks:1.13.4"
}

请注意,当我删除依赖项部分并使用未配置额外 sevntu 检查的 checkstyle xml 文档进行测试时,构建工作正常。我的配置也类似于sevntu-checkstyle/checkstyle-samples 中的示例

我在这里想念什么?

4

1 回答 1

3

所以我终于想通了:

结果显示https://github.com/sevntu-checkstyle/checkstyle-samples/blob/master/gradle-project/build.gradle的示例仅在您将自定义检查的完整类路径作为每个自定义的名称时才有效查看。

这是因为 checkstyle 不知道自定义检查在包中的位置。如果您在描述包含检查的包的 jar 中包含一个 checkstyle_packages.xml 文件,那么 checkstyle 可以发现这一点。

不幸的是,com.github.sevntu.checkstyle:sevntu-checks:1.13.4 中没有这样的文件。要获取此信息,您还需要包含“com.github.sevntu.checkstyle:sevntu-checkstyle-maven-plugin:1.13.4”,它基本上只包含 checkstyle_packages.xml 文件。

所以我把它添加到我的依赖项中,checkstyle 规则最终解析:

dependencies {
    checkstyle "com.github.sevntu.checkstyle:sevntu-checks:1.13.4",
               "com.github.sevntu.checkstyle:sevntu-checkstyle-maven-plugin:1.13.4"
}

希望这会在将来为某人减轻一些痛苦:)

于 2015-07-17T13:34:15.427 回答