How should I make my plugin that adds class file instrumentation to the output of classes compiled by compileJava work in Gradle 9?
Before Gradle 9, this was simple and worked:
- change the compileJava output dir to an intermediate directory
- use that intermediate directory as input directory for my custom task
- use the original output directory (main/classes) as the output directory of my custom task
- finalize compileJava by calling my custom task
This does not work in Gradle 9 anymore because whenever the compileJava output dir is changed, the sourceset main destination dir instantly reflects that change. What happens is that my processed classes are stored in the correct location, but all tasks (for example jar, test) that work on the sourceset main output use the classes from the intermediate dir, which contains the unprocessed classes.
Solutions that do not work:
- process files in place: breaks task caching because only a single task may produce a specific output when task caching is enabled.
- have my task copy the generated class files to an intermediate directory before processing: same as above
- changing the default compile spec: hard coded in a private method
- changing compiler options: I didn’t try yet, but I think the output dir will be set when the compile task is run
- just have compileJava output to a new directory, and have my task write the classes write to the standard directory and then reconfigure every Gradle task that consumes sourceset main output to instead use my tasks output (not doable).
- simply changing the sourceset destination dir after task are created and wired: there’s no method (like setClassesDir()) to do that.
What is the recommended way to do this in Gradle 9, or does this simply not work?