Skip to content

Commit

Permalink
Update RewritePlugin to load tasks lazily (#326)
Browse files Browse the repository at this point in the history
Motivation
----------

The current structure of RewritePlugin eagerly loads the RewriteRun, RewriteDryRun, and RewriteDiscover tasks. This is a problem in Gradle builds with a large number of modules, as it has to create hundreds or even thousands of tasks during configuration. This will happen even if the tasks are not used in that run. This commit updates the plugin by lazily loading these tasks to avoid this issue.

Modifications
-------------

The RewritePlugin was changed to call the register() method instead of the create() method for the three tasks. This method takes a configuration lambda where the existing configuration can be done. Next, the tasks' usages were replaced with the equivalent TaskProvider reference. Finally, fetching the appropriate JavaCompile tasks was updated to use a lazy load method (named()), so those tasks are not eagerly created. The plugin's Gradle version was unchanged and should still support Gradle 4.10.

Result
------

The result is a RewritePlugin that no longer eagerly loads its tasks. It is now safe to use in very large builds.
  • Loading branch information
mattbertolini authored Aug 21, 2024
1 parent f908308 commit 048ab77
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions plugin/src/main/java/org/openrewrite/gradle/RewritePlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -70,20 +71,23 @@ public void apply(Project project) {

Provider<Set<File>> resolvedDependenciesProvider = project.provider(() -> getResolvedDependencies(project, extension, rewriteConf));

RewriteRunTask rewriteRun = project.getTasks().create("rewriteRun", RewriteRunTask.class)
.setExtension(extension)
.setResolvedDependencies(resolvedDependenciesProvider);
rewriteRun.dependsOn(rewriteConf);
TaskProvider<RewriteRunTask> 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<RewriteDryRunTask> 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<RewriteDiscoverTask> 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));
Expand All @@ -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<RewriteDryRunTask> rewriteDryRun, TaskProvider<RewriteRunTask> 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 -> {
Expand All @@ -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<Task> 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
Expand All @@ -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<Task> compileTask = project.getTasks().named(sourceSet.getCompileJavaTaskName());
compileTask.configure(task -> task.setEnabled(false));
}
}
}));
Expand Down

0 comments on commit 048ab77

Please sign in to comment.