Alex Rivera | Logout

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

Asked 2011-01-27T22:11:49.607
16

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!

Edit
Report

1 Answer

8

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!

answered 2012-09-20T08:16:46.267

Your Answer