Skip to content

Commit

Permalink
Avoid eager task creation in the Gradle plugin
Browse files Browse the repository at this point in the history
Closes gh-319
  • Loading branch information
wilkinsona committed Feb 19, 2022
1 parent 044a405 commit 48edcc3
Showing 1 changed file with 26 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2021 the original author or authors.
* Copyright 2017-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,6 +24,8 @@
import org.gradle.api.plugins.JavaBasePlugin;
import org.gradle.api.plugins.JavaPluginConvention;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.TaskContainer;
import org.gradle.api.tasks.TaskProvider;

import io.spring.javaformat.gradle.tasks.CheckFormat;
import io.spring.javaformat.gradle.tasks.Format;
Expand All @@ -33,6 +35,7 @@
* Spring Format Gradle Plugin.
*
* @author Phillip Webb
* @author Andy Wilkinson
*/
public class SpringJavaFormatPlugin implements Plugin<Project> {

Expand All @@ -46,34 +49,37 @@ public void apply(Project project) {

private void addSourceTasks() {
this.project.getPlugins().withType(JavaBasePlugin.class, (plugin) -> {
Task formatAll = this.project.task(Format.NAME);
formatAll.setDescription(Format.DESCRIPTION);
Task checkAll = this.project.task(CheckFormat.NAME);
checkAll.setDescription(CheckFormat.DESCRIPTION);
this.project.getTasks().getByName(JavaBasePlugin.CHECK_TASK_NAME).dependsOn(checkAll);
TaskContainer tasks = this.project.getTasks();
TaskProvider<Task> formatAllProvider = tasks.register(Format.NAME);
formatAllProvider.configure((formatAll) -> formatAll.setDescription(Format.DESCRIPTION));
TaskProvider<Task> checkAllProvider = tasks.register(CheckFormat.NAME);
checkAllProvider.configure((checkAll) -> checkAll.setDescription(CheckFormat.DESCRIPTION));
tasks.named(JavaBasePlugin.CHECK_TASK_NAME).configure((check) -> check.dependsOn(checkAllProvider));
this.project.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets()
.all((sourceSet) -> addSourceTasks(sourceSet, checkAll, formatAll));
.all((sourceSet) -> addSourceTasks(sourceSet, checkAllProvider, formatAllProvider));
});
}

private void addSourceTasks(SourceSet sourceSet, Task checkAll, Task formatAll) {
CheckFormat checkTask = addFormatterTask(sourceSet, CheckFormat.class, CheckFormat.NAME,
private void addSourceTasks(SourceSet sourceSet, TaskProvider<Task> checkAllProvider, TaskProvider<Task> formatAllProvider) {
TaskProvider<CheckFormat> checkTaskProvider = addFormatterTask(sourceSet, CheckFormat.class, CheckFormat.NAME,
CheckFormat.DESCRIPTION);
checkTask.setReportLocation(
new File(this.project.getBuildDir(), "reports/format/" + sourceSet.getName() + "/check-format.txt"));
checkAll.dependsOn(checkTask);
Format formatSourceSet = addFormatterTask(sourceSet, Format.class, Format.NAME, Format.DESCRIPTION);
formatSourceSet.conventionMapping("encoding", () -> "UTF-8");
formatAll.dependsOn(formatSourceSet);
checkTaskProvider.configure((checkTask) -> checkTask.setReportLocation(
new File(this.project.getBuildDir(), "reports/format/" + sourceSet.getName() + "/check-format.txt")));
checkAllProvider.configure((checkAll) -> checkAll.dependsOn(checkTaskProvider));
TaskProvider<Format> formatTaskProvider = addFormatterTask(sourceSet, Format.class, Format.NAME, Format.DESCRIPTION);
formatTaskProvider.configure((format) -> format.conventionMapping("encoding", () -> "UTF-8"));
formatAllProvider.configure((formatAll) -> formatAll.dependsOn(formatTaskProvider));
}

private <T extends FormatterTask> T addFormatterTask(SourceSet sourceSet, Class<T> taskType, String name,
private <T extends FormatterTask> TaskProvider<T> addFormatterTask(SourceSet sourceSet, Class<T> taskType, String name,
String desc) {
String taskName = sourceSet.getTaskName(name, null);
T task = this.project.getTasks().create(taskName, taskType);
task.setDescription(desc + " for " + sourceSet.getName());
task.setSource(sourceSet.getAllJava());
return task;
TaskProvider<T> provider = this.project.getTasks().register(taskName, taskType);
provider.configure((task) -> {
task.setDescription(desc + " for " + sourceSet.getName());
task.setSource(sourceSet.getAllJava());
});
return provider;
}

}

0 comments on commit 48edcc3

Please sign in to comment.