-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathbuild.gradle
182 lines (162 loc) · 5.61 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import com.github.jengelman.gradle.plugins.shadow.transformers.Transformer
import com.github.jengelman.gradle.plugins.shadow.transformers.CacheableTransformer
import com.github.jengelman.gradle.plugins.shadow.transformers.TransformerContext
import org.apache.tools.zip.ZipOutputStream
import org.apache.tools.zip.ZipEntry
import org.gradle.api.file.FileTreeElement
plugins {
id "java"
id "maven-publish"
id "com.gradleup.shadow"
id "ru.vyarus.animalsniffer"
}
description = "gRPC: Netty Shaded"
sourceSets { testShadow {} }
evaluationDependsOn(':grpc-netty')
dependencies {
implementation project(':grpc-netty')
runtimeOnly libraries.netty.tcnative,
libraries.netty.tcnative.classes
runtimeOnly (libraries.netty.tcnative) {
artifact {
classifier = "linux-x86_64"
}
}
runtimeOnly (libraries.netty.tcnative) {
artifact {
classifier = "linux-aarch_64"
}
}
runtimeOnly (libraries.netty.tcnative) {
artifact {
classifier = "osx-x86_64"
}
}
runtimeOnly (libraries.netty.tcnative) {
artifact {
classifier = "osx-aarch_64"
}
}
runtimeOnly (libraries.netty.tcnative) {
artifact {
classifier = "windows-x86_64"
}
}
runtimeOnly (libraries.netty.transport.epoll) {
artifact {
classifier = "linux-x86_64"
}
}
runtimeOnly (libraries.netty.transport.epoll) {
artifact {
classifier = "linux-aarch_64"
}
}
testShadowImplementation files(shadowJar),
project(':grpc-testing-proto'),
project(':grpc-testing'),
libraries.truth
shadow project(':grpc-netty').configurations.runtimeClasspath.allDependencies.matching {
it.group != 'io.netty'
}
signature (libraries.signature.java) {
artifact {
extension = "signature"
}
}
signature (libraries.signature.android) {
artifact {
extension = "signature"
}
}
}
tasks.named("jar").configure {
// Must use a different archiveClassifier to avoid conflicting with shadowJar
archiveClassifier = 'original'
manifest {
attributes('Automatic-Module-Name': 'io.grpc.netty.shaded')
}
}
tasks.named("shadowJar").configure {
archiveClassifier = 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
// Avoid confusing error message "class file for
// io.grpc.internal.AbstractServerImplBuilder not found"
// (https://github.com/grpc/grpc-java/issues/5881). This can be
// removed after https://github.com/grpc/grpc-java/issues/7211 is
// resolved.
pom.withXml {
asNode().dependencies.'*'.findAll() { dep ->
dep.artifactId.text() == 'grpc-core'
}.each() { core ->
core.scope*.value = "compile"
}
}
}
}
}
tasks.register("testShadow", Test) {
testClassesDirs = sourceSets.testShadow.output.classesDirs
classpath = sourceSets.testShadow.runtimeClasspath
}
tasks.named("compileTestShadowJava").configure {
options.compilerArgs = compileTestJava.options.compilerArgs
options.encoding = compileTestJava.options.encoding
}
tasks.named("test").configure {
dependsOn tasks.named("testShadow")
}
/**
* A Transformer which updates the Netty JAR META-INF/ resources to accurately
* reference shaded class names.
*/
@CacheableTransformer
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 updatedPath = context.path.replace("io.netty", "io.grpc.netty.shaded.io.netty")
String updatedContent = context.is.getText().replace("io.netty", "io.grpc.netty.shaded.io.netty")
resources.put(updatedPath, 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()
}
}
}