Modifying parent paths in a Copy task creates unnecessary remnants

Whn the names of intermendiates are changied in a copy action, Gradle still creates the original relative path in the destination path, eventhough it would leave it empty… The behaviour can easlity be reproduced via the following short build script.

task foo << {
  [ 'abc','def' ] .each {
    File target = file("${buildDir}/source/${it}/_foo_/testfile")
    mkdir target.parentFile
    target.text = 'test'
  }
}

task copyFoo( type : Copy ) {
  dependsOn foo
  from "${buildDir}/source"
  into "${buildDir}/dest"
  eachFile { fcd ->
    fcd.path = fcd.path.replace('_foo_','bar')
  }
}

Run copyFoo and it will create the following filesystem structure:

build
build/dest
build/dest/abc
build/dest/abc/_foo_
build/dest/abc/bar
build/dest/abc/bar/testfile
build/dest/def
build/dest/def/_foo_
build/dest/def/bar
build/dest/def/bar/testfile
build/source
build/source/abc
build/source/abc/_foo_
build/source/abc/_foo_/testfile
build/source/def
build/source/def/_foo_
build/source/def/_foo_/testfile

Note the _foo_ directories under dest. They should not be there.

Gradle Version: 2.0 - 2.14
Operating System: Mac