From 960617fc1689e60763e650492e3bf1df88d5a0fc Mon Sep 17 00:00:00 2001 From: Goooler Date: Thu, 9 Mar 2023 10:09:21 +0800 Subject: [PATCH] Init fields in ShadowExtension's constructor Follow up 6bbc4010a0cd6e50a50a40f1c819fa6f3e8735d3. --- src/docs/publishing/README.md | 2 +- .../plugins/shadow/ShadowBasePlugin.groovy | 2 +- .../plugins/shadow/ShadowExtension.groovy | 25 +++++++++++++------ .../plugins/shadow/PublishingSpec.groovy | 4 +-- 4 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/docs/publishing/README.md b/src/docs/publishing/README.md index 9b8068307..e2aadc75d 100644 --- a/src/docs/publishing/README.md +++ b/src/docs/publishing/README.md @@ -16,7 +16,7 @@ apply plugin: 'com.github.johnrengelman.shadow' publishing { publications { shadow(MavenPublication) { publication -> - project.shadow.component(project, publication) + project.shadow.component(publication) } } repositories { diff --git a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowBasePlugin.groovy b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowBasePlugin.groovy index c8e3ea1d7..1f7a3a7d4 100644 --- a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowBasePlugin.groovy +++ b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowBasePlugin.groovy @@ -16,7 +16,7 @@ class ShadowBasePlugin implements Plugin { if (GradleVersion.current() < GradleVersion.version("8.0")) { throw new GradleException("This version of Shadow supports Gradle 8.0+ only. Please upgrade.") } - project.extensions.create(EXTENSION_NAME, ShadowExtension) + project.extensions.create(EXTENSION_NAME, ShadowExtension, project) createShadowConfiguration(project) project.tasks.register(KnowsTask.NAME, KnowsTask) { knows -> diff --git a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowExtension.groovy b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowExtension.groovy index abfa6cbed..865b3a3e1 100644 --- a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowExtension.groovy +++ b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowExtension.groovy @@ -1,27 +1,38 @@ package com.github.jengelman.gradle.plugins.shadow +import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar import org.gradle.api.Project import org.gradle.api.artifacts.ProjectDependency import org.gradle.api.artifacts.SelfResolvingDependency +import org.gradle.api.file.RegularFile +import org.gradle.api.provider.Provider import org.gradle.api.publish.maven.MavenPom import org.gradle.api.publish.maven.MavenPublication class ShadowExtension { + private final Provider> archiveFile + private final Provider> allDependencies - void component(Project project, MavenPublication publication) { - publication.artifact(project.tasks.named("shadowJar")) - - final def allDependencies = project.provider { - project.configurations.shadow.allDependencies.collect { - if ((it instanceof ProjectDependency) || ! (it instanceof SelfResolvingDependency)) { + ShadowExtension(Project project) { + archiveFile = project.provider { project.tasks.withType(ShadowJar).getByName("shadowJar").archiveFile } + allDependencies = project.provider { + project.configurations.getByName("shadow").allDependencies.collect { + if ((it instanceof ProjectDependency) || !(it instanceof SelfResolvingDependency)) { new Dep(it.group, it.name, it.version) } } } + } + + void component(MavenPublication publication) { + publication.artifact(archiveFile.get()) + + // Don't inline this variable, it seems Groovy closure capturing is confused by the field instead of a local variable. + final def allDeps = allDependencies publication.pom { MavenPom pom -> pom.withXml { xml -> def dependenciesNode = xml.asNode().get('dependencies') ?: xml.asNode().appendNode('dependencies') - allDependencies.get().each { + allDeps.get().each { def dependencyNode = dependenciesNode.appendNode('dependency') dependencyNode.appendNode('groupId', it.group) dependencyNode.appendNode('artifactId', it.name) diff --git a/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/PublishingSpec.groovy b/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/PublishingSpec.groovy index 2f0a68153..7ea155900 100644 --- a/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/PublishingSpec.groovy +++ b/src/test/groovy/com/github/jengelman/gradle/plugins/shadow/PublishingSpec.groovy @@ -42,7 +42,7 @@ class PublishingSpec extends PluginSpecification { publishing { publications { shadow(MavenPublication) { publication -> - project.shadow.component(project, publication) + project.shadow.component(publication) artifactId = 'maven-all' } } @@ -144,7 +144,7 @@ class PublishingSpec extends PluginSpecification { publishing { publications { shadow(MavenPublication) { publication -> - project.shadow.component(project, publication) + project.shadow.component(publication) artifactId = 'maven-all' } }