is there any difference between `tasks.withType&lt...
# gradle
g
is there any difference between
tasks.withType<KotlinCompile>().configureEach {
and
tasks.withType<KotlinCompile> {
? It seems like the
configureEach
is redundant
f
withType<T>(action)
is equivalent to
withType<T>().all(action)
which is an eager API. Meaning, every task is realized and the action is immediately executed. Every task that is going to be added to the task collection is immediately realized and the action is executed.
withType<T>().configureEach(action)
on the other hand is a lazy API. No task gets realized and the action is not executed. When any of the tasks the action was registered for gets realized, the action is executed. As you can see, there's a big difference. Always prefer
withType<T>().configureEach(action)
unless you have a good reason not to.
👍 1
g
oh I see, thanks for the detailed explanation. does the same apply elsewhere too, like
tasks.test.configure {
vs
tasks.test {
?
seems like the sort of thing IntelliJ normally has inspections for if one is obviously preferred
f
No,
tasks.test { }
uses
named
in the background and is lazy. You can always use jump to source and check out what it does behind the scenes. 🙂 Totally agree! IntelliJ has lots of issues with many of the modern and more recent changes and additions in Gradle. I guess it doesn't have such a high priority in general.
👍 1
g
for others stumbling over this thread, the general concept / thing to search for here seems to be called "Task Configuration Avoidance" https://docs.gradle.org/4.9/userguide/task_configuration_avoidance.html
👍 1