From 0c097fd06f01b13da1fc4761b7f1d35d127698af Mon Sep 17 00:00:00 2001 From: Uwe Schindler Date: Mon, 3 Oct 2022 12:48:08 +0200 Subject: [PATCH] Fix Signatures URLs added to any task are actually applied to all tasks. This closes #203 --- .../gradle/CheckForbiddenApis.java | 5 +++++ .../gradle/ForbiddenApisPlugin.java | 2 +- .../forbiddenapis/gradle/plugin-init.groovy | 19 ++++++++++++++++--- src/test/gradle/build.gradle | 10 ++++++++++ 4 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/main/java/de/thetaphi/forbiddenapis/gradle/CheckForbiddenApis.java b/src/main/java/de/thetaphi/forbiddenapis/gradle/CheckForbiddenApis.java index 8f25927e..5b3de23d 100644 --- a/src/main/java/de/thetaphi/forbiddenapis/gradle/CheckForbiddenApis.java +++ b/src/main/java/de/thetaphi/forbiddenapis/gradle/CheckForbiddenApis.java @@ -121,6 +121,11 @@ public class CheckForbiddenApis extends DefaultTask implements PatternFilterable private FileCollection classpath; private String targetCompatibility; + /** Gives access to internal data of plugin to plugin-init.groovy */ + CheckForbiddenApisExtension internalTaskData() { + return data; + } + /** * Directories with the class files to check. * Defaults to current sourseSet's output directory (Gradle 3) or output directories (Gradle 4.0+). diff --git a/src/main/java/de/thetaphi/forbiddenapis/gradle/ForbiddenApisPlugin.java b/src/main/java/de/thetaphi/forbiddenapis/gradle/ForbiddenApisPlugin.java index f749a763..f2bee437 100644 --- a/src/main/java/de/thetaphi/forbiddenapis/gradle/ForbiddenApisPlugin.java +++ b/src/main/java/de/thetaphi/forbiddenapis/gradle/ForbiddenApisPlugin.java @@ -102,7 +102,7 @@ public Class run() { loader.parseClass(csrc, false).asSubclass(DelegatingScript.class); return clazz; } catch (Exception e) { - throw new RuntimeException("Cannot compile Groovy script: " + PLUGIN_INIT_SCRIPT); + throw new RuntimeException("Cannot compile Groovy script: " + PLUGIN_INIT_SCRIPT, e); } } }); 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 37079b6b..5fcdc0d5 100644 --- a/src/main/resources/de/thetaphi/forbiddenapis/gradle/plugin-init.groovy +++ b/src/main/resources/de/thetaphi/forbiddenapis/gradle/plugin-init.groovy @@ -17,6 +17,7 @@ /** Initializes the plugin and binds it to project lifecycle. */ import org.gradle.api.plugins.JavaBasePlugin; +import org.gradle.api.file.ConfigurableFileCollection; project.plugins.apply(JavaBasePlugin.class); @@ -43,12 +44,24 @@ project.sourceSets.all{ sourceSet -> description = "Runs forbidden-apis checks on '${sourceSet.name}' classes."; dependsOn(sourceSet.output); outputs.upToDateWhen { true } + def taskData = internalTaskData() conventionMapping.with{ FORBIDDEN_APIS_EXTENSION_PROPS.each{ key -> - map(key, { extension[key] }); + map(key, { + def item = taskData[key] + if (item instanceof ConfigurableFileCollection) { + return item.from(extension[key]) + } else if (item instanceof Collection) { + item.addAll(extension[key]) + return item + } + return extension[key] + }) } - classesDirs = { sourceSet.output.hasProperty('classesDirs') ? sourceSet.output.classesDirs : project.files(sourceSet.output.classesDir) } - classpath = { sourceSet.compileClasspath } + ConfigurableFileCollection templateClassesDirs = project.files(); + classesDirs = { templateClassesDirs.from(sourceSet.output.hasProperty('classesDirs') ? sourceSet.output.classesDirs : sourceSet.output.classesDir) } + ConfigurableFileCollection templateClasspath = project.files(); + classpath = { templateClasspath.from(sourceSet.compileClasspath) } targetCompatibility = targetCompatibilityGetter } } diff --git a/src/test/gradle/build.gradle b/src/test/gradle/build.gradle index bef48b99..75e09bcc 100644 --- a/src/test/gradle/build.gradle +++ b/src/test/gradle/build.gradle @@ -38,6 +38,12 @@ sourceSets { srcDirs = [new File(forbiddenRootDir, 'src/main/java')] } } + main2 { + compileClasspath = files(forbiddenClasspath.tokenize(File.pathSeparator)) + java { + srcDirs = [new File(forbiddenRootDir, 'src/main/java')] + } + } test { compileClasspath = files(forbiddenTestClasspath.tokenize(File.pathSeparator)) java { @@ -53,6 +59,10 @@ forbiddenApis { } forbiddenApisMain { + bundledSignatures.add('jdk-internal') +} + +forbiddenApisMain2 { bundledSignatures += 'jdk-system-out' }