Gradle 无法创建根模块:config{…config/strict_checkstyle.xml},classpath{null}
我们有一个运行Checkstyle的Gradle脚本:Gradle 无法创建根模块:config{…config/strict_checkstyle.xml},classpath{null},gradle,checkstyle,Gradle,Checkstyle,我们有一个运行Checkstyle的Gradle脚本: apply plugin: 'checkstyle' checkstyle { configFile rootProject.file('config/strict_checkstyle.xml') ignoreFailures false showViolations true toolVersion = rootProject.ext.checkstyleVersion } task Checksty
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\u 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"/>
...
使用--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\u Checkstyle\u suppressions.xml
是正确的相对路径呢?为什么这只会发生在一台dev机器上 Checkstyle始终使用给定的路径。如果给它一个相对路径,它将使用相对路径。看见我只能假设gradle正在更改为系统目录。Checkstyle始终使用给定的路径。如果给它一个相对路径,它将使用相对路径。看见我只能假设gradle正在更改系统目录。
<module name="SuppressionXpathFilter">
<property name="file" value="/Users/user/our_application/config/app_checkstyle_suppressions.xml"/>
<property name="optional" value="false"/>
</module>