Lennart Jörelid
02/26/2024, 10:37 AMVampire
02/26/2024, 11:15 AMLennart Jörelid
02/26/2024, 11:30 AM/**
* Convenience method to set the [JavaVersion] to use within this project.
*
* @param javaVersion The Java version to use, both for source and target compatibility.
*/
fun useJavaVersion(javaVersion: JavaVersion) = withJavaVersion.set(javaVersion)Lennart Jörelid
02/26/2024, 11:31 AMourJavaStandards {
useJavaVersion(JavaVersion.VERSION_21)
}
... which is readable, typesafe and works well, I think.Vampire
02/26/2024, 11:36 AMHow would that "wire to a function call within JavaPluginExtension"? Just to set a property in your extension, I would not use a function. Unless of course you like the resulting DSL more, then it would be fine, but still not solve the question, would it?fun useJavaVersion(javaVersion: JavaVersion) = withJavaVersion.set(javaVersion)
Lennart Jörelid
02/26/2024, 11:38 AMLennart Jörelid
02/26/2024, 11:41 AMafterEvaluate block to find a time when the property from my extension has been given a chance to be set by users in the build file .... and then call the corresponding non-lazy methods in some other pluginsLennart Jörelid
02/26/2024, 11:43 AMmap{ ... } directive
2. If either extension does not use Lazy Properties, use the afterEvaluate { ... } block to call the functions in the target (non-lazy) extensionVampire
02/26/2024, 11:57 AMIt sets a property for use within my plugin.
That plugin would then read the configured value and use it to set the corresponding property within the Kotlin and Java (and others) plugins to harmonize the configuration to one single place.But where in your plugin? In the
apply is too early, as the consumer would not have set the value yet, afterEvaluate is evil.
So the recommendation was to do the necessary configuration in that method the consumer is calling.
Using afterEvaluate, as described in your other thread, is just symptom treatment and the main thing it does is introducing ordering problems, timing problems, and race conditions, which is why using it is discouraged bad practice.
Btw. that single place in this case should be the JVM toolchain, which should then be taken into account by all tasks and also by the Kotlin Gradle plugin. :-)Lennart Jörelid
02/26/2024, 12:05 PMVampire
02/26/2024, 12:13 PM*Compatibility defaults in your apply method and then that function can re-configure it as needed and thus you get the behavior you want.
It heavily depends on the concrete case.Lennart Jörelid
02/26/2024, 12:16 PMLennart Jörelid
02/26/2024, 12:16 PMLennart Jörelid
02/26/2024, 12:16 PMVampire
02/26/2024, 12:37 PMLennart Jörelid
02/26/2024, 12:42 PMVampire
02/26/2024, 1:34 PM