rewrite icon indicating copy to clipboard operation
rewrite copied to clipboard

Recipe: Convert task ... to tasks.register(...) in build.gradle files for lazy registration

Open Jenson3210 opened this issue 8 months ago • 5 comments

What problem are you trying to solve?

Gradle recommends using lazy task registration (tasks.register(...)) over eager task creation (task ...) to avoid unnecessary initialization during the configuration phase, which can improve build performance and is better aligned with modern Gradle best practices. IntelliJ IDEA does offer autofix suggestions for this change, but only on a per-line basis—mass cleanup across a project is not supported via code suggestions.

What precondition(s) should be checked before applying this recipe?

None—this change can be applied universally across all Gradle versions that support the tasks.register(...) syntax.

Describe the situation before applying the recipe

task allDeps(type: DependencyReportTask)

or

task allDeps(type: DependencyReportTask) { 
     group = 'reporting'
     description = '...'
     // additional configuration
}

Describe the situation after applying the recipe

tasks.register("allDeps", DependencyReportTask)

or

tasks.register("allDeps", DependencyReportTask) { 
     group = 'reporting'
     description = '...'
     // additional configuration
}

Have you considered any alternatives or workarounds?

This can be done manually or line-by-line using IntelliJ's quick fixes, but there is currently no automated way to perform this change across an entire codebase or multiple projects.

Any additional context

This change aligns with modern Gradle practices. While IntelliJ offers autofixes for single lines, a recipe in OpenRewrite would enable automated and large-scale migration of legacy Gradle scripts to use lazy task registration. It should be relatively straightforward to implement when needed.

Are you interested in contributing this recipe to OpenRewrite?

Not at this time, unless prompted by customer need.

Jenson3210 avatar Apr 23 '25 10:04 Jenson3210

Hi @Jenson3210, I'm interested in working on this issue if it's available.

jhl221123 avatar May 24 '25 12:05 jhl221123

Sure! Nothing here is mine 😉 If you need assistance, feel free to reach out!

Jenson3210 avatar May 24 '25 12:05 Jenson3210

  • Fix PR by @jhl221123 : https://github.com/openrewrite/rewrite/pull/5492

injae-kim avatar May 28 '25 13:05 injae-kim

@Jenson3210 is this issue still open ?

Attyuttam avatar Nov 26 '25 05:11 Attyuttam

  • We have https://github.com/openrewrite/rewrite/pull/5492 to address this one. @timtebeek can you give that one another review so we can close this whenever you have some time?

Jenson3210 avatar Nov 26 '25 08:11 Jenson3210