ivenhov
(Daniel)
February 15, 2024, 1:30pm
1
I have a toolchain defined as
java {
toolchain {
languageVersion = JavaLanguageVersion.of(8)
}
}
and custom compile
def compileBasicMapiGenerator = tasks.register('compileBasicMapiGenerator', JavaCompile) {
classpath=
source = files("my/src/com/MapiBasicTypesGenerator.java","my/src/com/TaskType.java")
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
https://docs.gradle.org/current/userguide/toolchains.html
and
https://docs.gradle.org/current/dsl/org.gradle.api.tasks.compile.JavaCompile.html#org.gradle.api.tasks.compile.JavaCompile:javaCompiler
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
opened 01:32PM - 12 Apr 21 UTC
a:feature
in:toolchains
### Expected Behavior
When configuring a toolchain at project level (`java { to… olchain { … } }`), all JavaExec tasks in that project default to using that toolchain.
### Current Behavior
JavaExec tasks use the JVM the Gradle build has been launched with, irrespective of the project-level toolchain.
### Context
I've started using toolchains rather than asking devs to use a specific JDK for the project (I used to throw when `JavaVersion.current()` was an unexpected value), and discovered that while JavaCompile, Javadoc and Test tasks use the configured toolchain, JavaExec tasks don't, so this needs to be configured explicitly:
```kotlin
tasks.withType<JavaExec>().configureEach {
javaLauncher.set(javaToolchains.launcherFor(java.toolchain))
}
```
Fwiw, the application plugin configures its JavaExec task to use the project toolchain: https://github.com/gradle/gradle/blob/v6.8.3/subprojects/plugins/src/main/java/org/gradle/api/plugins/ApplicationPlugin.java#L167
This suggests docs for Gradle 8.6 are wrong or I’m missing something obvious
Can anybody confirm?
Thanks
Vampire
(Björn Kautler)
February 15, 2024, 1:42pm
2
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.
ivenhov
(Daniel)
February 15, 2024, 1:47pm
3
Thanks
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?
Cheers
Vampire
(Björn Kautler)
February 15, 2024, 3:11pm
4
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.