|
1 |
| - |
2 |
| -import java.io.BufferedOutputStream |
| 1 | +import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar |
3 | 2 | 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 |
8 | 3 |
|
9 | 4 | plugins {
|
10 |
| - base |
| 5 | + `java-base` |
11 | 6 | `maven-publish`
|
| 7 | + id("com.github.johnrengelman.shadow") version "4.0.3" apply false |
| 8 | +} |
| 9 | + |
| 10 | +repositories { |
| 11 | + mavenCentral() |
12 | 12 | }
|
13 | 13 |
|
14 | 14 | val relocatedProtobuf by configurations.creating
|
15 | 15 | val relocatedProtobufSources by configurations.creating
|
16 | 16 |
|
17 | 17 | 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" |
20 | 21 |
|
21 | 22 | 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 } |
23 | 25 | }
|
24 | 26 |
|
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) |
53 | 32 |
|
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") |
88 | 35 | }
|
89 | 36 | }
|
90 | 37 |
|
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 | + ) |
99 | 44 |
|
100 |
| - into("$buildDir/libs/") |
101 |
| - rename { sourcesJarName } |
102 |
| -} |
| 45 | + into(renamedSources) |
103 | 46 |
|
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") } |
112 | 48 | }
|
113 | 49 |
|
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) |
117 | 55 | }
|
118 | 56 |
|
119 | 57 | publishing {
|
120 | 58 | publications {
|
121 | 59 | create<MavenPublication>("maven") {
|
122 |
| - artifact(mainArtifact) |
123 |
| - artifact(sourcesArtifact) |
| 60 | + artifact(prepare) |
| 61 | + artifact(prepareSources) |
124 | 62 | }
|
125 | 63 | }
|
126 | 64 |
|
|
0 commit comments