Krishnaraj
Krishnaraj

Reputation: 2420

Gradle - how to create distZip without parent directory?

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
    |-- ...

Upvotes: 10

Views: 3290

Answers (2)

Tomas F
Tomas F

Reputation: 7596

In the distribution block you can define where in the archive the files should go. (example from Kotlin DSL)

distributions {
   main {
      contents {
          into("/")
      }
   }  
}

Upvotes: 6

Stanislav
Stanislav

Reputation: 28106

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.

Upvotes: 11

Related Questions