Skip to content

Commit

Permalink
Use task configuration avoidance in gradle initialization
Browse files Browse the repository at this point in the history
Gradle task creation can be heavyweight when hundreds or thousands of
instances of tasks exist. This is especially true in large multi project
builds. Forbidden apis currently always creates a task per source set,
as well as the alias `forbiddenApis` task. Gradle provides a way to
lazily construct the task, and only configure when the task is actually
needed for a given task graph.

This commit switches the construction of the forbidden apis tasks to use
task registration, when it is available (based on gradle version).
  • Loading branch information
rjernst committed Apr 8, 2020
1 parent 0ba6767 commit 9d5d198
Showing 1 changed file with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

import java.lang.reflect.Modifier;
import org.gradle.api.plugins.JavaBasePlugin;
import org.gradle.util.GradleVersion;

final boolean TASK_AVOIDANCE_AVAILABLE = GradleVersion.current() >= GradleVersion.version("4.9");

project.plugins.apply(JavaBasePlugin.class);

Expand All @@ -33,15 +36,19 @@ def extensionProps = CheckForbiddenApisExtension.class.declaredFields.findAll{ f
}*.name;

// Create a convenience task for all checks (this does not conflict with extension, as it has higher priority in DSL):
def forbiddenTask = project.tasks.create(FORBIDDEN_APIS_TASK_NAME) {
def forbiddenTask = TASK_AVOIDANCE_AVAILABLE ? project.tasks.register(FORBIDDEN_APIS_TASK_NAME) : project.tasks.create(FORBIDDEN_APIS_TASK_NAME)
forbiddenTask.configure {
description = "Runs forbidden-apis checks.";
group = JavaBasePlugin.VERIFICATION_GROUP;
}

// Define our tasks (one for each SourceSet):
project.sourceSets.all{ sourceSet ->
def getSourceSetClassesDirs = { sourceSet.output.hasProperty('classesDirs') ? sourceSet.output.classesDirs : project.files(sourceSet.output.classesDir) }
project.tasks.create(sourceSet.getTaskName(FORBIDDEN_APIS_TASK_NAME, null), CheckForbiddenApis.class) { task ->
String sourceSetTaskName = sourceSet.getTaskName(FORBIDDEN_APIS_TASK_NAME, null);
def sourceSetTask = TASK_AVOIDANCE_AVAILABLE ? project.tasks.register(sourceSetTaskName, CheckForbiddenApis.class) :
project.tasks.create(sourceSetTaskName, CheckForbiddenApis.class);
sourceSetTask.configure { task ->
description = "Runs forbidden-apis checks on '${sourceSet.name}' classes.";
conventionMapping.with{
extensionProps.each{ key ->
Expand All @@ -61,9 +68,12 @@ project.sourceSets.all{ sourceSet ->
}
}
outputs.upToDateWhen { true }
forbiddenTask.dependsOn(task);
}
forbiddenTask.configure {
dependsOn(sourceSetTask)
}
}

// Add our task as dependency to chain
project.tasks[JavaBasePlugin.CHECK_TASK_NAME].dependsOn(forbiddenTask);
def checkTask = TASK_AVOIDANCE_AVAILABLE ? project.tasks.named(JavaBasePlugin.CHECK_TASK_NAME) : project.tasks.getByName(JavaBasePlugin.CHECK_TASK_NAME);
checkTask.configure { it.dependsOn(forbiddenTask) };

0 comments on commit 9d5d198

Please sign in to comment.