Gradle zip:如何轻松地包含和重命名一个文件?

eas*_*ter 2 zip rename gradle

创建一个ZIP文件添加"hello/world.xml"目录下"foo/bar"的"hello/universe.xml"

task myZip(type: Zip) {
     from ("foo/bar") {
         include "hello/world.xml"
         filesMatching("hello/*") {
             it.path = "hello/universe.xml"
         }
     }
}
Run Code Online (Sandbox Code Playgroud)

filesMatching(...)会明显影响表现.什么是更好的方法?喜欢:

task myZip(type: Zip) {
     from ("foo/bar") {
         include ("hello/world.xml") {
              rename "hello/universe.xml"
         }         
     }
}
Run Code Online (Sandbox Code Playgroud)

但rename不支持include.

Luk*_*fer 6

我不知道你为什么要使用它filesMatching.您只在孩子中包含一个文件CopySpec.只需重命名,一切都很好:

task myZip(type: Zip) {
    from ('foo/bar') {
        include 'hello/world.xml'
        rename { 'hello/universe.xml' }
    }
}
Run Code Online (Sandbox Code Playgroud)

如果要包含多个文件(甚至全部复制),但只想重命名其中一个文件,请使用正则表达式指定要重命名的文件作为第一个参数:

task myZip(type: Zip) {
    from 'foo/bar'
    rename 'hello/world.xml' 'hello/universe.xml'
}
Run Code Online (Sandbox Code Playgroud)