Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

rename

gradle

zip

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.

like image 671
eastwater Avatar asked Oct 29 '25 16:10

eastwater


1 Answers

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'
}
like image 169
Lukas Körfer Avatar answered Oct 31 '25 08:10

Lukas Körfer