my current problem is the following: I would like to be able to detect which is the latest officially supported java version for the version of Gradle I am currently using for building some project.
I could not find an API for that, GradleVersion does not provide information about the maximum supported Java toolchain.
The only place where I found the information is in the Javadoc documentation of JavaVersion.java:
...
/**
* Java 16 major version.
*
* @since 6.3
*/
VERSION_16,
/**
* Java 17 major version.
* Not officially supported by Gradle. Use at your own risk.
*
* @since 6.3
*/
VERSION_17,
...
versions not yet supported have a Not officially supported by Gradle. Use at your own risk. line, but detecting the maximum supported version this way is too fragile for my scopes.
In other words, I'd like to able to write the following Kotlin extension function:
/**
* Returns 16 for Gradle 7.2, Returns 13 for Gradle 6.0, etc.
*/
fun GradleVersion.latestSupportedJava(): JavaVersion = ...
For my scopes, it is perfectly fine if the function can be defined only for Gradle 7.2+ (namely, if it uses an API that is not there in previous versions of Gradle).
I'm adding a detail: the less-worse method I've found to this moment is to scrape this URL: docs.gradle.org/current/userguide/compatibility.html that still is rather fragile. This function works, but, as said, is fragile by design, even forgetting that scraping pages via Regex is not a good idea in general.
fun latestSupportedJava(): JavaLanguageVersion =
Regex("""<tr.*>[\s|\n|\r]*<td.*>.*?(\d+).*?<\/td>[\s|\n|\r]*<td.*>.*?(\d+(?:\.\d+)).*?<\/td>""")
.findAll(URL("https://docs.gradle.org/current/userguide/compatibility.html").readText())
.map {
val (javaVersion, gradleVersion) = it.destructured
JavaLanguageVersion.of(javaVersion) to GradleVersion.version(gradleVersion)
}
.filter { (_, gradleVersion) -> GradleVersion.current() >= gradleVersion }
.maxByOrNull { (_, gradleVersion) -> gradleVersion }
?.first
?: JavaLanguageVersion.of(16)
Update: I opened a feature request to the Gradle folks.