Skip to content

Commit 4664778

Browse files
authored
Migrate to gradleup-shadow (#751)
1 parent e2e9970 commit 4664778

File tree

6 files changed

+122
-27
lines changed

6 files changed

+122
-27
lines changed

build.gradle.kts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ allprojects.forEach { p ->
179179
}
180180

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

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

201201
val buildToolIntegrations by
202-
tasks.creating {
202+
tasks.registering {
203203
group = "Verification"
204204
description =
205205
"Checks whether bom works fine with build tools, requires preceding publishToMavenLocal in a separate Gradle invocation"

buildSrc/src/main/kotlin/PublishingHelperPlugin.kt

Lines changed: 113 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,24 @@
1414
* limitations under the License.
1515
*/
1616

17-
import com.github.jengelman.gradle.plugins.shadow.ShadowExtension
17+
import com.github.jengelman.gradle.plugins.shadow.ShadowPlugin
1818
import groovy.util.Node
1919
import groovy.util.NodeList
20+
import javax.inject.Inject
2021
import org.gradle.api.GradleException
2122
import org.gradle.api.Plugin
2223
import org.gradle.api.Project
2324
import org.gradle.api.artifacts.Configuration
25+
import org.gradle.api.artifacts.ConfigurationVariant
26+
import org.gradle.api.artifacts.ProjectDependency
2427
import org.gradle.api.artifacts.component.ModuleComponentSelector
2528
import org.gradle.api.artifacts.result.DependencyResult
29+
import org.gradle.api.attributes.Bundling
30+
import org.gradle.api.attributes.Category
31+
import org.gradle.api.attributes.LibraryElements
32+
import org.gradle.api.attributes.Usage
33+
import org.gradle.api.component.SoftwareComponentFactory
34+
import org.gradle.api.plugins.JavaBasePlugin
2635
import org.gradle.api.publish.PublishingExtension
2736
import org.gradle.api.publish.maven.MavenPublication
2837
import org.gradle.api.publish.tasks.GenerateModuleMetadata
@@ -37,7 +46,9 @@ import org.gradle.plugins.signing.SigningPlugin
3746

3847
/** Applies common configurations to all Nessie projects. */
3948
@Suppress("unused")
40-
class PublishingHelperPlugin : Plugin<Project> {
49+
class PublishingHelperPlugin
50+
@Inject
51+
constructor(private val softwareComponentFactory: SoftwareComponentFactory) : Plugin<Project> {
4152
override fun apply(project: Project): Unit =
4253
project.run {
4354
extensions.create("publishingHelper", PublishingHelperExtension::class.java, this)
@@ -46,24 +57,22 @@ class PublishingHelperPlugin : Plugin<Project> {
4657
configure<PublishingExtension> {
4758
publications {
4859
register<MavenPublication>("maven") {
49-
val shadowExtension = project.extensions.findByType(ShadowExtension::class.java)
50-
if (shadowExtension != null) {
51-
shadowExtension.component(this)
52-
project.afterEvaluate {
53-
// Sonatype requires the javadoc and sources jar to be present, but the
54-
// Shadow extension does not publish those.
55-
artifact(tasks.named("javadocJar"))
56-
artifact(tasks.named("sourcesJar"))
60+
val mavenPublication = this
61+
afterEvaluate {
62+
// This MUST happen in an 'afterEvaluate' to ensure that the Shadow*Plugin has
63+
// been applied.
64+
if (project.plugins.hasPlugin(ShadowPlugin::class.java)) {
65+
configureShadowPublishing(project, mavenPublication, softwareComponentFactory)
66+
} else {
67+
from(components.firstOrNull { c -> c.name == "javaPlatform" || c.name == "java" })
5768
}
58-
} else {
59-
from(components.firstOrNull { c -> c.name == "javaPlatform" || c.name == "java" })
69+
suppressPomMetadataWarningsFor("testApiElements")
70+
suppressPomMetadataWarningsFor("testJavadocElements")
71+
suppressPomMetadataWarningsFor("testRuntimeElements")
72+
suppressPomMetadataWarningsFor("testSourcesElements")
73+
suppressPomMetadataWarningsFor("testFixturesApiElements")
74+
suppressPomMetadataWarningsFor("testFixturesRuntimeElements")
6075
}
61-
suppressPomMetadataWarningsFor("testApiElements")
62-
suppressPomMetadataWarningsFor("testJavadocElements")
63-
suppressPomMetadataWarningsFor("testRuntimeElements")
64-
suppressPomMetadataWarningsFor("testSourcesElements")
65-
suppressPomMetadataWarningsFor("testFixturesApiElements")
66-
suppressPomMetadataWarningsFor("testFixturesRuntimeElements")
6776

6877
groupId = "$group"
6978
version = project.version.toString()
@@ -260,3 +269,89 @@ class PublishingHelperPlugin : Plugin<Project> {
260269
return null
261270
}
262271
}
272+
273+
internal fun configureShadowPublishing(
274+
project: Project,
275+
mavenPublication: MavenPublication,
276+
softwareComponentFactory: SoftwareComponentFactory,
277+
) =
278+
project.run {
279+
fun isPublishable(element: ConfigurationVariant): Boolean {
280+
for (artifact in element.artifacts) {
281+
if (JavaBasePlugin.UNPUBLISHABLE_VARIANT_ARTIFACTS.contains(artifact.type)) {
282+
return false
283+
}
284+
}
285+
return true
286+
}
287+
288+
val shadowJar = project.tasks.named("shadowJar")
289+
290+
val shadowApiElements =
291+
project.configurations.create("shadowApiElements") {
292+
isCanBeConsumed = true
293+
isCanBeResolved = false
294+
attributes {
295+
attribute(Usage.USAGE_ATTRIBUTE, project.objects.named(Usage::class.java, Usage.JAVA_API))
296+
attribute(
297+
Category.CATEGORY_ATTRIBUTE,
298+
project.objects.named(Category::class.java, Category.LIBRARY),
299+
)
300+
attribute(
301+
LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE,
302+
project.objects.named(LibraryElements::class.java, LibraryElements.JAR),
303+
)
304+
attribute(
305+
Bundling.BUNDLING_ATTRIBUTE,
306+
project.objects.named(Bundling::class.java, Bundling.SHADOWED),
307+
)
308+
}
309+
outgoing.artifact(shadowJar)
310+
}
311+
312+
val component = softwareComponentFactory.adhoc("shadow")
313+
component.addVariantsFromConfiguration(shadowApiElements) {
314+
if (isPublishable(configurationVariant)) {
315+
mapToMavenScope("compile")
316+
} else {
317+
skip()
318+
}
319+
}
320+
// component.addVariantsFromConfiguration(configurations.getByName("runtimeElements")) {
321+
component.addVariantsFromConfiguration(
322+
project.configurations.getByName("shadowRuntimeElements")
323+
) {
324+
if (isPublishable(configurationVariant)) {
325+
mapToMavenScope("runtime")
326+
} else {
327+
skip()
328+
}
329+
}
330+
// Sonatype requires the javadoc and sources jar to be present, but the
331+
// Shadow extension does not publish those.
332+
component.addVariantsFromConfiguration(project.configurations.getByName("javadocElements")) {}
333+
component.addVariantsFromConfiguration(project.configurations.getByName("sourcesElements")) {}
334+
mavenPublication.from(component)
335+
336+
// This a replacement to add dependencies to the pom, if necessary. Equivalent to
337+
// 'shadowExtension.component(mavenPublication)', which we cannot use.
338+
339+
mavenPublication.pom {
340+
withXml {
341+
val node = asNode()
342+
val depNode = node.get("dependencies")
343+
val dependenciesNode =
344+
if ((depNode as NodeList).isNotEmpty()) depNode[0] as Node
345+
else node.appendNode("dependencies")
346+
project.configurations.getByName("shadow").allDependencies.forEach {
347+
if (it is ProjectDependency) {
348+
val dependencyNode = dependenciesNode.appendNode("dependency")
349+
dependencyNode.appendNode("groupId", it.group)
350+
dependencyNode.appendNode("artifactId", it.name)
351+
dependencyNode.appendNode("version", it.version)
352+
dependencyNode.appendNode("scope", "runtime")
353+
}
354+
}
355+
}
356+
}
357+
}

conformance/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import com.google.protobuf.gradle.ProtobufPlugin
1919

2020
plugins {
2121
`java-library`
22-
id("com.github.johnrengelman.shadow")
22+
id("com.gradleup.shadow")
2323
id("org.caffinitas.gradle.testsummary")
2424
id("org.caffinitas.gradle.testrerun")
2525
`cel-conventions`

generated-antlr/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ plugins {
2121
antlr
2222
`maven-publish`
2323
signing
24-
id("com.github.johnrengelman.shadow")
24+
id("com.gradleup.shadow")
2525
`cel-conventions`
2626
}
2727

gradle/libs.versions.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ nessieRunPlugin = "0.27.3"
1717
protobuf = "4.33.0"
1818
protobuf3 = "3.25.4"
1919
protobufPlugin = "0.9.5"
20-
shadowPlugin = "8.1.1"
20+
shadowPlugin = "9.2.2"
2121
slf4j = "1.7.36"
2222
spotlessPlugin = "8.0.0"
2323

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

@@ -69,7 +69,7 @@ jandex = { id = "com.github.vlsi.jandex", version.ref = "jandexPlugin" }
6969
jmh = { id = "me.champeau.jmh", version = "0.7.3" }
7070
maven-central-publish = { id = "io.github.zenhelix.maven-central-publish", version = "0.8.0" }
7171
protobuf = { id = "com.google.protobuf", version.ref = "protobufPlugin" }
72-
shadow = { id = "com.github.johnrengelman.shadow", version.ref = "shadowPlugin" }
72+
shadow = { id = "com.gradleup.shadow", version.ref = "shadowPlugin" }
7373
spotless = { id = "com.diffplug.spotless", version.ref = "spotlessPlugin" }
7474
testrerun = { id = "org.caffinitas.gradle.testrerun", version = "0.1" }
7575
testsummary = { id = "org.caffinitas.gradle.testsummary", version = "0.1.1" }

standalone/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ plugins {
2020
`java-library`
2121
`maven-publish`
2222
signing
23-
id("com.github.johnrengelman.shadow")
23+
id("com.gradleup.shadow")
2424
`cel-conventions`
2525
}
2626

0 commit comments

Comments
 (0)