windowsgradletemporary-directory

Create a temporary directory for multiple gradle tasks


I am working with gradle in a windows environment. I wish for my gradle script to create a temporary directory, different tasks to perform operations on it in various orders, and then delete it upon completion (optional). How can I create this temporary dir in the gradle script?

My initial thought was Task.temporaryDir, however that directory only persists for the duration of the task.

Apologies if this is obvious, I am new to working on Windows and just picked up gradle yesterday.


Solution

  • If you need to create a temporary directory outside of project structure it can be done with adding:

    def tempDir = File.createTempDir()
    tempDir.deleteOnExit() // use with care, might be problematic with when daemon mode is used
    

    e.g. somewhere at the beginning of the script.

    Then the folder created can be used as an output directory elsewhere.