我正在尝试在项目中配置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}.
甚至包含规则的文件也位于指定的目录中。
当我将Android Gradle Plugin更新到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"/>
当config/checkstyle/checkstyle.xml
包含对不存在的文件的引用时,我收到此错误。就我而言,通过:
<module name="SuppressionFilter">
<property name="file" value="config/checkstyle/checkstyle-suppressions.xml"/> <!-- Oops: No such file! -->
</module>
创建文件config/checkstyle/checkstyle-suppressions.xml
修复了问题。
如果有帮助,其内容(与checkstyle.xml
具有相同的标题)是:
<suppressions>
<suppress files="/generated/" checks="\w+"/>
<suppress files="/test/" checks="\w+"/>
</suppressions>
我也有类似的问题和相同的异常消息。以下步骤解决了我的问题:
已将工具版本更新为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,它使用它。
问题是它无法找到规则文件。所以我改变了:
configFile = rootProject.file('config/checkstyle/checkstyle.xml')
至:
configFile = file("${rootDir}/config/checkstyle/ckeckstyle.xml")
现在它正确地拿起它。
只要无法读取配置文件,就会发生此错误。你确定你有一个/Users/user/Development/project/config/checkstyle/checkstyle.xml
文件?
我有一个非常相似的问题,我基本上使用了@Bohemian的答案。 checkStyle无法找到一个文件所以我强行喂它想要它。在我的例子中,根本原因是NetBeans的gradle插件没有正确传播所有“环境”变量,所以对${basedir}/config/checkstyle/import-control.xml
的引用失败了。当checkStyle
在NetBeans中运行时,${basedir}
设置为$USER_HOME
,而不是项目的根目录。作为一种解决方法,我将所需的文件复制到我的主目录中,这使checkStyle
能够正确运行。
丑陋,但总比没有好。
我的问题是这个过程是从一个不同于我想象的目录运行的。
即使我检查确保checkstyle.xml
确实存在于它显示的路径中,它仍然是破坏因为我的构建过程未能将cd
转到正确的目录来运行它!
确保你从你认为的地方运行它!