openrewrite / openrewrite/rewrite

Replace `tasks.withType<...> { ... }` with `tasks.withType<...>().configureEach { ... }` to support Gradle configuration avoidance

Open
#3,694 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

gradle groovy recipe
Dominant language
Java
Stars
3.7k
Forks
570
Avg merge
13h 12m
Merged PRs (30d)
261

Description

What problem are you trying to solve?

tasks.withType<...> {...} does not support configuration avoidance, and it is almost always should be replaced with tasks.withType<...>().configureEach {...}

See https://docs.gradle.org/current/userguide/task_configuration_avoidance.html#sec:old_vs_new_configuration_api_overview

The same could be done for Groovy DSL.

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

Gradle should be 4.9+

Describe the situation before applying the recipe

tasks.withType<Javadoc> {
    // generated ANTLR sources violate doclint
    (options as StandardJavadocDocletOptions).addStringOption("Xdoclint:none", "-quiet")

    // Items besides JavaParser due to lombok error which looks similar to this:
    //     openrewrite/rewrite/rewrite-java/src/main/java/org/openrewrite/java/OrderImports.java:42: error: cannot find symbol
    // @AllArgsConstructor(onConstructor_=@JsonCreator)
    //                     ^
    //   symbol:   method onConstructor_()
    //   location: @interface AllArgsConstructor
    // 1 error
    exclude("**/JavaParser**", "**/ChangeMethodTargetToStatic**", "**/J.java")
}

Describe the situation after applying the recipe

tasks.withType<Javadoc>().configureEach {
    // generated ANTLR sources violate doclint
    (options as StandardJavadocDocletOptions).addStringOption("Xdoclint:none", "-quiet")

    // Items besides JavaParser due to lombok error which looks similar to this:
    //     openrewrite/rewrite/rewrite-java/src/main/java/org/openrewrite/java/OrderImports.java:42: error: cannot find symbol
    // @AllArgsConstructor(onConstructor_=@JsonCreator)
    //                     ^
    //   symbol:   method onConstructor_()
    //   location: @interface AllArgsConstructor
    // 1 error
    exclude("**/JavaParser**", "**/ChangeMethodTargetToStatic**", "**/J.java")
}

Any additional context

See https://youtrack.jetbrains.com/issue/IDEA-297893/Inspection-for-tasks.all-tasks.withType...-...-prefer-lazy-task-configuration-configuration-avoidance
See https://github.com/openrewrite/rewrite/blob/efd3214f6f3317a8a59d64821f30211ac6d9993e/rewrite-java/build.gradle.kts#L61-L73

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with rewrite-java/build.gradle.kts#L61-L73 and the linked Gradle task-configuration-avoidance guide. Search the repository for Kotlin and Groovy tasks.withType usages, check the Gradle 4.9 precondition, and verify that matching blocks use configureEach without changing their contents.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, kotlin
Domain
build-system
Issue type
Refactor
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.