From f0bb9270bf085bbd27f0c5a2522bc2c3c13bd746 Mon Sep 17 00:00:00 2001 From: Goooler Date: Wed, 28 Aug 2024 09:40:40 +0800 Subject: [PATCH 1/8] Revert "Moved graldeApi() exclusion to the task configuration." This reverts commit 2f94a2558023b208ffb840471281703cb2c9b222. --- .../gradle/plugins/shadow/ShadowJavaPlugin.groovy | 3 --- .../jengelman/gradle/plugins/shadow/ShadowPlugin.groovy | 9 +++++++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowJavaPlugin.groovy b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowJavaPlugin.groovy index aec258850..d85ebb5ba 100644 --- a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowJavaPlugin.groovy +++ b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowJavaPlugin.groovy @@ -75,9 +75,6 @@ class ShadowJavaPlugin implements Plugin { shadow.configurations = [project.configurations.findByName('runtimeClasspath') ? project.configurations.runtimeClasspath : project.configurations.runtime] shadow.exclude('META-INF/INDEX.LIST', 'META-INF/*.SF', 'META-INF/*.DSA', 'META-INF/*.RSA', 'module-info.class') - shadow.dependencies { - exclude(dependency(project.dependencies.gradleApi())) - } } project.artifacts.add(ShadowBasePlugin.CONFIGURATION_NAME, project.tasks.named(SHADOW_JAR_TASK_NAME)) } diff --git a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowPlugin.groovy b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowPlugin.groovy index 36ced5f5d..b46108898 100644 --- a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowPlugin.groovy +++ b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowPlugin.groovy @@ -40,6 +40,15 @@ class ShadowPlugin implements Plugin { rootProject.buildScan.value "shadow.${task.path}.configurations", task.configurations*.name.join(", ") } } + + afterEvaluate { + plugins.withId('java-gradle-plugin') { + // needed to prevent inclusion of gradle-api into shadow JAR + configurations.named(JavaPlugin.API_CONFIGURATION_NAME) { + dependencies.remove(project.dependencies.gradleApi()) + } + } + } } } } From 938c00b108121564cb311f37f6ed4b5730166354 Mon Sep 17 00:00:00 2001 From: Goooler Date: Wed, 28 Aug 2024 09:41:22 +0800 Subject: [PATCH 2/8] Revert "re-add the code that removes gradleApi from the api configuration" This reverts commit d530cff6 --- build.gradle | 7 ------- 1 file changed, 7 deletions(-) diff --git a/build.gradle b/build.gradle index a184c9e77..28e75df46 100644 --- a/build.gradle +++ b/build.gradle @@ -44,13 +44,6 @@ tasks.withType(Test).configureEach { ) } -// Remove the gradleApi so it isn't merged into the jar file. -// This is required because 'java-gradle-plugin' adds gradleApi() to the 'api' configuration. -// See https://github.com/gradle/gradle/blob/972c3e5c6ef990dd2190769c1ce31998a9402a79/subprojects/plugin-development/src/main/java/org/gradle/plugin/devel/plugins/JavaGradlePluginPlugin.java#L161 -configurations.named(JavaPlugin.API_CONFIGURATION_NAME) { - dependencies.remove(project.dependencies.gradleApi()) -} - tasks.named('shadowJar', ShadowJar) { from rootProject.file('LICENSE') from rootProject.file('NOTICE') From dfbdbc2f01b6bc55fc710e951c75e2e61668e416 Mon Sep 17 00:00:00 2001 From: Goooler Date: Wed, 28 Aug 2024 09:56:37 +0800 Subject: [PATCH 3/8] Match JavaGradlePluginPlugin --- .../gradle/plugins/shadow/ShadowJavaPlugin.groovy | 9 +++++++++ .../jengelman/gradle/plugins/shadow/ShadowPlugin.groovy | 9 --------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowJavaPlugin.groovy b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowJavaPlugin.groovy index d85ebb5ba..3ebad8571 100644 --- a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowJavaPlugin.groovy +++ b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowJavaPlugin.groovy @@ -7,8 +7,10 @@ import org.gradle.api.attributes.Bundling import org.gradle.api.attributes.Category import org.gradle.api.attributes.LibraryElements import org.gradle.api.attributes.Usage +import org.gradle.api.plugins.JavaPlugin import org.gradle.api.tasks.SourceSetContainer import org.gradle.configuration.project.ProjectConfigurationActionContainer +import org.gradle.plugin.devel.plugins.JavaGradlePluginPlugin import javax.inject.Inject @@ -51,6 +53,13 @@ class ShadowJavaPlugin implements Plugin { mapToOptional() // make it a Maven optional dependency } } + + project.plugins.withType(JavaGradlePluginPlugin).configureEach { + // needed to prevent inclusion of gradle-api into shadow JAR + project.configurations.named(JavaPlugin.API_CONFIGURATION_NAME) { + it.dependencies.remove(project.dependencies.gradleApi()) + } + } } protected static void configureShadowTask(Project project) { diff --git a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowPlugin.groovy b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowPlugin.groovy index b46108898..36ced5f5d 100644 --- a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowPlugin.groovy +++ b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowPlugin.groovy @@ -40,15 +40,6 @@ class ShadowPlugin implements Plugin { rootProject.buildScan.value "shadow.${task.path}.configurations", task.configurations*.name.join(", ") } } - - afterEvaluate { - plugins.withId('java-gradle-plugin') { - // needed to prevent inclusion of gradle-api into shadow JAR - configurations.named(JavaPlugin.API_CONFIGURATION_NAME) { - dependencies.remove(project.dependencies.gradleApi()) - } - } - } } } } From e583783460fc1372b4ffe1d9541e910c363b6409 Mon Sep 17 00:00:00 2001 From: Goooler Date: Wed, 28 Aug 2024 10:04:52 +0800 Subject: [PATCH 4/8] Tweak docs --- src/docs/plugins/README.md | 4 ++-- .../jengelman/gradle/plugins/shadow/ShadowJavaPlugin.groovy | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/docs/plugins/README.md b/src/docs/plugins/README.md index 6ce6397f4..636562c59 100644 --- a/src/docs/plugins/README.md +++ b/src/docs/plugins/README.md @@ -48,13 +48,13 @@ See the [Gradle Plugin Publish docs](https://docs.gradle.org/current/userguide/p ## Special Handling of the Java Gradle Plugin Development Plugin -The Java Gradle Plugin Development plugin, `java-gradle-plugin`, automatically adds the full Gradle API to the `compile` +The Java Gradle Plugin Development plugin, `java-gradle-plugin`, automatically adds the full Gradle API to the `api` configuration; thus overriding a possible assignment of `gradleApi()` to the `shadow` configuration. Since it is never a good idea to include the Gradle API when creating a Gradle plugin, the dependency is removed so that it is not included in the resultant shadow jar. Virtually: // needed to prevent inclusion of gradle-api into shadow JAR - configurations.compile.dependencies.remove dependencies.gradleApi() + configurations.api.dependencies.remove dependencies.gradleApi() ## Automatic package relocation with Shadow prior to v8.1.0 diff --git a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowJavaPlugin.groovy b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowJavaPlugin.groovy index 3ebad8571..23cae36bb 100644 --- a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowJavaPlugin.groovy +++ b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowJavaPlugin.groovy @@ -54,6 +54,9 @@ class ShadowJavaPlugin implements Plugin { } } + // Remove the gradleApi so it isn't merged into the jar file. + // This is required because 'java-gradle-plugin' adds gradleApi() to the 'api' configuration. + // See https://github.com/gradle/gradle/blob/972c3e5c6ef990dd2190769c1ce31998a9402a79/subprojects/plugin-development/src/main/java/org/gradle/plugin/devel/plugins/JavaGradlePluginPlugin.java#L161 project.plugins.withType(JavaGradlePluginPlugin).configureEach { // needed to prevent inclusion of gradle-api into shadow JAR project.configurations.named(JavaPlugin.API_CONFIGURATION_NAME) { From 150b5c4a27cac78c27c3e3d792941057b0d7ee5e Mon Sep 17 00:00:00 2001 From: Goooler Date: Wed, 28 Aug 2024 11:09:22 +0800 Subject: [PATCH 5/8] Add `exclude gradleApi() by default` test --- .../plugins/shadow/ShadowPluginSpec.groovy | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowPluginSpec.groovy b/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowPluginSpec.groovy index 7cec61db8..6099b7dd4 100644 --- a/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowPluginSpec.groovy +++ b/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowPluginSpec.groovy @@ -1217,6 +1217,41 @@ class ShadowPluginSpec extends PluginSpecification { assert result.output.contains('TestApp: Hello World! (foo)') } + @Issue("https://github.com/GradleUp/shadow/pull/459") + def 'exclude gradleApi() by default'() { + given: + buildFile.text = getDefaultBuildScript('java-gradle-plugin') + buildFile << """ + dependencies { + compileOnly gradleApi() + } + """.stripIndent() + + file('src/main/java/my/plugin/MyPlugin.java') << """ + package my.plugin; + import org.gradle.api.Plugin; + import org.gradle.api.Project; + public class MyPlugin implements Plugin { + public void apply(Project project) { + System.out.println("MyPlugin: Hello World!"); + } + } + """.stripIndent() + file('src/main/resources/META-INF/gradle-plugins/my.plugin.properties') << """ + implementation-class=my.plugin.MyPlugin + """.stripIndent() + + when: + run('shadowJar') + + then: + assert output.exists() + + and: + JarFile jar = new JarFile(output) + assert jar.entries().collect().findAll { it.name.endsWith('.class') }.size() == 1 + } + private String escapedPath(File file) { file.path.replaceAll('\\\\', '\\\\\\\\') } From 44c74af62901e833570c32006e813eaef84e054d Mon Sep 17 00:00:00 2001 From: Goooler Date: Wed, 28 Aug 2024 11:47:46 +0800 Subject: [PATCH 6/8] Remove explicit `compileOnly gradleApi()` --- gradle/dependencies.gradle | 1 - .../gradle/plugins/shadow/ShadowJavaPlugin.groovy | 11 +++++++---- .../gradle/plugins/shadow/ShadowPluginSpec.groovy | 5 ----- 3 files changed, 7 insertions(+), 10 deletions(-) diff --git a/gradle/dependencies.gradle b/gradle/dependencies.gradle index 0d6264cd4..62dcc8e6e 100644 --- a/gradle/dependencies.gradle +++ b/gradle/dependencies.gradle @@ -1,6 +1,5 @@ dependencies { compileOnly localGroovy() - compileOnly gradleApi() shadow 'org.codehaus.groovy:groovy-backports-compat23:3.0.8' implementation 'org.jdom:jdom2:2.0.6.1' diff --git a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowJavaPlugin.groovy b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowJavaPlugin.groovy index 23cae36bb..d3926974e 100644 --- a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowJavaPlugin.groovy +++ b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowJavaPlugin.groovy @@ -54,14 +54,17 @@ class ShadowJavaPlugin implements Plugin { } } - // Remove the gradleApi so it isn't merged into the jar file. - // This is required because 'java-gradle-plugin' adds gradleApi() to the 'api' configuration. - // See https://github.com/gradle/gradle/blob/972c3e5c6ef990dd2190769c1ce31998a9402a79/subprojects/plugin-development/src/main/java/org/gradle/plugin/devel/plugins/JavaGradlePluginPlugin.java#L161 project.plugins.withType(JavaGradlePluginPlugin).configureEach { - // needed to prevent inclusion of gradle-api into shadow JAR + // Remove the gradleApi so it isn't merged into the jar file. + // This is required because 'java-gradle-plugin' adds gradleApi() to the 'api' configuration. + // See https://github.com/gradle/gradle/blob/972c3e5c6ef990dd2190769c1ce31998a9402a79/subprojects/plugin-development/src/main/java/org/gradle/plugin/devel/plugins/JavaGradlePluginPlugin.java#L161 project.configurations.named(JavaPlugin.API_CONFIGURATION_NAME) { it.dependencies.remove(project.dependencies.gradleApi()) } + // Compile only gradleApi() to make sure the plugin can compile against Gradle API. + project.configurations.named(JavaPlugin.COMPILE_ONLY_CONFIGURATION_NAME) { + it.dependencies.add(project.dependencies.gradleApi()) + } } } diff --git a/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowPluginSpec.groovy b/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowPluginSpec.groovy index 6099b7dd4..f7035e395 100644 --- a/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowPluginSpec.groovy +++ b/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowPluginSpec.groovy @@ -1221,11 +1221,6 @@ class ShadowPluginSpec extends PluginSpecification { def 'exclude gradleApi() by default'() { given: buildFile.text = getDefaultBuildScript('java-gradle-plugin') - buildFile << """ - dependencies { - compileOnly gradleApi() - } - """.stripIndent() file('src/main/java/my/plugin/MyPlugin.java') << """ package my.plugin; From 04e55b35d4d326d4aee264402348c6fc83a0a1e0 Mon Sep 17 00:00:00 2001 From: Goooler Date: Wed, 28 Aug 2024 11:49:22 +0800 Subject: [PATCH 7/8] Note link --- src/docs/plugins/README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/docs/plugins/README.md b/src/docs/plugins/README.md index 636562c59..e5a4059af 100644 --- a/src/docs/plugins/README.md +++ b/src/docs/plugins/README.md @@ -48,8 +48,9 @@ See the [Gradle Plugin Publish docs](https://docs.gradle.org/current/userguide/p ## Special Handling of the Java Gradle Plugin Development Plugin -The Java Gradle Plugin Development plugin, `java-gradle-plugin`, automatically adds the full Gradle API to the `api` -configuration; thus overriding a possible assignment of `gradleApi()` to the `shadow` configuration. Since it is never +The Java Gradle Plugin Development plugin, `java-gradle-plugin`, [automatically adds the full Gradle API to the `api` +configuration](https://github.com/gradle/gradle/blob/972c3e5c6ef990dd2190769c1ce31998a9402a79/subprojects/plugin-development/src/main/java/org/gradle/plugin/devel/plugins/JavaGradlePluginPlugin.java#L161) +; thus overriding a possible assignment of `gradleApi()` to the `shadow` configuration. Since it is never a good idea to include the Gradle API when creating a Gradle plugin, the dependency is removed so that it is not included in the resultant shadow jar. Virtually: From 99285e41bed0b03271eea53c1fac2657eebf6fdd Mon Sep 17 00:00:00 2001 From: Goooler Date: Wed, 28 Aug 2024 11:52:00 +0800 Subject: [PATCH 8/8] Remove the section about `Special Handling of the Java Gradle Plugin Development Plugin` --- src/docs/configuration/relocation/README.md | 4 +--- src/docs/plugins/README.md | 11 ----------- 2 files changed, 1 insertion(+), 14 deletions(-) diff --git a/src/docs/configuration/relocation/README.md b/src/docs/configuration/relocation/README.md index aa2c36d6e..ee5b90d6d 100644 --- a/src/docs/configuration/relocation/README.md +++ b/src/docs/configuration/relocation/README.md @@ -84,6 +84,4 @@ In versions before 8.1.0 it was necessary to configure a separate `ConfigureShad > Configuring package auto relocation can add significant time to the shadow process as it will process all dependencies in the configurations declared to be shadowed. By default, this is the `runtime` or `runtimeClasspath` configurations. Be mindful that some Gradle plugins will automatically add dependencies to your class path. You may need to remove these -dependencies if you do not intend to shadow them into your library. The `java-gradle-plugin` would normally cause such -problems if it were not for the special handling that Shadow provides as described in -[Special Handling of the Java Gradle Plugin Development Plugin](/plugins/#special-handling-of-the-java-gradle-plugin-gevelopmeny-plugin). +dependencies if you do not intend to shadow them into your library. \ No newline at end of file diff --git a/src/docs/plugins/README.md b/src/docs/plugins/README.md index e5a4059af..1736cb9c9 100644 --- a/src/docs/plugins/README.md +++ b/src/docs/plugins/README.md @@ -46,17 +46,6 @@ Starting with this version, plugin projects that apply both Shadow and the Gradl automatically configured to publish the output of the `shadowJar` tasks as the consumable artifact for the plugin. See the [Gradle Plugin Publish docs](https://docs.gradle.org/current/userguide/publishing_gradle_plugins.html#shadow_dependencies) for details. -## Special Handling of the Java Gradle Plugin Development Plugin - -The Java Gradle Plugin Development plugin, `java-gradle-plugin`, [automatically adds the full Gradle API to the `api` -configuration](https://github.com/gradle/gradle/blob/972c3e5c6ef990dd2190769c1ce31998a9402a79/subprojects/plugin-development/src/main/java/org/gradle/plugin/devel/plugins/JavaGradlePluginPlugin.java#L161) -; thus overriding a possible assignment of `gradleApi()` to the `shadow` configuration. Since it is never -a good idea to include the Gradle API when creating a Gradle plugin, the dependency is removed so that it is not -included in the resultant shadow jar. Virtually: - - // needed to prevent inclusion of gradle-api into shadow JAR - configurations.api.dependencies.remove dependencies.gradleApi() - ## Automatic package relocation with Shadow prior to v8.1.0 Prior to Shadow v8.1.0, Shadow handled this by introducing a new task type `ConfigureShadowRelocation`.