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 9aa65c3f5..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,33 +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 -import org.gradle.util.GradleVersion class ShadowExtension { + private final Provider> archiveFile + private final Provider> allDependencies - void component(Project project, MavenPublication publication) { - - if (GradleVersion.current() >= GradleVersion.version("6.6")) { - publication.artifact(project.tasks.named("shadowJar")) - } else { - publication.artifact(project.tasks.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' } }