Force creation of empty archive

I’m using the Zip task in my build to bundle some files as a zip archive, which is part of the deliverables. It is possible for the input directory to be empty, in which case the Zip task does nothing, with an outcome of NO-SOURCE. This is probably alright in many cases; in my case however, I really need the zip file to be created - it’s fine if it is empty (that is, a valid zip file without any entries), but it needs to exist.

Is there any way to achieve this? Setting includeEmptyDirs to true makes no difference.

The task as a “skip if empty” input.
You probably need to trick it with something like

val foo by tasks.registering(Zip::class) {
    from(buildscript.sourceFile)
    eachFile {
        if (file == buildscript.sourceFile) {
            exclude()
        }
    }
}

That works, thanks. It does seem a bit ugly though, and not at all obvious; I’d prefer if the “skip when empty” behavior of the task could toggled directly. Do you think I should create an issue for this?

If there is none, probably doesn’t hurt, then you at least learn the opinion of the Gradle folks about it. :slight_smile:

Issue created: https://github.com/gradle/gradle/issues/33019

1 Like