Given such varying ways/mechanism i was thinking if a gradle task can be developed that can tell us what is the version of java required?
There are many more places involved, and you can also require version X on task A and version Y on task B.
You can surely easily have some task that reports the toolchain configured on the java extension or the configured compatibility on the java extensions.
But each task can have a different compatibility set, or (always better than compatibility) release set, or a different toolchain configured.
So I don’t think there is really a good way to determine the toolchains needed.
The issue is , we need to know the java version in order to run the gradle command
Why?
The exact sense to use the Java toolchains feature is to decouple the Java version used to run Gradle from the Java version used to compile and run your production code.
So if you for example configured a Java 17 toolchain and run Gradle with Java 8, this just works like a charm.
Since recent versions you can even configure the toolchain used to run the Gradle build (well, actually the daemon) for example if you use some plugin or library in your build logic that needs some minimum Java version to run. Then the version used to start Gradle is only the one used for the CLI process that is only responsible to get input from the user and display output to the user.
no changes should be made in gradle files ( dont ask. some requiremnt)
exact java version needed for compilation should be installed and used ( once again dont ask some requirement)
For what ever purpose i need to abide by the above two rules.( i have tried fighting it.. but alas)
One thing you said piqued my interest
You can surely easily have some task that reports the toolchain configured on the java extension or the configured compatibility on the java extensions.
The exact sense to use the Java toolchains feature is to decouple the Java version used to run Gradle from the Java version used to compile and run your production code.
Putting the two together, i suppose it is possible to install a version of java ( supported by the gradle version in use) and then execute a gradle command ( via init script) that can tell me the java toolchain requirements? atleast as much as we can ?
Again, no you can’t unless you know exactly what you might set where.
You can set a toolchain on the java extension for all tasks that consider it.
You can also set a toolchain on each individual task.
You can also set the compatibility on the extension.
You can also set the compatibility on each individual task.
You can also set the release on the Java compile task.
You can also set a specific free-text executable on a task.
…
So unless you know exactly which task you are interested in and how you might set it, you will have a hard time to figure out a generic solution.