如何找到 gradle 使用的是哪个 checkstyle 版本?

How to find which checkstyle version gradle is using?

在我的 gradle 文件中我有:

apply plugin: 'checkstyle'

我正在尝试创建自己的 checkstyle 规则。出于这个原因,我在我的 gradle 文件中添加了一个依赖项。

dependencies {
     checkstyle 'com.puppycrawl.tools:checkstyle:8.2'
}

我正在尝试扩展 "Check" class 的 checkstyle。但是checkstyle的版本很多,我不知道gradle用的是哪个。

如何找到 gradle 使用的 checkstyle 的确切版本号?

开箱即用 apply plugin: 'checkstyle',checkstyle 将使用 v6.19

已下载的依赖项:

Download https://repo1.maven.org/maven2/com/puppycrawl/tools/checkstyle/6.19/checkstyle-6.19.pom
Download https://repo1.maven.org/maven2/org/antlr/antlr4-runtime/4.5.3/antlr4-runtime-4.5.3.pom
Download https://repo1.maven.org/maven2/org/antlr/antlr4-master/4.5.3/antlr4-master-4.5.3.pom
Download https://repo1.maven.org/maven2/commons-beanutils/commons-beanutils/1.9.3/commons-beanutils-1.9.3.pom
Download https://repo1.maven.org/maven2/commons-cli/commons-cli/1.3.1/commons-cli-1.3.1.pom
Download https://repo1.maven.org/maven2/org/apache/commons/commons-parent/41/commons-parent-41.pom
Download https://repo1.maven.org/maven2/commons-cli/commons-cli/1.3.1/commons-cli-1.3.1.jar
Download https://repo1.maven.org/maven2/com/puppycrawl/tools/checkstyle/6.19/checkstyle-6.19.jar
Download https://repo1.maven.org/maven2/commons-beanutils/commons-beanutils/1.9.3/commons-beanutils-1.9.3.jar
Download https://repo1.maven.org/maven2/org/antlr/antlr4-runtime/4.5.3/antlr4-runtime-4.5.3.jar

dependencies { checkstyle 'com.puppycrawl.tools:checkstyle:8.2' }后,下载的依赖列表:

Download https://repo1.maven.org/maven2/com/puppycrawl/tools/checkstyle/8.2/checkstyle-8.2.pom
Download https://repo1.maven.org/maven2/org/antlr/antlr4-runtime/4.7/antlr4-runtime-4.7.pom
Download https://repo1.maven.org/maven2/org/antlr/antlr4-master/4.7/antlr4-master-4.7.pom
Download https://repo1.maven.org/maven2/commons-cli/commons-cli/1.4/commons-cli-1.4.pom
Download https://repo1.maven.org/maven2/net/sf/saxon/Saxon-HE/9.8.0-4/Saxon-HE-9.8.0-4.pom
Download https://repo1.maven.org/maven2/org/antlr/antlr4-runtime/4.7/antlr4-runtime-4.7.jar
Download https://repo1.maven.org/maven2/net/sf/saxon/Saxon-HE/9.8.0-4/Saxon-HE-9.8.0-4.jar
Download https://repo1.maven.org/maven2/commons-cli/commons-cli/1.4/commons-cli-1.4.jar
Download https://repo1.maven.org/maven2/com/puppycrawl/tools/checkstyle/8.2/checkstyle-8.2.jar

目前我能想到三种方法,最不吸引人的第一个:

  • 你可以看看 Gradle source code.
  • 你可以检查Checkstyle Compatibility Matrix(L列,黄色单元格)。
    两者都说从Gradle3.3开始,默认的Checkstyle版本是6.19;以前是5.9。只有 Gradle 2.4 之前的版本使用更旧版本的 Checkstyle。
  • 但是推荐的方法是明确选择 Checkstyle 版本,方法是在 build.gradle 文件:

    checkstyle {
        configFile file('your/checkstyle.xml');
        toolVersion '8.2';    // your choice here
    }
    

    这比依赖默认版本要好,因为您可以使用更新版本的 Checkstyle,并且您的 Checkstyle 设置不会在您更新时中断 Gradle。

提供更好的方案,可以参考checkstyle源码配置文件的例子,不用担心兼容性问题

简直运行gradle dependencies | grep checkstyle

然后在此处获取相应的checkstyle源以参考配置示例:https://github.com/checkstyle/checkstyle/releases

您可以检查 checkstyle.toolVersion 的当前值,方法是将其写入您的 build.gradle 文件并重新加载您的 gradle 项目

plugins {
    id 'java'
    id 'checkstyle'
}

println checkstyle.toolVersion