Checkstyle,无法创建根模块

Checkstyle, Unable to create Root Module

我正在尝试在项目中配置 Checkstyle。我添加了:

    apply plugin: 'checkstyle'
    checkstyle {
        // assign the latest checkstyle version explicitly
        // default version is very old, likes 5.9
        toolVersion = '8.6'
        // checkstyle.xml copy from:
        // https://raw.githubusercontent.com/checkstyle/checkstyle/checkstyle-8.6/src/main/resources/google_checks.xml
        // the version should be as same as plugin version
        configFile = rootProject.file('config/checkstyle/checkstyle.xml')
    }
    task Checkstyle(type: Checkstyle) {
        source 'src/main/java'
        include '**/*.java'
        exclude '**/gen/**'
        exclude '**/R.java'
        exclude '**/BuildConfig.java'

        // empty classpath
        classpath = rootProject.files()
    }

allprojects 中的根 gradle 文件。

但是当我运行./gradlew checkstyle

我得到:

* What went wrong:
Execution failed for task ':app:Checkstyle'.
> Unable to create Root Module: config {/Users/user/Development/project/config/checkstyle/checkstyle.xml}, classpath {null}.

即使有规则的文件也位于指定的目录中。

只要无法读取配置文件,就会发生此错误。您确定有 /Users/user/Development/project/config/checkstyle/checkstyle.xml 文件吗?

config/checkstyle/checkstyle.xml 包含对 non-existent 文件的引用时,我收到此错误。就我而言,通过:

<module name="SuppressionFilter">
    <property name="file" value="config/checkstyle/checkstyle-suppressions.xml"/> <!-- Oops: No such file! -->
</module>

创建文件 config/checkstyle/checkstyle-suppressions.xml 解决了问题。

如果有帮助,其内容(header 与 checkstyle.xml 相同)是:

<suppressions>
    <suppress files="/generated/" checks="\w+"/>
    <suppress files="/test/" checks="\w+"/>
</suppressions>

我有一个非常相似的问题,我基本上使用了@Bohemian 的答案。 checkStyle 找不到文件,所以我强行输入了它想要的内容。在我的例子中,根本原因是 NetBeans 的 gradle 插件没有正确传播所有 "environment" 变量,因此对 ${basedir}/config/checkstyle/import-control.xml 的引用失败。当 checkStyle 在 NetBeans 中为 运行ning 时,${basedir} 设置为 $USER_HOME,而不是项目的根目录。作为一种解决方法,我将所需的文件复制到我的主目录中,这允许 checkStyle 到 运行 正确。
丑陋,但总比没有好。

我的问题是这个进程 运行 来自与我想象的不同的目录。

即使我检查以确保 checkstyle.xml 确实存在于它显示的路径中,它仍然会中断,因为我的构建过程未能 cd 到正确的目录 运行这个!

确保你运行从你认为自己所在的地方开始!

问题是找不到规则文件。所以我改变了:

configFile = rootProject.file('config/checkstyle/checkstyle.xml')

至:

configFile = file("${rootDir}/config/checkstyle/checkstyle.xml")

现在它可以正确拾取它了。

我也遇到了类似的问题和相同的异常消息。以下步骤解决了我的问题:

已将工具版本更新到 8.16。在我有8.1之前 我使用 'configDir' 而不是 'configFile' 属性。

configDir = file("$rootProject.projectDir/etc/checkstyle")

之前是:

configFile = file("$rootProject.projectDir/etc/checkstyle/checkstyle.xml")

此外,我有一个 'reportDir' 属性 也被更改为指向目录而不是文件。

reportsDir = file("$project.buildDir/reports/checkstyle")

之前是:

reportsDir = file("$project.buildDir/reports/checkstyle/checkstyle.xml")

我正在使用 JDK 11,它可以正常工作。

我将 Android Gradle 插件更新到 3.3.0

时发生了这种情况

请运行这个:

./gradlew checkstyle --stacktrace

您可能需要检查 checkstyle.xml 配置文件。

我需要移动这两个标签:

 <module name="SuppressionCommentFilter"/>

        <!--Enable usage SUPPRESS CHECKSTYLE comment-->
        <module name="SuppressWithNearbyCommentFilter">
            <property name="commentFormat" value="CHECKSTYLE IGNORE"/>
            <property name="checkFormat" value=".*"/>
            <property name="influenceFormat" value="0"/>
        </module>

里面

<module name="TreeWalker"> 

标签

我还需要删除:

<module name="SuppressionCommentFilter"/>

这个问题也可能是由于您在 checkstyle 配置中使用了过时的 api。请参阅此链接 here and here

我看到这个问题在不同的维度上被问到(在 SO 和 github 上被问到),这里是我发现的总结。

我假设 checkstyle.xml 的位置不是这里的问题。在了解此解决方案之前,我建议您查看其他答案以了解如何解决该问题。最快的验证方法是将此打印语句放入您的一项任务中,然后 运行 它(任务)

println(file("${rootDir.projectDir}/path/to/checkstyle.xml"))

确保它在控制台中打印出的路径是correct/valid。如果它是正确的路径但您仍然看到错误,那么您遇到了不同的问题;继续以下场景:

  • 如果你像这样在 Gradle 中添加了 checkstyle 插件(我使用的是 Gradle >= 4.0):
plugins {
id 'checkstyle'
}

默认的 checkstyle 版本将用于该版本的 Gradle。 Gradle 版本与默认检查样式插件版本 HERE

有另一个兼容性矩阵
  • 如果您想指定插件使用的 checkstyle.xml,您需要牢记这一点 - 插件版本与兼容 checkstyle.xml 版本之间存在一个矩阵。对于 google 检查样式,我发现最好的方法是检查插件版本。例如,如果我想在我的项目中使用版本 8.2,它的 build.gradle 条目将如下所示:
plugins {
id 'checkstyle'
}

checkstyle {
  project.ext.checkstyleVersion = '8.20'
}

这将自动使用(拉取)与指定版本(即 8.20)兼容的 checkstyle.xml

  • 如果您想自定义(add/remove)一些检查样式规则,我建议您使用(编辑)兼容版本tart。 要了解与此插件版本兼容的 checkstyle.xml 版本,请转到 Google Checkstyle releases on Github and download the 8.20 release zip 或 tar。 解压后,在 src/main/resources/ 目录中找到 google(或 sun)checkstyle xml。根据需要自定义 xml 后,您的 build.gradle 条目将如下所示

plugins {
id 'checkstyle'
}

checkstyle {
  project.ext.checkstyleVersion = '8.20'
  configFile = file("${rootDir.projectDir}/path/to/google_checks.xml")
}

希望您将不再看到该错误。

来自 version 8.24 的发行说明:

Change LineLength Check parent from TreeWalker to Checker.

所以我不得不搬家

<module name="LineLength">

从里面

<module name="TreeWalker">

到里面

<module name="Checker">

对我来说,由于 java 版本不匹配,它失败了。

我是 运行 java 11 但在 build.gradle sourceCompatibility = 1.8

一旦我切换到 java 1.8,它运行良好。