diff --git a/plugin/src/main/java/org/openrewrite/gradle/RewritePlugin.java b/plugin/src/main/java/org/openrewrite/gradle/RewritePlugin.java index 1ccd779bb..5ea34f325 100644 --- a/plugin/src/main/java/org/openrewrite/gradle/RewritePlugin.java +++ b/plugin/src/main/java/org/openrewrite/gradle/RewritePlugin.java @@ -31,6 +31,7 @@ import org.gradle.api.plugins.quality.CheckstylePlugin; import org.gradle.api.provider.Provider; import org.gradle.api.tasks.SourceSetContainer; +import org.gradle.api.tasks.TaskProvider; import java.io.File; import java.util.Comparator; @@ -70,20 +71,23 @@ public void apply(Project project) { Provider> resolvedDependenciesProvider = project.provider(() -> getResolvedDependencies(project, extension, rewriteConf)); - RewriteRunTask rewriteRun = project.getTasks().create("rewriteRun", RewriteRunTask.class) - .setExtension(extension) - .setResolvedDependencies(resolvedDependenciesProvider); - rewriteRun.dependsOn(rewriteConf); + TaskProvider rewriteRun = project.getTasks().register("rewriteRun", RewriteRunTask.class, task -> { + task.setExtension(extension); + task.setResolvedDependencies(resolvedDependenciesProvider); + task.dependsOn(rewriteConf); + }); - RewriteDryRunTask rewriteDryRun = project.getTasks().create("rewriteDryRun", RewriteDryRunTask.class) - .setExtension(extension) - .setResolvedDependencies(resolvedDependenciesProvider); - rewriteDryRun.dependsOn(rewriteConf); + TaskProvider rewriteDryRun = project.getTasks().register("rewriteDryRun", RewriteDryRunTask.class, task -> { + task.setExtension(extension); + task.setResolvedDependencies(resolvedDependenciesProvider); + task.dependsOn(rewriteConf); + }); - RewriteDiscoverTask rewriteDiscover = project.getTasks().create("rewriteDiscover", RewriteDiscoverTask.class) - .setExtension(extension) - .setResolvedDependencies(resolvedDependenciesProvider); - rewriteDiscover.dependsOn(rewriteConf); + TaskProvider rewriteDiscover = project.getTasks().register("rewriteDiscover", RewriteDiscoverTask.class, task -> { + task.setExtension(extension); + task.setResolvedDependencies(resolvedDependenciesProvider); + task.dependsOn(rewriteConf); + }); if (isRootProject) { project.allprojects(subproject -> configureProject(subproject, extension, rewriteDryRun, rewriteRun)); @@ -92,7 +96,7 @@ public void apply(Project project) { } } - private static void configureProject(Project project, RewriteExtension extension, RewriteDryRunTask rewriteDryRun, RewriteRunTask rewriteRun) { + private static void configureProject(Project project, RewriteExtension extension, TaskProvider rewriteDryRun, TaskProvider rewriteRun) { // DomainObjectCollection.all() accepts a function to be applied to both existing and subsequently added members of the collection // Do not replace all() with any form of collection iteration which does not share this important property project.getPlugins().all(plugin -> { @@ -119,9 +123,9 @@ private static void configureProject(Project project, RewriteExtension extension } sourceSets.all(sourceSet -> { // This is intended to ensure that any Groovy/Kotlin/etc. and dependent project sources are available - Task compileTask = project.getTasks().getByPath(sourceSet.getCompileJavaTaskName()); - rewriteRun.dependsOn(compileTask); - rewriteDryRun.dependsOn(compileTask); + TaskProvider compileTask = project.getTasks().named(sourceSet.getCompileJavaTaskName()); + rewriteRun.configure(task -> task.dependsOn(compileTask)); + rewriteDryRun.configure(task -> task.dependsOn(compileTask)); }); // Detect SourceSets which overlap other sourceSets and disable the compilation task of the overlapping @@ -139,8 +143,8 @@ private static void configureProject(Project project, RewriteExtension extension })).forEach(sourceSet -> { for (File file : sourceSet.getAllJava().getSourceDirectories().getFiles()) { if (!sourceDirs.add(file.getAbsolutePath())) { - Task compileTask = project.getTasks().getByPath(sourceSet.getCompileJavaTaskName()); - compileTask.setEnabled(false); + TaskProvider compileTask = project.getTasks().named(sourceSet.getCompileJavaTaskName()); + compileTask.configure(task -> task.setEnabled(false)); } } }));