Checkstyle 自定义检查不适用于 gradle checkstyle 插件

Checkstyle custom check does not work on gradle checkstyle plugin

问题

我已经为 checkstyle 创建了自己的自定义检查,它可以在命令行上和通过 maven checkstyle 插件工作。 然而 通过 gradle checkstyle 插件,它出现以下错误 .

* What went wrong:
Execution failed for task ':my-project:checkstyleMain'.
> Unable to create Root Module: config {C:\Users\[path to my project]\build\tmp\resource\string8421659201972573805.txt}, classpath { ...many of classpathes. not "null" }.

每当从 checkstyle.xml 中排除自定义检查时,任务都会起作用。

如何在 gradle 上进行自定义检查?

版本

实施

public class MyCheck extends AbstractCheck {
...
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
          "-//Puppy Crawl//DTD Check Configuration 1.3//EN"
          "http://checkstyle.sourceforge.net/dtds/configuration_1_3.dtd">

<module name = "Checker">
    ...
    <module name="TreeWalker">
        ...
        <module name="package.to.my.check.MyCheck"/>
    </module>
</module>

自定义检查 class 和 checkstyle.xml 被打包到名为“mycheck-module”的工件中。

        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-checkstyle-plugin</artifactId>
          <version>3.1.2</version>
          <dependencies>
            <dependency>
              <groupId>com.puppycrawl.tools</groupId>
              <artifactId>checkstyle</artifactId>
              <version>8.37</version>
            </dependency>
            <dependency>
              <groupId>package.to.my.check.module</groupId>
              <artifactId>mycheck-module</artifactId>
              <version>[version]</version>
            </dependency>
          </dependencies>
          <executions>
            <execution>
              <phase>test</phase>
              <goals>
                <goal>check</goal>
              </goals>
            </execution>
          </executions>
          <configuration>
            <sourceDirectories>
              <sourceDirectory>${project.build.sourceDirectory}</sourceDirectory>
            </sourceDirectories>
            <configLocation>checkstyle.xml</configLocation>
            <enableFilesSummary>true</enableFilesSummary>
            <maxAllowedViolations>0</maxAllowedViolations>
            <violationSeverity>warning</violationSeverity>
            <consoleOutput>true</consoleOutput>
            <failOnViolation>
              false
            </failOnViolation>

            <propertyExpansion>
              checkstyleSuppressionConfigDir=${project.basedir}
            </propertyExpansion>
          </configuration>
        </plugin>
buildscript {
    ...
    dependencies {
        ...
        classpath 'package.to.my.check.module:mycheck-module:[version]'
    }
}

...

// Set up checkstyle
apply plugin: 'checkstyle'

def checkstyleSuppressionConfigDir = file("${rootDir}/suppressCheckstyle")

checkstyle {
    toolVersion = '8.37'
    sourceSets = [it.sourceSets.main]

    config = resources.text.fromString(getClass().getResourceAsStream('checkstyle.xml').text)
    ignoreFailures = false
    maxWarnings = 0
    maxErrors = 0

    configProperties.checkstyleSuppressionConfigDir = checkstyleSuppressionConfigDir
}

我自己解决了。 您可以使用下面的 [Append] 将依赖项添加到 checkstyle 中:

  • build.gradle
buildscript {
    ...
    dependencies {
        ...
        classpath 'package.to.my.check.module:mycheck-module:[version]'
    }
}

...

// Set up checkstyle
apply plugin: 'checkstyle'

def checkstyleSuppressionConfigDir = file("${rootDir}/suppressCheckstyle")

// ================ [Append] ================
dependencies {
    checkstyle 'package.to.my.check.module:mycheck-module:[version]'
}
// ================ [Append] ================

checkstyle {
    toolVersion = '8.37'
    sourceSets = [it.sourceSets.main]

    config = resources.text.fromString(getClass().getResourceAsStream('checkstyle.xml').text)
    ignoreFailures = false
    maxWarnings = 0
    maxErrors = 0

    configProperties.checkstyleSuppressionConfigDir = checkstyleSuppressionConfigDir
}