Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ allprojects.forEach { p ->
}

val buildToolIntegrationGradle by
tasks.creating(Exec::class) {
tasks.registering(Exec::class) {
group = "Verification"
description =
"Checks whether bom works fine with Gradle, requires preceding publishToMavenLocal in a separate Gradle invocation"
Expand All @@ -189,7 +189,7 @@ val buildToolIntegrationGradle by
}

val buildToolIntegrationMaven by
tasks.creating(Exec::class) {
tasks.registering(Exec::class) {
group = "Verification"
description =
"Checks whether bom works fine with Maven, requires preceding publishToMavenLocal in a separate Gradle invocation"
Expand All @@ -199,7 +199,7 @@ val buildToolIntegrationMaven by
}

val buildToolIntegrations by
tasks.creating {
tasks.registering {
group = "Verification"
description =
"Checks whether bom works fine with build tools, requires preceding publishToMavenLocal in a separate Gradle invocation"
Expand Down
131 changes: 113 additions & 18 deletions buildSrc/src/main/kotlin/PublishingHelperPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,24 @@
* limitations under the License.
*/

import com.github.jengelman.gradle.plugins.shadow.ShadowExtension
import com.github.jengelman.gradle.plugins.shadow.ShadowPlugin
import groovy.util.Node
import groovy.util.NodeList
import javax.inject.Inject
import org.gradle.api.GradleException
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.artifacts.Configuration
import org.gradle.api.artifacts.ConfigurationVariant
import org.gradle.api.artifacts.ProjectDependency
import org.gradle.api.artifacts.component.ModuleComponentSelector
import org.gradle.api.artifacts.result.DependencyResult
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.component.SoftwareComponentFactory
import org.gradle.api.plugins.JavaBasePlugin
import org.gradle.api.publish.PublishingExtension
import org.gradle.api.publish.maven.MavenPublication
import org.gradle.api.publish.tasks.GenerateModuleMetadata
Expand All @@ -37,7 +46,9 @@ import org.gradle.plugins.signing.SigningPlugin

/** Applies common configurations to all Nessie projects. */
@Suppress("unused")
class PublishingHelperPlugin : Plugin<Project> {
class PublishingHelperPlugin
@Inject
constructor(private val softwareComponentFactory: SoftwareComponentFactory) : Plugin<Project> {
override fun apply(project: Project): Unit =
project.run {
extensions.create("publishingHelper", PublishingHelperExtension::class.java, this)
Expand All @@ -46,24 +57,22 @@ class PublishingHelperPlugin : Plugin<Project> {
configure<PublishingExtension> {
publications {
register<MavenPublication>("maven") {
val shadowExtension = project.extensions.findByType(ShadowExtension::class.java)
if (shadowExtension != null) {
shadowExtension.component(this)
project.afterEvaluate {
// Sonatype requires the javadoc and sources jar to be present, but the
// Shadow extension does not publish those.
artifact(tasks.named("javadocJar"))
artifact(tasks.named("sourcesJar"))
val mavenPublication = this
afterEvaluate {
// This MUST happen in an 'afterEvaluate' to ensure that the Shadow*Plugin has
// been applied.
if (project.plugins.hasPlugin(ShadowPlugin::class.java)) {
configureShadowPublishing(project, mavenPublication, softwareComponentFactory)
} else {
from(components.firstOrNull { c -> c.name == "javaPlatform" || c.name == "java" })
}
} else {
from(components.firstOrNull { c -> c.name == "javaPlatform" || c.name == "java" })
suppressPomMetadataWarningsFor("testApiElements")
suppressPomMetadataWarningsFor("testJavadocElements")
suppressPomMetadataWarningsFor("testRuntimeElements")
suppressPomMetadataWarningsFor("testSourcesElements")
suppressPomMetadataWarningsFor("testFixturesApiElements")
suppressPomMetadataWarningsFor("testFixturesRuntimeElements")
}
suppressPomMetadataWarningsFor("testApiElements")
suppressPomMetadataWarningsFor("testJavadocElements")
suppressPomMetadataWarningsFor("testRuntimeElements")
suppressPomMetadataWarningsFor("testSourcesElements")
suppressPomMetadataWarningsFor("testFixturesApiElements")
suppressPomMetadataWarningsFor("testFixturesRuntimeElements")

groupId = "$group"
version = project.version.toString()
Expand Down Expand Up @@ -260,3 +269,89 @@ class PublishingHelperPlugin : Plugin<Project> {
return null
}
}

internal fun configureShadowPublishing(
project: Project,
mavenPublication: MavenPublication,
softwareComponentFactory: SoftwareComponentFactory,
) =
project.run {
fun isPublishable(element: ConfigurationVariant): Boolean {
for (artifact in element.artifacts) {
if (JavaBasePlugin.UNPUBLISHABLE_VARIANT_ARTIFACTS.contains(artifact.type)) {
return false
}
}
return true
}

val shadowJar = project.tasks.named("shadowJar")

val shadowApiElements =
project.configurations.create("shadowApiElements") {
isCanBeConsumed = true
isCanBeResolved = false
attributes {
attribute(Usage.USAGE_ATTRIBUTE, project.objects.named(Usage::class.java, Usage.JAVA_API))
attribute(
Category.CATEGORY_ATTRIBUTE,
project.objects.named(Category::class.java, Category.LIBRARY),
)
attribute(
LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE,
project.objects.named(LibraryElements::class.java, LibraryElements.JAR),
)
attribute(
Bundling.BUNDLING_ATTRIBUTE,
project.objects.named(Bundling::class.java, Bundling.SHADOWED),
)
}
outgoing.artifact(shadowJar)
}

val component = softwareComponentFactory.adhoc("shadow")
component.addVariantsFromConfiguration(shadowApiElements) {
if (isPublishable(configurationVariant)) {
mapToMavenScope("compile")
} else {
skip()
}
}
// component.addVariantsFromConfiguration(configurations.getByName("runtimeElements")) {
component.addVariantsFromConfiguration(
project.configurations.getByName("shadowRuntimeElements")
) {
if (isPublishable(configurationVariant)) {
mapToMavenScope("runtime")
} else {
skip()
}
}
// Sonatype requires the javadoc and sources jar to be present, but the
// Shadow extension does not publish those.
component.addVariantsFromConfiguration(project.configurations.getByName("javadocElements")) {}
component.addVariantsFromConfiguration(project.configurations.getByName("sourcesElements")) {}
mavenPublication.from(component)

// This a replacement to add dependencies to the pom, if necessary. Equivalent to
// 'shadowExtension.component(mavenPublication)', which we cannot use.

mavenPublication.pom {
withXml {
val node = asNode()
val depNode = node.get("dependencies")
val dependenciesNode =
if ((depNode as NodeList).isNotEmpty()) depNode[0] as Node
else node.appendNode("dependencies")
project.configurations.getByName("shadow").allDependencies.forEach {
if (it is ProjectDependency) {
val dependencyNode = dependenciesNode.appendNode("dependency")
dependencyNode.appendNode("groupId", it.group)
dependencyNode.appendNode("artifactId", it.name)
dependencyNode.appendNode("version", it.version)
dependencyNode.appendNode("scope", "runtime")
}
}
}
}
}
2 changes: 1 addition & 1 deletion conformance/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import com.google.protobuf.gradle.ProtobufPlugin

plugins {
`java-library`
id("com.github.johnrengelman.shadow")
id("com.gradleup.shadow")
id("org.caffinitas.gradle.testsummary")
id("org.caffinitas.gradle.testrerun")
`cel-conventions`
Expand Down
2 changes: 1 addition & 1 deletion generated-antlr/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ plugins {
antlr
`maven-publish`
signing
id("com.github.johnrengelman.shadow")
id("com.gradleup.shadow")
`cel-conventions`
}

Expand Down
6 changes: 3 additions & 3 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ nessieRunPlugin = "0.27.3"
protobuf = "4.33.0"
protobuf3 = "3.25.4"
protobufPlugin = "0.9.5"
shadowPlugin = "8.1.1"
shadowPlugin = "9.2.2"
slf4j = "1.7.36"
spotlessPlugin = "8.0.0"

Expand Down Expand Up @@ -59,7 +59,7 @@ junit-jupiter-engine = { module = "org.junit.jupiter:junit-jupiter-engine" }
junit-jupiter-params = { module = "org.junit.jupiter:junit-jupiter-params" }
protobuf-java = { module = "com.google.protobuf:protobuf-java", version.ref = "protobuf" }
protobuf-plugin = { module = "com.google.protobuf:protobuf-gradle-plugin", version.ref = "protobufPlugin" }
shadow-plugin = { module = "com.github.johnrengelman:shadow", version.ref = "shadowPlugin" }
shadow-plugin = { module = "com.gradleup.shadow:shadow-gradle-plugin", version.ref = "shadowPlugin" }
spotless-plugin = { module = "com.diffplug.spotless:spotless-plugin-gradle", version.ref = "spotlessPlugin" }
tomcat-annotations-api = { module = "org.apache.tomcat:annotations-api", version = "6.0.53" }

Expand All @@ -69,7 +69,7 @@ jandex = { id = "com.github.vlsi.jandex", version.ref = "jandexPlugin" }
jmh = { id = "me.champeau.jmh", version = "0.7.3" }
maven-central-publish = { id = "io.github.zenhelix.maven-central-publish", version = "0.8.0" }
protobuf = { id = "com.google.protobuf", version.ref = "protobufPlugin" }
shadow = { id = "com.github.johnrengelman.shadow", version.ref = "shadowPlugin" }
shadow = { id = "com.gradleup.shadow", version.ref = "shadowPlugin" }
spotless = { id = "com.diffplug.spotless", version.ref = "spotlessPlugin" }
testrerun = { id = "org.caffinitas.gradle.testrerun", version = "0.1" }
testsummary = { id = "org.caffinitas.gradle.testsummary", version = "0.1.1" }
2 changes: 1 addition & 1 deletion standalone/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ plugins {
`java-library`
`maven-publish`
signing
id("com.github.johnrengelman.shadow")
id("com.gradleup.shadow")
`cel-conventions`
}

Expand Down