繁体   English   中英

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 上进行自定义检查?

版本

  • 检查样式:8.37
  • maven checkstyle插件版本:3.1.2
  • gradle 版本:5.6.2

执行

  • 自定义检查
public class MyCheck extends AbstractCheck {
...
  • checkstyle.xml
<?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”的工件中。

  • pom.xml(有效)
        <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>
  • build.gradle(不起作用)
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
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM