Search code examples
buildgradleresource-cleanup

Clean task does not clean up specified outputs.file


I wrote a build.gradle script to automatically download hazelcast from a given URL. Afterwards the file is unzipped and only the mancenter.war as well as the origin zip file remains in the destination directory. Later on this war file is taken referenced for a jetty run.

Nevertheless, although I defined outputs.file for two of my tasks, the files do not get cleaned up when I execute gradle clean. Thus I'd like to know what I have to do that the downloaded and unzipped files get removed when I execute gradle clean. Here is my script:

Btw., if you have any recommendations how to enhance the script, please don't hesitate to tell me!

apply plugin: "application"

dependencies {
    compile "org.eclipse.jetty:jetty-webapp:${jettyVersion}"
    compile "org.eclipse.jetty:jetty-jsp:${jettyVersion}"
}

ext {
    distDir = "${projectDir}/dist"

    downloadUrl = "http://download.hazelcast.com/download.jsp?version=hazelcast-${hazelcastVersion}"
    zipFilePath = "${distDir}/hazelcast-${hazelcastVersion}.zip"
    warFilePath = "${distDir}/mancenter-${hazelcastVersion}.war"

    mainClass = "mancenter.MancenterBootstrap"
}

task downloadZip() {
    outputs.file file(zipFilePath)
    logging.setLevel(LogLevel.INFO)
    doLast {
        ant.get(src: downloadUrl, dest: zipFilePath)
    }
}

task extractWar(dependsOn: downloadZip) {
    outputs.file file(warFilePath)
    logging.setLevel(LogLevel.INFO)
    doLast {
        ant.unzip(src: zipFilePath, dest: distDir, overwrite:"true") {
            patternset( ) {
                include( name: '**/mancenter*.war' )
            }
            mapper(type:"flatten")
        }
    }
}

task startMancenter(dependsOn: extractWar, type: JavaExec) {
    main mainClass
    classpath = sourceSets.main.runtimeClasspath
    args warFilePath
}

UPDATE

I found this link, which describes how to provide additional locations to delete when the clean task is invoked. Basically you can do sth. like this:

clean{
    delete zipFilePath
    delete warFilePath
}

Solution

  • I got confirmation from the source code that the clean task simply deletes the build directory. It assumes that you want to clean everything, and that all the task outputs are somewhere in this build directory.

    So, the easiest and best practice is to only store outputs somewhere under the build directory.