rspec/rules/S6623/kotlin/rule.adoc

57 lines
1.6 KiB
Plaintext

== Why is this an issue?
Using the Kotlin Gradle DSL, a task can be defined in several ways:
* `tasks.create(...)` will eagerly configure the task, regardless of whether it is required.
* `tasks.register(...)` lazily configures the task only when it is required. This happens when it is located using query methods such as `TaskCollection.getByName(java.lang.String)`, when it is added to the task graph for execution, or when `Provider.get()` is called on the return value of this method.
It is generally more efficient to use `tasks.register(...)` instead of `tasks.create(...)` as the task will not be configured if it is not needed.
== How to fix it
Replace `tasks.create(...)` with `tasks.register(...)`.
=== Code examples
==== Noncompliant code example
[source,kotlin,diff-id=1,diff-type=noncompliant]
----
tasks.create("myTask") {
group = JavaBasePlugin.DOCUMENTATION_GROUP
description = "My task."
// other configuration logic
doLast {
// ...
}
}
----
==== Compliant solution
[source,kotlin,diff-id=1,diff-type=compliant]
----
tasks.register("myTask") {
group = JavaBasePlugin.DOCUMENTATION_GROUP
description = "My task."
// other configuration logic
doLast {
// ...
}
}
----
== Resources
=== Documentation
* https://docs.gradle.org/current/javadoc/org/gradle/api/tasks/TaskContainer.html[org.gradle.api.tasks.TaskContainer]
=== Standards
* https://docs.gradle.org/current/userguide/task_configuration_avoidance.html[Task configuration avoidance]
* https://www.youtube.com/watch?v=LvuVboGNVoE&list=PL0UJI1nZ56yAHv9H9kZA6vat4N1kSRGis&index=10[Configuration avoidance (Gradle best practice tip #8)]