Search code examples
gradlezipsoftware-distribution

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!


Solution

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