I need to convert an existing plugin to work with Java Toolchains.
I tried converting an existing custom task to use the Worker API and using processIsolation on the WorkerExecutor such that I can provide the toolchain executable.
I thought this would work, but the task I converted needs access to the Project. I tried to supply this as a Property, but DefaultProject is not serializable.
Can you recommend an alternative approach for executing a task using Java Toolchains when that task requires access to the Project?
Besides that, if your task action needs the Project instance, this is bad practice anyway. With enabled STABLE_CONFIGURATION_CACHE feature preview also in Gradle 7 it is also deprecated to use the Project at execution time and latest if you try to use the configuration cache it is forbidden to use the Project at execution time.
So you should anyway refactor your task to remove that need.
Thanks very much for the fast and detailed response. @Vampire , just to be clear, we should not use the Project, for example, on any method annotated with @TaskAction on a Task?
Exactly.
Just enable the STABLE_CONFIGURATION_CACHE feature preview and you will get a deprecation warning if you do.
Or try to use the configuration cache (even though it is not officially stable in 7.6.4 yet) and it will fail.
I’m not sure what you mean.
If you mean you want to execute one task from another task, this never was supported.
In oooold Gradle version there was an execute method on Task but this was never intended to be public API and is long gone and should never have been used due to many reasons.