what's the difference between the two? (Except the...
# gradle
e
what's the difference between the two? (Except the first one defines for a single task and the latter for multiple ones)
Copy code
named<KotlinCompilationTask<*>>("compileKotlin") { 
    compilerOptions { } 
}
withType<KotlinCompile<*>>().all {
    kotlinOptions { }
}
I'm looking to set things such as opt-in, language version, etc
j
compileKotlin is only configuring that specific task, if there are more compile kotlin tasks the shouldn’t be configured. The second one is configuring all tasks which extend that class
e
Yeah, and other than that?
j
If you check the IDE gradle tasks window you will see there are multiple compile kotlin tasks
for tests
KMP has more too if i remember correctly
so I am not sure if you get the same behavior doing withType as you can do:
Copy code
tasks.compileKotlin { … }
tasks.compileKotlinTest { … }
v
Two notable differences are, that you use the old
kotlinOptions
vs the new recommended
compilerOptions
, and that by using
withType<...>().all { ... }
like when using
withType<...> { ... }
you are destroying task-configuration avoidance for those tasks and should instead do
withType<...>().configureEach { ... }
.
j
IMO
withType<>{}
should use
configureEach
under the hood
For non Gradle expert it can be a problem to know these details
Even for people which work with Gradle, I do some search when I work in my plugins to check if I forgot some
configureEach
some times
v
Yes, but that will break builds out there as
withType<> {}
was there before the lazy APIs existed and thus cannot be changed backwards compatibly.
j
Hope Gradle 9 do that even breaking backwards compatibility, if not Gradle will be hard for newbies
v
I don't really think it will. You cannot really deprecate it. And simply changing the behavior will be bad. You would probably need to deprecate the whole method and instead provide some other or none. But besides that, the non-lazy is currently basically just relevant for task collections. Most other collections are not really used lazily anyway.
You can even break things. I remember one change where they did something lazily and then the functionality did not work anymore at all, because the container was then never queried for the values.
t
My 2 cents -
kotlinOptions
is semi-deprecated, better to use
compilerOptions
. Actually in
1.9.0
it will expose
optIn
and
progressive
options while
kotlinOptions
not. And in
1.9.0
project level DSL for "org.jetbrains.kotlin.jvm" plugin is coming based on `compilerOptions`:
Copy code
kotlin {
    compilerOptions { ... }
}
e
nice to read that ps: how may one discover what is becoming deprecated?
t
it is not so easy to deprecate 😅 Hopefully in 1.9.20 I will add
@Deprecated
to it
but it will stay for a long time as too many SO answers or posts has
kotlinOptions
in code snippets
v
chore: edit them 😄
t
ain't got time for that 😅
v
Perfect job for the next intern 😄