Triggering code generation task only when an input file timestamp is newer than the generated code

Greetings. I’m using the org.openapi.generator Gradle Plugin to generate Java classes based on an OpenAPI input spec in YAML format. When the generator gets invoked, it will spit out the Java source files in a directory which I’ve configured to be ${project.rootDir}/generated.

I’ve set the compileJava task to depend on the task that generates the code since there is Java code that uses the generated code. So the first time I compile, the OpenAPI generator gets invoked and creates the Java source files according to the input spec. Then the regular Java compilation takes place. If I recompile, the generator will not be invoked. We have a project requirement that the generated Java source files must be checked into our repo. However, when I check the generated files into git, the next time I run ./gradlew build, the OpenAPI generator gets invoked again.

All I want to accomplish is that the OpenAPI generator task ONLY gets invoked if the input spec file has a newer timestamp than the output directory, or if the output directory doesn’t exist. I have looked into the documentation for Gradle Incremental Builds, etc., and have tried implementing a Gradle task to somehow do it, but haven’t been able to wrap my head around it. I wish is was as simple as in the good old Make days… :stuck_out_tongue_winking_eye:
Any help with this is highly appreciated. Thanks.

The problem is, that the timestamps are useless.
If you for example switch back and forth branches, each time the changed files get touched, but you never know whether this has anything to do with their up-to-dateness.
If you properly configured input and outputs for the generation task, then it should only run if either inputs changed, or outputs changed.