Skip to content

Commit ffa41fb

Browse files
Try to figure out how to shade protobuf (doesn't work yet...)
1 parent 72e572f commit ffa41fb

File tree

2 files changed

+88
-3
lines changed

2 files changed

+88
-3
lines changed

buildSrc/src/main/kotlin/library-publishing-conventions.gradle.kts

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,68 @@ project.afterEvaluate {
77
publishing {
88
publications {
99
create<MavenPublication>("maven") {
10+
afterEvaluate {
11+
val shadowJar = tasks.findByName("shadowJar")
12+
if (shadowJar == null) {
13+
from(components["java"])
14+
}
15+
else {
16+
apply(plugin = "com.gradleup.shadow")
17+
18+
from(components["shadow"])
19+
artifact(tasks["sourcesJar"]!!)
20+
artifact(tasks["javadocJar"]!!)
21+
22+
afterEvaluate {
23+
// Fix for avoiding inclusion of runtime dependencies marked as 'shadow' in MANIFEST Class-Path.
24+
// https://github.com/johnrengelman/shadow/issues/324
25+
pom.withXml {
26+
val rootNode = asElement()
27+
val doc = rootNode.ownerDocument
28+
29+
val dependenciesNode =
30+
if (rootNode.getElementsByTagName("dependencies").length != 0) {
31+
rootNode.getElementsByTagName("dependencies").item(0)
32+
} else {
33+
rootNode.appendChild(
34+
doc.createElement("dependencies")
35+
)
36+
}
37+
38+
project.configurations["shade"].allDependencies.forEach { dep ->
39+
dependenciesNode.appendChild(
40+
doc.createElement("dependency").apply {
41+
appendChild(
42+
doc.createElement("groupId").apply {
43+
textContent = dep.group
44+
}
45+
)
46+
appendChild(
47+
doc.createElement("artifactId").apply {
48+
textContent = dep.name
49+
}
50+
)
51+
appendChild(
52+
doc.createElement("version").apply {
53+
textContent = dep.version
54+
}
55+
)
56+
appendChild(
57+
doc.createElement("scope").apply {
58+
textContent = "runtime"
59+
}
60+
)
61+
}
62+
)
63+
}
64+
}
65+
}
66+
}
67+
}
68+
1069
groupId = "dev.restate"
1170
artifactId = project.name
1271

13-
from(components["java"])
14-
1572
pom {
1673
name = "Restate SDK :: ${project.name}"
1774
description = project.description!!

sdk-core/build.gradle.kts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import org.gradle.kotlin.dsl.allDependencies
12
import org.jetbrains.dokka.gradle.AbstractDokkaTask
3+
import org.jetbrains.kotlin.gradle.utils.extendsFrom
24

35
plugins {
46
`java-library`
@@ -8,19 +10,26 @@ plugins {
810
`library-publishing-conventions`
911
alias(libs.plugins.jsonschema2pojo)
1012
alias(libs.plugins.protobuf)
13+
alias(libs.plugins.shadow)
1114

1215
// https://github.com/gradle/gradle/issues/20084#issuecomment-1060822638
1316
id(libs.plugins.spotless.get().pluginId) apply false
1417
}
1518

1619
description = "Restate SDK Core"
1720

21+
val shade by configurations.creating
22+
val implementation by configurations.getting
23+
implementation.extendsFrom(shade)
24+
val api by configurations.getting
25+
api.extendsFrom(shade)
26+
1827
dependencies {
1928
compileOnly(libs.jspecify)
2029

2130
implementation(project(":sdk-common"))
2231

23-
implementation(libs.protobuf.java)
32+
shade(libs.protobuf.java)
2433
implementation(libs.log4j.api)
2534

2635
// We need this for the manifest
@@ -33,6 +42,7 @@ dependencies {
3342
api(libs.opentelemetry.api)
3443

3544
testCompileOnly(libs.jspecify)
45+
testImplementation(libs.protobuf.java)
3646
testImplementation(libs.mutiny)
3747
testImplementation(libs.junit.jupiter)
3848
testImplementation(libs.assertj)
@@ -75,6 +85,24 @@ tasks {
7585
dependsOn(generateJsonSchema2Pojo, generateProto)
7686
}
7787
withType<AbstractDokkaTask>().configureEach { dependsOn(generateJsonSchema2Pojo, generateProto) }
88+
89+
getByName("jar") {
90+
enabled = false
91+
dependsOn(shadowJar)
92+
}
93+
94+
shadowJar {
95+
configurations = listOf(shade)
96+
enableRelocation = true
97+
archiveClassifier = null
98+
relocate("com.google.protobuf", "dev.restate.shaded.com.google.protobuf")
99+
dependencies {
100+
project.configurations["shadow"].allDependencies.forEach {
101+
exclude(dependency(it))
102+
}
103+
exclude("**/google/protobuf/*.proto")
104+
}
105+
}
78106
}
79107

80108
// spotless configuration for protobuf

0 commit comments

Comments
 (0)