How to set @OutputFile value when it is created by a third-party process?

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?

What exactly do you mean when you say “third-party process”? Is this just some code that you are calling from you ‘@TaskAction’ method? Is the name of the file that the third-party code creates not configurable or determinable at configuration time?

Yes, by third-party I mean I’m making a command line call that creates the file within the @Taskaction section; therefore I don’t determine the file’s exact name - only some characteristics that I can filter on.

FYI, this problem arose when I was writing a task that uses that fpm command line tool to create an RPM.

If your task produces an indeterminate output then you should probably use the ‘@OutputDirectory’ annotation instead.

OK. I’ll try that. Thanks.