/
build.gradle
130 lines (109 loc) · 4.03 KB
/
build.gradle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import com.github.jengelman.gradle.plugins.shadow.transformers.Transformer
import com.github.jengelman.gradle.plugins.shadow.transformers.TransformerContext
import org.gradle.api.file.FileTreeElement
import shadow.org.apache.tools.zip.ZipOutputStream
import shadow.org.apache.tools.zip.ZipEntry
buildscript {
repositories {
jcenter()
}
dependencies {
classpath "com.github.jengelman.gradle.plugins:shadow:6.1.0"
}
}
plugins {
id "java"
id "maven-publish"
id "com.github.johnrengelman.shadow"
}
description = "gRPC: Netty Shaded"
sourceSets { testShadow {} }
dependencies {
implementation project(':grpc-netty')
runtimeOnly libraries.netty_tcnative,
libraries.netty_epoll
testShadowImplementation files(shadowJar),
project(':grpc-testing-proto'),
project(':grpc-testing'),
libraries.truth
shadow project(':grpc-netty').configurations.runtimeClasspath.allDependencies.matching {
it.group != 'io.netty'
}
}
jar {
// Must use a different classifier to avoid conflicting with shadowJar
classifier = 'original'
}
shadowJar {
classifier = null
dependencies {
include(project(':grpc-netty'))
include(dependency('io.netty:'))
}
exclude 'META-INF/maven/**'
relocate 'io.grpc.netty', 'io.grpc.netty.shaded.io.grpc.netty'
relocate 'io.netty', 'io.grpc.netty.shaded.io.netty'
// We have to be careful with these replacements as they must not match any
// string in NativeLibraryLoader, else they cause corruption. Note that
// this includes concatenation of string literals and constants.
relocate 'META-INF/native/libnetty', 'META-INF/native/libio_grpc_netty_shaded_netty'
relocate 'META-INF/native/netty', 'META-INF/native/io_grpc_netty_shaded_netty'
transform(NettyResourceTransformer.class)
mergeServiceFiles()
}
publishing {
publications {
maven(MavenPublication) {
project.shadow.component(it)
// Empty jars are not published via withJavadocJar() and withSourcesJar()
artifact javadocJar
artifact sourcesJar
// shadow.component() is run after the main build.gradle's withXml
pom.withXml {
asNode().dependencies.'*'.findAll() { dep ->
dep.artifactId.text() in ['grpc-api', 'grpc-core']
}.each() { core ->
core.version*.value = "[" + core.version.text() + "]"
}
}
}
}
}
task testShadow(type: Test) {
testClassesDirs = sourceSets.testShadow.output.classesDirs
classpath = sourceSets.testShadow.runtimeClasspath
}
compileTestShadowJava.options.compilerArgs = compileTestJava.options.compilerArgs
compileTestShadowJava.options.encoding = compileTestJava.options.encoding
test.dependsOn testShadow
/**
* A Transformer which updates the Netty JAR META-INF/ resources to accurately
* reference shaded class names.
*/
class NettyResourceTransformer implements Transformer {
// A map of resource file paths to be modified
private Map<String, String> resources = [:]
@Override
boolean canTransformResource(FileTreeElement fileTreeElement) {
fileTreeElement.name.startsWith("META-INF/native-image/io.netty")
}
@Override
void transform(TransformerContext context) {
String updatedContent = context.is.getText().replace("io.netty", "io.grpc.netty.shaded.io.netty")
resources.put(context.path, updatedContent)
}
@Override
boolean hasTransformedResource() {
resources.size() > 0
}
@Override
void modifyOutputStream(ZipOutputStream outputStream, boolean preserveFileTimestamps) {
for (resourceEntry in resources) {
ZipEntry entry = new ZipEntry(resourceEntry.key)
entry.time = TransformerContext.getEntryTimestamp(preserveFileTimestamps, entry.time)
outputStream.putNextEntry(entry)
outputStream.write(resourceEntry.value.getBytes())
outputStream.closeEntry()
}
}
}