问题描述
问题
我为 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 上进行自定义检查?
版本
- checkstyle: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>
自定义检查类和 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
}