Default toolchain ignored by JavaExec and JavaCompile

I have a toolchain defined as

java {
	toolchain {
		languageVersion = JavaLanguageVersion.of(8)

and custom compile

def compileBasicMapiGenerator = tasks.register('compileBasicMapiGenerator', JavaCompile) {
    source = files("my/src/com/","my/src/com/")
    destinationDirectory = file("build/basicMapiGenerator") 

When launched I get info

2024-02-15T13:20:18.964+0000 [INFO] [org.gradle.jvm.toolchain.internal.DefaultToolchainJavaCompiler] Compiling with toolchain '/Users/daniel/.sdkman/candidates/java/17.0.9-tem'.

As far as I understand

specified toolchain should be used

When I add

tasks.withType(JavaCompile).configureEach {
    javaCompiler = javaToolchains.compilerFor {
        languageVersion = java.toolchain.languageVersion

correct toolchain for Java 8 is used

Also I found this

This suggests docs for Gradle 8.6 are wrong or I’m missing something obvious
Can anybody confirm?


You are right.

The toolchain gets configured for all Javadoc tasks and all Test tasks, but only for the JavaCompile tasks of each source set, but not for custom registered JavaCompile tasks.

Whether this is a doc bug or an implementation bug, I have no idea.


Is the code

tasks.withType(JavaCompile).configureEach {

tasks.withType(JavaExec).configureEach {

the best way do reconfigure it then?
I think there is a way to specify conventions (which I haven’t looked into yet) but I’m not sure if that would apply to all tasks?


To configure all tasks of type JavaCompile and JavaExec this is fine, yes.
You will though for example not match cases where javaExec { ... } is used, which is what the issue you linked to talks about when it says that some infrastructure is missing afair.