Short Version
Q) How do I extend the compileJava task to make use of the LazyProvider API?
Details
I’m trying to have a task myGenerateJava
which generates java code, which compileJava()
should depend on. So far, the build works if I run the build twice. If I only run the build once, then compileJava fails because it can’t see all of the code generated from myGenerateJava.
Simply adding compileJava.dependsOn('myGenerateJava')
and compileJava.mustRunAfter('myGenerateJava')
doesn’t work, I think because the code that compileJava is meant to depend on doesn’t exist yet. I think, I am meant to make use of the lazy_configuration API to fix this problem as the descriptive text maps perfectly onto what I’m trying to do. [1]
However, the example given with a producer and consumer are both custom/bespoke tasks, whereas in my case, the producer is my own codegen, and the consumer is the out of the box compileJava task.
References
[1] https://docs.gradle.org/current/userguide/lazy_configuration.html
Many builds have several tasks connected together, where one task consumes the outputs of another task as an input. To make this work, we would need to configure each task to know where to look for its inputs and place its outputs, make sure that the producing and consuming tasks are configured with the same location, and attach task dependencies between the tasks. This can be cumbersome and brittle if any of these values are configurable by a user or configured by multiple plugins, as task properties need to be configured in the correct order and locations and task dependencies kept in sync as values change. The
Property
API makes this easier by keeping track of not just the value for a property, which we have seen already, but also the task that produces the value, so that you don’t have to specify it as well.