Gradle zip: how to include and rename one file easily?

末鹿安然 提交于 2019-12-11 01:29:38

问题


Create a zip adding file "hello/world.xml" under directory "foo/bar" as "hello/universe.xml"

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

filesMatching(...) will impact performance obviously. What is a better way? like:

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

But rename is not supported with include.


回答1:


I don't get why you are using filesMatching at all. You are only including one single file in your child CopySpec. Simply rename it and everything is fine:

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

If you want to include multiple files (or even copy all), but only want to rename one of them, specify which file(s) to rename with a regular expression as first argument:

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



回答2:


If the last one did not work, try:

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


来源:https://stackoverflow.com/questions/44997196/gradle-zip-how-to-include-and-rename-one-file-easily

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!