Skip to content

Commit

Permalink
Init fields in ShadowExtension's constructor
Browse files Browse the repository at this point in the history
Follow up 6bbc401.
  • Loading branch information
Goooler committed Mar 9, 2023
1 parent 95d3da4 commit a86645c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/docs/publishing/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ShadowBasePlugin implements Plugin<Project> {
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 ->
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Provider<RegularFile>> archiveFile
private final Provider<List<Dep>> 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
}
Expand Down Expand Up @@ -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'
}
}
Expand Down

0 comments on commit a86645c

Please sign in to comment.