我们有一个运行Checkstyle的Gradle脚本:
apply plugin: 'checkstyle'
checkstyle {
configFile rootProject.file('config/strict_checkstyle.xml')
ignoreFailures false
showViolations true
toolVersion = rootProject.ext.checkstyleVersion
}
task Checkstyle(type: Checkstyle) {
configFile rootProject.file('config/strict_checkstyle.xml')
source 'src'
ignoreFailures false
showViolations true
include '**/*.java'
classpath = files()
}
// adds checkstyle task to existing check task
afterEvaluate {
if (project.tasks.getByName("check")) {
check.dependsOn('checkstyle')
}
}
与此strict_checkstyle.xml
文件一起>
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE module PUBLIC
"-//Puppy Crawl//DTD Check Configuration 1.2//EN"
"http://www.puppycrawl.com/dtds/configuration_1_2.dtd">
<module name="Checker">
<module name="FileLength"/>
<module name="FileTabCharacter"/>
<module name="TreeWalker">
<module name="SuppressionXpathFilter">
<property name="file" value="config/app_checkstyle_suppressions.xml"/>
<property name="optional" value="false"/>
</module>
<module name="SuppressionXpathFilter">
<property name="file" value="config/api_checkstyle_suppressions.xml"/>
<property name="optional" value="false"/>
</module>
<!-- Checks for imports -->
<!-- See http://checkstyle.sf.net/config_import.html -->
<module name="AvoidStarImport"/>
<module name="IllegalImport"/>
<module name="RedundantImport"/>
<module name="UnusedImports"/>
...
如您所见,我们为两个gradle模块app
和api
设置了抑制基线。这适用于我们的CI机器和除一个之外的所有开发机器。他遇到此错误:
What went wrong:
Execution failed for task ':module:Checkstyle'.
> Unable to create Root Module: config {/Users/user/our_application/config/strict_checkstyle.xml}, classpath {null}.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
使用--stacktrace
运行会显示问题:
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':component:Checkstyle'.
...
Caused by: : Unable to create Root Module: config {/Users/user/our_application/config/strict_checkstyle.xml}, classpath {null}.
...
Caused by: com.puppycrawl.tools.checkstyle.api.CheckstyleException: cannot initialize module TreeWalker - Unable to find: config/app_checkstyle_suppressions.xml
...
Caused by: com.puppycrawl.tools.checkstyle.api.CheckstyleException: Unable to find: config/app_checkstyle_suppressions.xml
...
Caused by: java.io.FileNotFoundException: /Users/user/.gradle/daemon/5.1.1/config/app_checkstyle_suppressions.xml (No such file or directory)
Checkstyle或Gradle都在用户的系统 gradle目录中查找,而不是在我们自己的应用程序目录中查找。因此,我们让开发人员使用完整路径更新了checkstyle.xml:
<module name="SuppressionXpathFilter">
<property name="file" value="/Users/user/our_application/config/app_checkstyle_suppressions.xml"/>
<property name="optional" value="false"/>
</module>
然后一切正常。那么,为什么Checkstyle / Gradle无法识别config/app_checkstyle_suppressions.xml
是正确的相对路径?为什么仅在一台开发机上会发生这种情况?