Skip to content

Commit

Permalink
Add automatic handling of Gradle API when using java-gradle-plugin
Browse files Browse the repository at this point in the history
The Java Gradle Plugin Development plugin, `java-gradle-plugin`,
automatically adds the full Gradle API to the `compile` 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.
  • Loading branch information
maguro committed Feb 11, 2019
1 parent c47b255 commit 996957c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 20 deletions.
7 changes: 4 additions & 3 deletions src/docs/configuration/relocation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ tasks.shadowJar.dependsOn tasks.relocateShadowJar

> 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 (such as `java-gradle-plugin` will automatically add dependencies to your class path
(e.g. `java-gradle-plugin` automatically adds the full Gradle API to your `compile` configuratinon. You may need to
remove these dependencies if you do not intend to shadow them into your library.
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).
12 changes: 11 additions & 1 deletion src/docs/plugins/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,14 @@ tasks.shadowJar.dependsOn tasks.relocateShadowJar
Note that the `localGroovy()` and `gradleApi()` dependencies are added to the `shadow` configuration instead of the
normal `compile` configuration. These 2 dependencies are provided by Gradle to compile your project but are ultimately
provided by the Gradle runtime when executing the plugin. Thus, it is **not** advisable to bundle these dependencies
with your plugin.
with your plugin.

## 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`
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()
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,41 @@ import org.gradle.api.Project
import org.gradle.api.plugins.ApplicationPlugin
import org.gradle.api.plugins.JavaPlugin

import static java.util.Objects.nonNull

class ShadowPlugin implements Plugin<Project> {

@Override
void apply(Project project) {
project.plugins.apply(ShadowBasePlugin)
project.plugins.withType(JavaPlugin) {
project.plugins.apply(ShadowJavaPlugin)
}
project.plugins.withType(ApplicationPlugin) {
project.plugins.apply(ShadowApplicationPlugin)
}
project.with {
plugins.apply(ShadowBasePlugin)
plugins.withType(JavaPlugin) {
plugins.apply(ShadowJavaPlugin)
}
plugins.withType(ApplicationPlugin) {
plugins.apply(ShadowApplicationPlugin)
}

def rootProject = project.rootProject
rootProject.plugins.withId('com.gradle.build-scan') {
rootProject.buildScan.buildFinished {
def shadowTasks = project.tasks.withType(ShadowJar)
shadowTasks.each { task ->
if (task.didWork) {
task.stats.buildScanData.each { k, v ->
rootProject.buildScan.value "shadow.${task.path}.${k}", v.toString()
rootProject.plugins.withId('com.gradle.build-scan') {
rootProject.buildScan.buildFinished {
def shadowTasks = tasks.withType(ShadowJar)
shadowTasks.each { task ->
if (task.didWork) {
task.stats.buildScanData.each { k, v ->
rootProject.buildScan.value "shadow.${task.path}.${k}", v.toString()
}
rootProject.buildScan.value "shadow.${task.path}.configurations", task.configurations*.name.join(", ")
}
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.compile.dependencies.remove dependencies.gradleApi()
}
}
}
}
}

0 comments on commit 996957c

Please sign in to comment.