From 9d5d198f6bb014b02e613ced5b2881d9598a8277 Mon Sep 17 00:00:00 2001 From: Ryan Ernst Date: Wed, 8 Apr 2020 15:34:53 -0700 Subject: [PATCH] Use task configuration avoidance in gradle initialization 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). --- .../forbiddenapis/gradle/plugin-init.groovy | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/main/resources/de/thetaphi/forbiddenapis/gradle/plugin-init.groovy b/src/main/resources/de/thetaphi/forbiddenapis/gradle/plugin-init.groovy index e988dd63..51db803e 100644 --- a/src/main/resources/de/thetaphi/forbiddenapis/gradle/plugin-init.groovy +++ b/src/main/resources/de/thetaphi/forbiddenapis/gradle/plugin-init.groovy @@ -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); @@ -33,7 +36,8 @@ 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; } @@ -41,7 +45,10 @@ def forbiddenTask = project.tasks.create(FORBIDDEN_APIS_TASK_NAME) { // 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 -> @@ -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) };