How to properly identify java toolchains required for gradle across different versions

Goal : our ci pipelines needs to implement a metrics on specific java toolchains required for gradle projects to run

There are many different ways in which java version/ java toolchain required for gradle can be specified

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(17)
    }
}

or

java {
    sourceCompatibility = JavaVersion.VERSION_1_8
    targetCompatibility = JavaVersion.VERSION_1_8
}

or could also be logical such as

def toolchainVersion() {
	if (project.hasProperty('testToolchain')) {
		return project.property('testToolchain').toString().toInteger()
	}
	return 17
}
java {
		toolchain {
			languageVersion = JavaLanguageVersion.of(toolchainVersion())
		}
	}

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?

The issue is , we need to know the java version in order to run the gradle command… which leads to a weird cyclic problem? is there a way to do this?

i was thinking along the lines of gradle –init-script=tellMeVersion.groovy :whichjava

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.

Thank you .

The issue is :

  1. no changes should be made in gradle files ( dont ask. :frowning: some requiremnt)
  2. exact java version needed for compilation should be installed and used ( once again dont ask :frowning: 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.