Skip to content

Commit 48edcc3

Browse files
committed
Avoid eager task creation in the Gradle plugin
Closes gh-319
1 parent 044a405 commit 48edcc3

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed

spring-javaformat-gradle/spring-javaformat-gradle-plugin/src/main/java/io/spring/javaformat/gradle/SpringJavaFormatPlugin.java

+26-20
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2021 the original author or authors.
2+
* Copyright 2017-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,6 +24,8 @@
2424
import org.gradle.api.plugins.JavaBasePlugin;
2525
import org.gradle.api.plugins.JavaPluginConvention;
2626
import org.gradle.api.tasks.SourceSet;
27+
import org.gradle.api.tasks.TaskContainer;
28+
import org.gradle.api.tasks.TaskProvider;
2729

2830
import io.spring.javaformat.gradle.tasks.CheckFormat;
2931
import io.spring.javaformat.gradle.tasks.Format;
@@ -33,6 +35,7 @@
3335
* Spring Format Gradle Plugin.
3436
*
3537
* @author Phillip Webb
38+
* @author Andy Wilkinson
3639
*/
3740
public class SpringJavaFormatPlugin implements Plugin<Project> {
3841

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

4750
private void addSourceTasks() {
4851
this.project.getPlugins().withType(JavaBasePlugin.class, (plugin) -> {
49-
Task formatAll = this.project.task(Format.NAME);
50-
formatAll.setDescription(Format.DESCRIPTION);
51-
Task checkAll = this.project.task(CheckFormat.NAME);
52-
checkAll.setDescription(CheckFormat.DESCRIPTION);
53-
this.project.getTasks().getByName(JavaBasePlugin.CHECK_TASK_NAME).dependsOn(checkAll);
52+
TaskContainer tasks = this.project.getTasks();
53+
TaskProvider<Task> formatAllProvider = tasks.register(Format.NAME);
54+
formatAllProvider.configure((formatAll) -> formatAll.setDescription(Format.DESCRIPTION));
55+
TaskProvider<Task> checkAllProvider = tasks.register(CheckFormat.NAME);
56+
checkAllProvider.configure((checkAll) -> checkAll.setDescription(CheckFormat.DESCRIPTION));
57+
tasks.named(JavaBasePlugin.CHECK_TASK_NAME).configure((check) -> check.dependsOn(checkAllProvider));
5458
this.project.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets()
55-
.all((sourceSet) -> addSourceTasks(sourceSet, checkAll, formatAll));
59+
.all((sourceSet) -> addSourceTasks(sourceSet, checkAllProvider, formatAllProvider));
5660
});
5761
}
5862

59-
private void addSourceTasks(SourceSet sourceSet, Task checkAll, Task formatAll) {
60-
CheckFormat checkTask = addFormatterTask(sourceSet, CheckFormat.class, CheckFormat.NAME,
63+
private void addSourceTasks(SourceSet sourceSet, TaskProvider<Task> checkAllProvider, TaskProvider<Task> formatAllProvider) {
64+
TaskProvider<CheckFormat> checkTaskProvider = addFormatterTask(sourceSet, CheckFormat.class, CheckFormat.NAME,
6165
CheckFormat.DESCRIPTION);
62-
checkTask.setReportLocation(
63-
new File(this.project.getBuildDir(), "reports/format/" + sourceSet.getName() + "/check-format.txt"));
64-
checkAll.dependsOn(checkTask);
65-
Format formatSourceSet = addFormatterTask(sourceSet, Format.class, Format.NAME, Format.DESCRIPTION);
66-
formatSourceSet.conventionMapping("encoding", () -> "UTF-8");
67-
formatAll.dependsOn(formatSourceSet);
66+
checkTaskProvider.configure((checkTask) -> checkTask.setReportLocation(
67+
new File(this.project.getBuildDir(), "reports/format/" + sourceSet.getName() + "/check-format.txt")));
68+
checkAllProvider.configure((checkAll) -> checkAll.dependsOn(checkTaskProvider));
69+
TaskProvider<Format> formatTaskProvider = addFormatterTask(sourceSet, Format.class, Format.NAME, Format.DESCRIPTION);
70+
formatTaskProvider.configure((format) -> format.conventionMapping("encoding", () -> "UTF-8"));
71+
formatAllProvider.configure((formatAll) -> formatAll.dependsOn(formatTaskProvider));
6872
}
6973

70-
private <T extends FormatterTask> T addFormatterTask(SourceSet sourceSet, Class<T> taskType, String name,
74+
private <T extends FormatterTask> TaskProvider<T> addFormatterTask(SourceSet sourceSet, Class<T> taskType, String name,
7175
String desc) {
7276
String taskName = sourceSet.getTaskName(name, null);
73-
T task = this.project.getTasks().create(taskName, taskType);
74-
task.setDescription(desc + " for " + sourceSet.getName());
75-
task.setSource(sourceSet.getAllJava());
76-
return task;
77+
TaskProvider<T> provider = this.project.getTasks().register(taskName, taskType);
78+
provider.configure((task) -> {
79+
task.setDescription(desc + " for " + sourceSet.getName());
80+
task.setSource(sourceSet.getAllJava());
81+
});
82+
return provider;
7783
}
7884

7985
}

0 commit comments

Comments
 (0)