Skip to content

Commit

Permalink
Merge pull request #162 from rjernst/task_avoidance
Browse files Browse the repository at this point in the history
Use task configuration avoidance in gradle initialization
  • Loading branch information
uschindler authored Apr 9, 2020
2 parents 0ba6767 + 9d5d198 commit 261ff53
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 261ff53

Please sign in to comment.