Win10环境编译spring-framework4.1.9版本,报错"Failed to capture snapshot of input files for task 'distZip'"

在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环境下就全部替换成反斜杠;

修改完毕后再次编译构建,顺利完成;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员欣宸

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值