Modifying file in place generates a warning


(Andreas Axelsson) #1

I have a task which modifies a file in place, so the task file property is annotated with both @InputFile and @OutputFile, but in gradle 4.5 this generates a warning about conflicting property types. How can I avoid it? The file must be modified in place, for various reasons.

For details, it reads the file, modifies it according to certain rules, then writes it back out.


(Sterling Greene) #2

The easiest thing to do would be to remove all @OutputXXX annotations (or all @InputXXX or @OutputXXX annotations). The task can never be incremental/up-to-date, so the annotations aren’t useful.


(Andreas Axelsson) #3

The task uses its own OnlyIf logic to check if the modification is in place, so it will skip it if everything is up to date, but it needs to know If someone else has modified the file too. When I wrote it, some time ago, the combination of input/output/onlyif was the only thing that worked, but I’ll take another look at it.


(Andreas Axelsson) #4

Here’s a cut-down version:

class MyTask extends DefaultTask {
    @Input String someOption
    @InputFile @OutputFile File configFile
    MyTask() {
        onlyIf {
            return checkIfConfigFileHasOption()
        }
    }
    @TaskAction() void run() {
        setOptionInFile()
    }
}

So I want the task to run if someone else has modified it, or if the someOption property has changed, but only if it doesn’t already contain the right option. Other tasks depending on changes in the configFile should always run if it’s been modified. After removing @OutputFile and extending my test suite, it seems like things still work, so I guess I’m good for now.