How can I copy an empty directory structure?Copy task is skipped 'as it has no source files'

How can I copy an empty directory structure? Copy task is skipped

the log ( gradle -i) says

Skipping task ‘:’ as it has no source files’

Warning! Warning! Warning!

Serious hack about to be presented. There should be an easier way, but defeating the copy task’s desired to actually want at least one file to copy is not straight-forward. There must be a better pattern, but this at least works, although in order to catch new/removed directories you have to force it to always run (otherwise it’ll think it is up-to-date, as the checks are only done against changed files)

task mySync(type: Sync) {
  from 'bogus'
  includeEmptyDirs = true
    from 'empty'
  into "$buildDir/sync"
    doFirst {
    // Some totally unique name to exclude upon
    exclude 'bogus.txt'
  }
    outputs.upToDateWhen { false }
}
ROOT
|
 build.gradle
|
+---bogus
|
     bogus.txt
|
\---empty
    +---lower
    |
 \---waydown
    \---other

After running gradle mySync you should find just the empty directories copied to where you’d like.

ROOT
|
 build.gradle
|
+---bogus
|
     bogus.txt
|
+---build
|
 \---sync
|
     +---lower
|
     |
 \---waydown
|
     \---other
\---empty
    +---lower
    |
 \---waydown
    \---other

A Copy task would also work assuming directories were never removed between invocations. The upToDateWhen() is simply to be able to catch the case where a new empty subdirectory is added (or removed), since the checks only examine file hashes.

If you have at least one file in your directory structure, then simply setting includeEmptyDirs = true would be sufficient for a Copy or Sync task, but that did not seem to be your issue.

-Spencer

Add this to a ‘doLast’ block of your task:

["empty/lower/waydown/",
  "empty/other/", ]
  .each { new File(it).mkdirs() }