Gradle - how to create distZip without parent dire

2019-04-08 13:43发布

问题:

Gradle's distZip task creates a zip with the following structure

MyApp.zip
`-- MyApp
    |-- bin
    |   |-- ...
    `-- lib
        |-- ...

how to skip the parent directory and just zip the files like below

MyApp.zip
|-- bin
|   |-- ...
`-- lib
    |-- ...

回答1:

It's not possible by default, but it is possible to travrse all the files, which will be included into the final zip and modify it's destination path in this zip, as:

distZip {
    eachFile { file ->
        String path = file.relativePath
        file.setPath(path.substring(path.indexOf("/")+1,path.length()))
    }
}

Here is the additional distZip task's configuration added, which modifies each file's destination path within the final zip-archive, deleting the root folder from it. In your case, it will delete the MyApp folder from the zip.



标签: gradle