Skip to content

Commit 3ed44e9

Browse files
ivandev0Space Team
authored and
Space Team
committed
[Build] Update protobuf repacking build script
1. Update protobuf version to `4.29.3` 2. Update the script of repacking protobuf-lite. In the current version it is a separate artifact and not the part of common `protobuf-java`. #KT-74258
1 parent 5419b24 commit 3ed44e9

File tree

3 files changed

+42
-114
lines changed

3 files changed

+42
-114
lines changed

dependencies/protobuf/build.gradle.kts

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ How to Publish
66
3. Execute `./gradlew -p dependencies/protobuf publish -PkotlinSpaceUsername=usr -PkotlinSpacePassword=token`
77
*/
88

9-
val protobufVersion by extra("2.6.1")
10-
val publishedVersion by extra("2.6.1-1")
9+
val protobufVersion by extra("4.29.3")
10+
val publishedVersion by extra("4.29.3-1")
1111

1212
allprojects {
1313
group = "org.jetbrains.kotlin"

dependencies/protobuf/protobuf-lite/build.gradle.kts

+34-96
Original file line numberDiff line numberDiff line change
@@ -1,126 +1,64 @@
1-
2-
import java.io.BufferedOutputStream
1+
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
32
import java.io.File
4-
import java.io.FileOutputStream
5-
import java.util.jar.JarEntry
6-
import java.util.jar.JarFile
7-
import java.util.zip.ZipOutputStream
83

94
plugins {
10-
base
5+
`java-base`
116
`maven-publish`
7+
id("com.github.johnrengelman.shadow") version "4.0.3" apply false
8+
}
9+
10+
repositories {
11+
mavenCentral()
1212
}
1313

1414
val relocatedProtobuf by configurations.creating
1515
val relocatedProtobufSources by configurations.creating
1616

1717
val protobufVersion: String by rootProject.extra
18-
val outputJarPath = "$buildDir/libs/protobuf-lite-$protobufVersion.jar"
19-
val sourcesJarName = "protobuf-lite-$protobufVersion-sources.jar"
18+
19+
val renamedSources = "${layout.buildDirectory.get()}/renamedSrc/"
20+
val outputJarsPath = "${layout.buildDirectory.get()}/libs"
2021

2122
dependencies {
22-
relocatedProtobuf(project(":protobuf-relocated"))
23+
relocatedProtobuf("com.google.protobuf:protobuf-javalite:$protobufVersion") { isTransitive = false }
24+
relocatedProtobufSources("com.google.protobuf:protobuf-javalite:$protobufVersion:sources") { isTransitive = false }
2325
}
2426

25-
val prepare by tasks.registering {
26-
inputs.files(relocatedProtobuf) // this also adds a dependency
27-
outputs.file(outputJarPath)
28-
doFirst {
29-
File(outputJarPath).parentFile.mkdirs()
30-
}
31-
doLast {
32-
val INCLUDE_START = "<include>**/"
33-
val INCLUDE_END = ".java</include>"
34-
val POM_PATH = "META-INF/maven/com.google.protobuf/protobuf-java/pom.xml"
35-
36-
fun loadAllFromJar(file: File): Map<String, Pair<JarEntry, ByteArray>> {
37-
val result = hashMapOf<String, Pair<JarEntry, ByteArray>>()
38-
JarFile(file).use { jar ->
39-
for (jarEntry in jar.entries()) {
40-
result[jarEntry.name] = Pair(jarEntry, jar.getInputStream(jarEntry).readBytes())
41-
}
42-
}
43-
return result
44-
}
45-
46-
val mainJar = relocatedProtobuf.resolvedConfiguration.resolvedArtifacts.single {
47-
it.name == "protobuf-relocated" && it.classifier == null
48-
}.file
49-
50-
val allFiles = loadAllFromJar(mainJar)
51-
52-
val keepClasses = arrayListOf<String>()
27+
val prepare = tasks.register<ShadowJar>("prepare") {
28+
destinationDirectory.set(File(outputJarsPath))
29+
archiveVersion.set(protobufVersion)
30+
archiveClassifier.set("")
31+
from(relocatedProtobuf)
5332

54-
val pomBytes = allFiles[POM_PATH]?.second ?: error("pom.xml is not found in protobuf jar at $POM_PATH")
55-
val lines = String(pomBytes).lines()
56-
57-
var liteProfileReached = false
58-
for (lineUntrimmed in lines) {
59-
val line = lineUntrimmed.trim()
60-
61-
if (liteProfileReached && line == "</includes>") {
62-
break
63-
}
64-
else if (line == "<id>lite</id>") {
65-
liteProfileReached = true
66-
continue
67-
}
68-
69-
if (liteProfileReached && line.startsWith(INCLUDE_START) && line.endsWith(INCLUDE_END)) {
70-
keepClasses.add(line.removeSurrounding(INCLUDE_START, INCLUDE_END))
71-
}
72-
}
73-
74-
assert(liteProfileReached && keepClasses.isNotEmpty()) { "Wrong pom.xml or the format has changed, check its contents at $POM_PATH" }
75-
76-
val outputFile = File(outputJarPath).apply { delete() }
77-
ZipOutputStream(BufferedOutputStream(FileOutputStream(outputFile))).use { output ->
78-
for ((name, value) in allFiles) {
79-
val className = name.substringAfter("org/jetbrains/kotlin/protobuf/").substringBeforeLast(".class")
80-
if (keepClasses.any { className == it || className.startsWith(it + "$") }) {
81-
val (entry, bytes) = value
82-
output.putNextEntry(entry)
83-
output.write(bytes)
84-
output.closeEntry()
85-
}
86-
}
87-
}
33+
relocate("com.google.protobuf", "org.jetbrains.kotlin.protobuf" ) {
34+
exclude("META-INF/maven/com.google.protobuf/protobuf-javalite/pom.properties")
8835
}
8936
}
9037

91-
val prepareSources = tasks.register<Copy>("prepareSources") {
92-
dependsOn(":protobuf-relocated:prepareSources")
93-
from(provider {
94-
relocatedProtobuf
95-
.resolvedConfiguration
96-
.resolvedArtifacts
97-
.single { it.name == "protobuf-relocated" && it.classifier == "sources" }.file
98-
})
38+
val relocateSources = task<Copy>("relocateSources") {
39+
from(
40+
provider {
41+
zipTree(relocatedProtobufSources.files.single())
42+
}
43+
)
9944

100-
into("$buildDir/libs/")
101-
rename { sourcesJarName }
102-
}
45+
into(renamedSources)
10346

104-
val mainArtifact = artifacts.add(
105-
"default",
106-
provider {
107-
prepare.get().outputs.files.singleFile
108-
}
109-
) {
110-
builtBy(prepare)
111-
classifier = ""
47+
filter { it.replace("com.google.protobuf", "org.jetbrains.kotlin.protobuf") }
11248
}
11349

114-
val sourcesArtifact = artifacts.add("default", File("$buildDir/libs/$sourcesJarName")) {
115-
builtBy(prepareSources)
116-
classifier = "sources"
50+
val prepareSources = task<Jar>("prepareSources") {
51+
destinationDirectory.set(File(outputJarsPath))
52+
archiveVersion.set(protobufVersion)
53+
archiveClassifier.set("sources")
54+
from(relocateSources)
11755
}
11856

11957
publishing {
12058
publications {
12159
create<MavenPublication>("maven") {
122-
artifact(mainArtifact)
123-
artifact(sourcesArtifact)
60+
artifact(prepare)
61+
artifact(prepareSources)
12462
}
12563
}
12664

dependencies/protobuf/protobuf-relocated/build.gradle.kts

+6-16
Original file line numberDiff line numberDiff line change
@@ -15,38 +15,30 @@ val baseProtobuf by configurations.creating
1515
val baseProtobufSources by configurations.creating
1616

1717
val protobufVersion: String by rootProject.extra
18-
val protobufJarPrefix = "protobuf-$protobufVersion"
1918

20-
val renamedSources = "$buildDir/renamedSrc/"
21-
val outputJarsPath = "$buildDir/libs"
19+
val renamedSources = "${layout.buildDirectory.get()}/renamedSrc/"
20+
val outputJarsPath = "${layout.buildDirectory.get()}/libs"
2221

2322
dependencies {
24-
baseProtobuf("com.google.protobuf:protobuf-java:$protobufVersion")
25-
baseProtobufSources("com.google.protobuf:protobuf-java:$protobufVersion:sources")
23+
baseProtobuf("com.google.protobuf:protobuf-java:$protobufVersion") { isTransitive = false }
24+
baseProtobufSources("com.google.protobuf:protobuf-java:$protobufVersion:sources") { isTransitive = false }
2625
}
2726

2827
val prepare = tasks.register<ShadowJar>("prepare") {
2928
destinationDirectory.set(File(outputJarsPath))
3029
archiveVersion.set(protobufVersion)
3130
archiveClassifier.set("")
32-
from(
33-
provider {
34-
baseProtobuf.files.find { it.name.startsWith("protobuf-java") }?.canonicalPath
35-
}
36-
)
31+
from(baseProtobuf)
3732

3833
relocate("com.google.protobuf", "org.jetbrains.kotlin.protobuf" ) {
3934
exclude("META-INF/maven/com.google.protobuf/protobuf-java/pom.properties")
4035
}
4136
}
4237

43-
artifacts.add("default", prepare)
44-
4538
val relocateSources = task<Copy>("relocateSources") {
4639
from(
4740
provider {
48-
zipTree(baseProtobufSources.files.find { it.name.startsWith("protobuf-java") && it.name.endsWith("-sources.jar") }
49-
?: throw GradleException("sources jar not found among ${baseProtobufSources.files}"))
41+
zipTree(baseProtobufSources.files.single())
5042
}
5143
)
5244

@@ -62,8 +54,6 @@ val prepareSources = task<Jar>("prepareSources") {
6254
from(relocateSources)
6355
}
6456

65-
artifacts.add("default", prepareSources)
66-
6757
publishing {
6858
publications {
6959
create<MavenPublication>("maven") {

0 commit comments

Comments
 (0)