Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I produce a ZIP of my source using Gradle?

I'm converting a relatively small project from Ant to Gradle. I expect to reduce the number of lines in the build script by approximately 75%!

One thing the Ant build does is to produce a source ZIP (i.e. a ZIP of the whole project, with certain bits removed - ./build, various Eclipse folders, etc.) Whilst migrating this to Gradle, I decided to use an "inclusive" approach, rather than an "exclusive" approach so that things don't get included by accident later on.

I'd like to be able to grab the source code and resources for all of the source sets without having to list the directories explicitly, but I can't get it working.

Here's what I have so far (doesn't even run!):

task srcZip(type: Zip) {
    classifier = 'src'
    from projectDir
    include {
        sourceSets.collect {
            it.allSource.asPath
        }
    }
}

The ZIP file should end up with folders 'src/main/java/...', 'src/main/resources/...', 'src/test/java/...', etc., and I shouldn't need to re-visit this task when I add more source sets later on.

Thanks in advance!

like image 836
dty Avatar asked Jan 27 '11 22:01

dty


4 Answers

To get all sources into 1 zip file you can use this:

task srcZip(type: Zip) {
    classifier = 'src'
    from sourceSets*.allSource
}

It won't give you the directory structure you asked for though. The files from all source sets are put into the same hierarchy.

To get what you asked for you can use this:

task srcZip2(type: Zip) {
    classifier = 'src'
    from projectDir
    include 'src/**/*'
}

Of course, it doesn't take into account any changes you might make to source directory locations.

like image 117
David Resnick Avatar answered Oct 07 '22 05:10

David Resnick


So, 21 months later, here's what I did to get this to work (with Gradle 1.2)

task srcZip(type: Zip) {
    classifier = 'src'
    from projectDir
    include sourceSets*.allSource.srcDirs*.collect { relativePath(it) }.flatten()
    include 'LICENCE', 'README', 'NOTICE', 'gradlew*'
}
assemble.dependsOn(srcZip)

My Groovy-fu is not strong, so I'm sure that horrible bit in the middle could be simplified, but it work for now!

like image 34
dty Avatar answered Oct 07 '22 05:10

dty


Here is another way how we can create the zip of the source code. We can include/exclude files as per requirement. Works in case of custom plugin that I was using and I spent a lot of time figuring out how to make this work. Finally I could achieve it using below code :

Task taskSourceZip = project.task("makeSourceZip", type:Zip){
        baseName = "sourceZip"
        from (project.rootDir){
            exclude ('*/*.iml')
            exclude 'sourceZip.zip'
        }
        destinationDir project.rootDir
    }
like image 44
sver Avatar answered Oct 07 '22 05:10

sver


Example from one of my projects:

task srcZip(type: Zip) {
    classifier = 'src'
    from projectDir
    include 'src/**/*'
    include '*.gradle'
    include 'README.md'
    doLast {
        println "Path to zip: $archivePath.path"
    }
}
like image 31
naXa Avatar answered Oct 07 '22 04:10

naXa