I have a simple “createFile” task that creates a file called “test.txt” in the project’s build folder.
task createFile (type:CreateFile) {
fileContents project['fileContents']
}
class CreateFile extends DefaultTask {
@Input String fileContents
@TaskAction doit() {
newFile.text = fileContents
}
@OutputFile File getNewFile() {
new File(project.buildDir, 'test.txt')
// project.files{project.buildDir.listFiles()}.filter {"*.txt"}.getSingleFile()
}
}
I’m using the @OutputFile annotation so that the task will be up to date on later runs.
My testing indicates that getNewFile is called multiple times before the task action (that actually creates the file) is called.
Suppose that the task action is instead a third-party process that creates a single *.txt file in the build folder. I must rewrite getNewFile to use something like what’s the getNewFile comment above. The problem is that this fails when getNewFile is called when this file doesn’t exist. What should getNewFile return is the case where it’s called before the task action is executed?