Daw*_*yży 17 android checkstyle
我正在尝试在项目中配置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()
}
Run Code Online (Sandbox Code Playgroud)
到我的根gradle文件中allprojects.
但是当我跑步的时候 ./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}.
Run Code Online (Sandbox Code Playgroud)
甚至包含规则的文件也位于指定的目录中.
当config/checkstyle/checkstyle.xml包含对不存在的文件的引用时,我收到此错误。就我而言,通过:
<module name="SuppressionFilter">
<property name="file" value="config/checkstyle/checkstyle-suppressions.xml"/> <!-- Oops: No such file! -->
</module>
Run Code Online (Sandbox Code Playgroud)
创建文件config/checkstyle/checkstyle-suppressions.xml解决了问题。
如果有帮助,它的内容(与 相同的标题checkstyle.xml)是:
<suppressions>
<suppress files="/generated/" checks="\w+"/>
<suppress files="/test/" checks="\w+"/>
</suppressions>
Run Code Online (Sandbox Code Playgroud)
当我将Android Gradle插件更新为3.3.0时,这发生在我身上
请运行以下命令:
./gradlew checkstyle-堆栈跟踪
可能您需要检查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>
Run Code Online (Sandbox Code Playgroud)
内
<module name="TreeWalker">
Run Code Online (Sandbox Code Playgroud)
标签
我还需要删除:
<module name="SuppressionCommentFilter"/>
Run Code Online (Sandbox Code Playgroud)
问题是它找不到规则文件。所以我改变了:
configFile = rootProject.file('config/checkstyle/checkstyle.xml')
Run Code Online (Sandbox Code Playgroud)
到:
configFile = file("${rootDir}/config/checkstyle/checkstyle.xml")
Run Code Online (Sandbox Code Playgroud)
现在它正确地捡起了它。
我已经看到这个问题在不同的维度上被问到(在 SO 和 github 上都被问到),这里是我发现的摘要。
我假设这里的位置checkstyle.xml不是问题。在讨论此解决方案之前,我建议您查看其他答案以了解如何解决该问题。最快的验证方法是将此打印语句放入您的任务之一并运行它(任务)
println(file("${rootDir.projectDir}/path/to/checkstyle.xml"))
确保它在控制台中打印出的路径是正确/有效的。如果这是正确的路径,但您仍然看到错误,那么您遇到了不同的问题;继续以下场景:
plugins {
id 'checkstyle'
}
Run Code Online (Sandbox Code Playgroud)
默认的 checkstyle 版本将用于该版本的 Gradle。这里还有另一个 Gradle 版本与默认 Check Style 插件版本的兼容性矩阵
checkstyle.xml插件使用的版本,则需要记住这一点 - 有一个插件版本与兼容checkstyle.xml版本的矩阵。对于谷歌检查风格,我发现最好的方法是检查插件版本。例如,如果我想8.2在项目中使用版本,build.gradle它的条目将如下所示:plugins {
id 'checkstyle'
}
checkstyle {
project.ext.checkstyleVersion = '8.20'
}
Run Code Online (Sandbox Code Playgroud)
这将自动使用(拉)checkstyle.xml与指定版本(即 8.20)的兼容。
checkstyle.xml与此插件版本兼容的版本,请转至Github 上的Google Checkstyle 版本并下载 8.20 版本zip 或 tar。解压后,在src/main/resources/目录中找到google(或sun)checkstyle xml。根据需要自定义 xml 后,您的build.gradle条目将如下所示
plugins {
id 'checkstyle'
}
checkstyle {
project.ext.checkstyleVersion = '8.20'
configFile = file("${rootDir.projectDir}/path/to/google_checks.xml")
}
Run Code Online (Sandbox Code Playgroud)
希望您不会再看到该错误。
| 归档时间: |
|
| 查看次数: |
8286 次 |
| 最近记录: |