1 minute read Published:

Gradle – Create a New File From Text

This feels like a feature that should be baked into Gradle or at least common, but nothing comes up when I search how to create a file on the fly. The requirements:

  • Create a file where one does not exist from generated text
  • Make it incremental

The following is my solution:

project.task('createFile') {
    inputs.property "myTextProperty", "any old text"
    outputs.file outputFile
        outputFile.write "some text"
        outputFile.write inputs.properties.myTextProperty

Using the inputs and outputs makes the task incremental and the doLast is required if you don’t want the file to be created during the configuration phase. You might also need to set the input property in the doLast block if the information is not available until execution. This was not necessary the last two time I was creating a file: Creating a version file from a hard coded string, and the last time I was using the Gradle classpath to generate a file.

How do you create a file on the fly?


Here is another solution I found.