Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle task to create a zip archive of a directory

I have a gradle task to create a zip archive of a directory. The gradle task is:

task archiveReports(type: Zip) {
   from '/projects/Reports/*'
   archiveName 'Reports.zip'
}

When I am running the command 'gradle archiveReports', its showing the build is successful. However, no zip file is getting created.

Am I missing something here?

like image 574
Praveen Avatar asked Apr 26 '16 11:04

Praveen


People also ask

How do I create a zip file in Gradle?

In Gradle, zip files can be created by using 'type: zip' in the task. In the below example, from is the path of source folder which we want to zip. destinationDir is the path of the destination where the zip file is to be created.

What is Ziptree in Gradle?

A FileTree represents a hierarchy of files. It extends FileCollection to add hierarchy query and manipulation methods. You typically use a FileTree to represent files to copy or the contents of an archive. You can obtain a FileTree instance using Project.

What does the wrapper task do?

Using the wrapper task ensures that any optimizations made to the Wrapper shell script or batch file with that specific Gradle version are applied to the project. As usual, you should commit the changes to the Wrapper files to version control. Note that running the wrapper task once will update gradle-wrapper.

What is ProcessResources in Gradle?

Class ProcessResourcesCopies resources from their source to their target directory, potentially processing them. Makes sure no stale resources remain in the target directory.


3 Answers

I figured out a way for this: Its working for me now.

task myZip(type: Zip) {    from 'Reports/'    include '*'    include '*/*' //to include contents of a folder present inside Reports directory    archiveName 'Reports.zip'    destinationDir(file('/dir1/dir2/dir3/')) } 
like image 115
Praveen Avatar answered Nov 02 '22 14:11

Praveen


With Gradle 6.7,

task packageDistribution(type: Zip) {
    archiveFileName = "my-distribution.zip"
    destinationDirectory = file("$buildDir/dist")

    from "$buildDir/toArchive"
}

Note : archiveName is deprected.

like image 36
Jafar Karuthedath Avatar answered Nov 02 '22 15:11

Jafar Karuthedath


With Kotlin DSL

tasks.register<Zip>("packageDistribution") {
    archiveFileName.set("my-distribution.zip")
    destinationDirectory.set(layout.buildDirectory.dir("dist"))

    from(layout.buildDirectory.dir("toArchive"))
}

With Groovy

tasks.register('packageDistribution', Zip) {
    archiveFileName = "my-distribution.zip"
    destinationDirectory = layout.buildDirectory.dir('dist')

    from layout.buildDirectory.dir("toArchive")
}

Taken from the official docs

like image 34
Joker Avatar answered Nov 02 '22 15:11

Joker