【问题标题】:Gradle zip: how to include and rename one file easily?Gradle zip:如何轻松包含和重命名一个文件?
【发布时间】:2017-12-13 07:28:27
【问题描述】:

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

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

filesMatching(...) 会明显影响性能。 有什么更好的方法?喜欢:

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

rename 不支持include

【问题讨论】:

    标签: gradle zip rename


    【解决方案1】:

    我完全不明白你为什么使用filesMatching。您只在您的孩子CopySpec 中包含一个文件。只需重命名它,一切都很好:

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

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

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

    【讨论】:

    • 对于正则表达式,它是否需要匹配整个文件名,或者java string.replaceAll(pattern,replacement)中使用的任何模式?谢谢。
    • docs 说:“与源正则表达式不匹配的文件将使用原始名称复制。”所以我认为任何匹配都足够了。
    【解决方案2】:

    如果最后一个不起作用,请尝试:

    rename ('a.java', 'b.java')
    

    【讨论】:

      猜你喜欢
      • 2010-12-22
      • 2010-09-28
      • 1970-01-01
      • 2015-06-20
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      • 2013-12-14
      • 2018-08-02
      相关资源
      最近更新 更多