Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle distribution - create an empty directory

Tags:

gradle

Is there a way to add empty directories (e.g, "logs") when creating a distribution with the gradle distribution plugin?

I saw this JIRA, describing the exact same thing. It is still open https://issues.gradle.org/browse/GRADLE-1671

I wonder if there are any workarounds I can use. I don't quite understand the workarounds described in the jira.

Thank you.

like image 595
Legato Avatar asked Apr 08 '15 12:04

Legato


People also ask

How do I create a directory in Gradle?

Creating directories All core Gradle tasks ensure that any output directories they need are created if necessary using this mechanism. As described in the Apache Ant manual, the mkdir task will automatically create all necessary directories in the given path and will do nothing if the directory already exists.

What does Gradlew installDist do?

You can run gradle installDist to assemble the uncompressed distribution into $buildDir/install/${project.name} .

What is FileTree in Gradle?

A FileTree represents a hierarchy of files. It extends FileCollection to add hierarchy query and manipulation methods. You typically use a FileTree to represent files to copy or the contents of an archive. You can obtain a FileTree instance using Project.


2 Answers

So I managed to work around this by following the suggestion in the mentioned JIRA to create a dummy empty directory and then copy it to the distribution location.

It's ugly but works. I'm sure it can be written more efficiently though. This is the Copy block from inside distributions/main/contents:

into('') {
    //create an empty 'logs' directory in distribution root
    def logDirBase = new File('/tmp/app-dummy-dir')
    logDirBase.mkdirs()
    def logDir = new File(logDirBase.absolutePath + '/logs')
    logDir.mkdirs()

    from {logDirBase}
}
like image 158
Legato Avatar answered Sep 28 '22 09:09

Legato


Based on Logato's own answer I've come up with the following code, which is more elegant and also closes the file pointer correctly (using the with context):

distributions {
    main {
        baseName = 'app'
        contents {
            into('') {
                File.createTempDir().with {
                    def tmpLog = new File(absolutePath, 'logs')
                    println tmpLog.absolutePath
                    tmpLog.mkdirs()
                    from (absolutePath) {
                        includeEmptyDirs = true
                    }
                }
                // ...
            }
            // ...
        }
    }
}
like image 38
Christian Avatar answered Sep 28 '22 09:09

Christian