Search code examples
pluginsconfigurationgradlezip

How to cherry pick ZIP contents with Gradle Distribution plugin?


My Gradle build currently produces the following directory structure under a build dir in my project root:

myapp/
    src/
    build.gradle
    build/
        docs/
            groovydoc/* (all Groovydocs)
        libs/
            myapp-SNAPSHOT.jar
            myapp-SNAPSHOT-sources.jar
        reports/
            codenarc/
                main.html
        test-results/* (JUnit test results)

I would like to add the distribution plugin (or anything that accomplishes my goals, really) to have Gradle produce a ZIP file with the following directory structure:

myapp-SNAPSHOT-buildreport.zip/
    tests/
        (JUnit tests from build/test-results above)
    reports/
        main.html (CodeNarc report from build/reports/codenarc above)
    api/
        (Groovydocs from build/docs above)
    source/
        myapp-SNAPSHOT-sources.jar (from build/libs above)
    bin/
        myapp-SNAPSHOT.jar (from build/libs above)

After reading the plugin's documentation, I can't tell how to configure it to suit these needs. Its obvious that I need to run gradle distZip, but as to how to actually configure it to produce the desired directory structure, it doesn't seem to provide any documentation/examples. Any ideas?

Note: The JAR's version is obviously SNAPSHOT, and is passed into the Gradle build with a -Pversion=SNAPSHOT command-line argument.


Solution

  • I would probably not use the distribution plugin and instead just create a new custom Zip task. It would look something like this:

    task buildreportZip(type: Zip, dependsOn: build) {
        classifier = 'buildreport'
    
        from('build/test-results') {
            into 'tests'
        }
    
        from('build/reports/codenarc') {
            into 'reports'
        }
    
        from('build/docs') {
            into 'api'
        }
    
        from(sourcesJar) { // or whatever you source jar task name is
            into 'source'
        }
    
        from(jar) {
            into 'bin'
        }
    }