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) };