Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between `tasks.named('test')` and `test`

Tags:

gradle

groovy

I noticed that https://start.spring.io now uses

tasks.named('test') {
    useJUnitPlatform()
}

When it used to be

test {
    useJUnitPlatform()
}

I was wondering why the change? The second one seems cleaner even on the IDE.

These are both groovy Gradle no Kotlin

like image 796
Archimedes Trajano Avatar asked Oct 25 '25 01:10

Archimedes Trajano


1 Answers

This change has been made to declare tasks lazily, see this Gradle page describing the "Configure avoidance" principles : https://docs.gradle.org/current/userguide/task_configuration_avoidance.html

Especialy this part which explains the difference between your two examples ( someTask is test in your case):

Beware of the hidden eager task realization. There are many ways that a task can be configured eagerly. For example, configuring a task using the task name and a DSL block will cause the task to immediately be created when using the Groovy DSL

// Given a task lazily created with
tasks.register("someTask")

// Some time later, the task is configured using a DSL block
someTask {
     // This causes the task to be created and this configuration to be executed immediately
}

Instead use the named() method to acquire a reference to the task and configure it:

tasks.named("someTask") {

}

( related commit Spring initializr project : https://github.com/spring-io/initializr/commit/27fc9d40672496c93742f562b13544d9a27cd484 )

like image 103
M.Ricciuti Avatar answered Oct 26 '25 19:10

M.Ricciuti



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!