Automatic clean for a Copy task deletes content unrelated to it


I might be getting something wrong, but it seems that automatic clean of a copy task doesn’t do the right thing.

The problem is that if the “contents” directory in the example below already contained something before the copySomeStuff task is executed it will be deleted when the cleanCopySomeStuff is called. If one prints the outputs of the copy task it contains the directory instead of list of actually copied files.

task(copySomeStuff, type: Copy) {
      from configurations.runtime
      into "some/already/existing/destination/with/some/contents"
        outputs.files.each {
            println it
1 Like

The copy task (and friends) currently treat the entire contents of the destination directory as the outputs.

There are planned (but not scheduled) improvements to be made here:

What makes it worse is that it does not even seem possible to override the outputs of the copy task with the files being copied. In my case, even with something like

task unzipDependencies(type: Copy) {
    into '.'
      FileTree output
    configurations.compile.resolvedConfiguration.resolvedArtifacts.each { artifact ->
        if ('.zip')) {
            FileTree contents = zipTree(artifact.file)
            from contents
            if (output == null) {
                output = contents
            } else {
                output += contents
    outputs.files output

cleanUnzipDependencies would still delete everything under ‘.’.