在Win10环境编译spring-framework4.1.9版本会报错”Failed to capture snapshot of input files for task ‘distZip’ during up-to-date check.”,详细信息如下:
FAILURE: Build failed with an exception.
* What went wrong:
Failed to capture snapshot of input files for task 'distZip' during up-to-date check.
> java.io.FileNotFoundException: C:\study\spring-framework\build\distributions\spring-framework-4.1.9.BUILD-SNAPSHOT-schema.zip (系统找不到指定的文件。)
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
:distZip
BUILD FAILED
该错误和windows文件路径中的反斜杠\有关,打开build.gradle文件,在schemaZip这个task中,有两处文件路径的处理都只考虑了Linux环境(使用正斜杠),这段脚本在windows环境下使用带有正斜杠的路径无法匹配到对应的文件,如下图两个红框所示:
解决办法是修改schemaZip这个task的代码,如果是Windows环境就使用反斜杠,修改后的schemaZip源码如下,上图两个红框中的代码被替换,新增和修改的代码都带了注释:
task schemaZip(type: Zip) {
group = "Distribution"
baseName = "spring-framework"
classifier = "schema"
description = "Builds -${classifier} archive containing all " +
"XSDs for deployment at http://springframework.org/schema."
duplicatesStrategy 'exclude'
//当前系统是否是windows的标志
def isWindows = System.properties['os.name'].toUpperCase().contains('WINDOWS')
//不同的操作系统,表示子目录的符号是不同的
def schemaPath = isWindows ? "META-INF\\spring.schemas" : "META-INF/spring.schemas"
moduleProjects.each { subproject ->
def Properties schemas = new Properties();
subproject.sourceSets.main.resources.find {
it.path.endsWith(schemaPath)
}?.withInputStream { schemas.load(it) }
for (def key : schemas.keySet()) {
def shortName = key.replaceAll(/http.*schema.(.*).spring-.*/, '$1')
assert shortName != key
File xsdFile = subproject.sourceSets.main.resources.find {
//如果是windows环境,就要对路径中的分隔符做替换
isWindows ? it.path.endsWith(schemas.get(key).replaceAll('\\/','\\\\')) : it.path.endsWith(schemas.get(key))
}
assert xsdFile != null
into (shortName) {
from xsdFile.path
}
}
}
}
如上所示,相比原有代码一共修改了以下几处:
1. 用一个标志isWindows来代表当前是否是Windows环境;
2. 原有的字符串”META-INF/spring.schemas”,其中的正斜杠只有在非Windows环境才用,Windows环境下改用反斜杠;
3. schemas.get(key)返回的字符串中,如果带有正斜杠,在Windows环境下就全部替换成反斜杠;
修改完毕后再次编译构建,顺利完成;