xref: /aosp_15_r20/external/grpc-grpc-java/xds/build.gradle (revision e07d83d3ffcef9ecfc9f7f475418ec639ff0e5fe)
1import java.util.jar.JarFile
2
3plugins {
4    id "java"
5    id "maven-publish"
6
7    id "com.github.johnrengelman.shadow"
8    id "com.google.protobuf"
9    id "ru.vyarus.animalsniffer"
10}
11
12description = "gRPC: XDS plugin"
13
14sourceSets {
15    thirdparty {
16        java {
17            srcDir "${projectDir}/third_party/zero-allocation-hashing/main/java"
18        }
19        proto {
20            srcDir 'third_party/envoy/src/main/proto'
21            srcDir 'third_party/protoc-gen-validate/src/main/proto'
22            srcDir 'third_party/xds/src/main/proto'
23            srcDir 'third_party/googleapis/src/main/proto'
24            srcDir 'third_party/istio/src/main/proto'
25        }
26    }
27    test {
28        java {
29            srcDir "${projectDir}/third_party/zero-allocation-hashing/test/java"
30        }
31    }
32}
33
34configurations {
35    pomDeps {
36        extendsFrom configurations.thirdpartyRuntimeClasspath, configurations.shadow
37    }
38}
39
40dependencies {
41    thirdpartyCompileOnly libraries.javax.annotation
42    thirdpartyImplementation project(':grpc-protobuf'),
43            project(':grpc-stub'),
44            libraries.opencensus.proto
45    implementation sourceSets.thirdparty.output
46    implementation project(':grpc-stub'),
47            project(':grpc-core'),
48            project(':grpc-services'),
49            project(':grpc-auth'),
50            project(path: ':grpc-alts', configuration: 'shadow'),
51            libraries.gson,
52            libraries.re2j,
53            libraries.auto.value.annotations,
54            libraries.protobuf.java.util
55    def nettyDependency = implementation project(':grpc-netty')
56
57    testImplementation project(':grpc-rls')
58    testImplementation testFixtures(project(':grpc-core'))
59
60    annotationProcessor libraries.auto.value
61    // At runtime use the epoll included in grpc-netty-shaded
62    compileOnly libraries.netty.transport.epoll
63
64    testImplementation project(':grpc-testing'),
65            project(':grpc-testing-proto')
66    testImplementation (libraries.netty.transport.epoll) {
67        artifact {
68            classifier = "linux-x86_64"
69        }
70    }
71    testImplementation (libraries.guava.testlib) {
72        exclude group: 'junit', module: 'junit'
73    }
74
75    shadow configurations.implementation.getDependencies().minus([nettyDependency])
76    shadow project(path: ':grpc-netty-shaded', configuration: 'shadow')
77
78    signature libraries.signature.java
79    testRuntimeOnly libraries.netty.tcnative,
80            libraries.netty.tcnative.classes
81    testRuntimeOnly (libraries.netty.tcnative) {
82        artifact {
83            classifier = "linux-x86_64"
84        }
85    }
86    testRuntimeOnly (libraries.netty.tcnative) {
87        artifact {
88            classifier = "linux-aarch_64"
89        }
90    }
91    testRuntimeOnly (libraries.netty.tcnative) {
92        artifact {
93            classifier = "osx-x86_64"
94        }
95    }
96    testRuntimeOnly (libraries.netty.tcnative) {
97        artifact {
98            classifier = "osx-aarch_64"
99        }
100    }
101    testRuntimeOnly (libraries.netty.tcnative) {
102        artifact {
103            classifier = "windows-x86_64"
104        }
105    }
106}
107
108configureProtoCompilation()
109
110tasks.named("compileThirdpartyJava").configure {
111    options.errorprone.enabled = false
112    options.compilerArgs += [
113        // valueOf(int) in RoutingPriority has been deprecated
114        "-Xlint:-deprecation",
115    ]
116}
117
118tasks.named("checkstyleThirdparty").configure {
119    enabled = false
120}
121
122tasks.named("compileJava").configure {
123    it.options.compilerArgs += [
124        // TODO: remove
125        "-Xlint:-deprecation",
126        // only has AutoValue annotation processor
127        "-Xlint:-processing",
128    ]
129    appendToProperty(
130            it.options.errorprone.excludedPaths,
131            ".*/build/generated/sources/annotationProcessor/java/.*",
132            "|")
133    // remove after upgrade error-prone
134    appendToProperty(
135            it.options.errorprone.excludedPaths,
136            ".*/build/tmp/compileJava/compileTransaction/annotation-output/io/grpc/xds/.*",
137            "|")
138}
139
140tasks.named("jar").configure {
141    archiveClassifier = 'original'
142    from sourceSets.thirdparty.output
143}
144
145tasks.named("javadoc").configure {
146    // Exclusions here should generally also be relocated
147    exclude 'com/github/udpa/**'
148    exclude 'com/github/xds/**'
149    exclude 'com/google/security/**'
150    exclude 'io/envoyproxy/**'
151    // Need to clean up the class structure to reduce how much is exposed
152    exclude 'io/grpc/xds/*LoadBalancer*'
153    exclude 'io/grpc/xds/Bootstrapper.java'
154    exclude 'io/grpc/xds/Envoy*'
155    exclude 'io/grpc/xds/FilterChainMatchingProtocolNegotiators.java'
156    exclude 'io/grpc/xds/TlsContextManager.java'
157    exclude 'io/grpc/xds/XdsAttributes.java'
158    exclude 'io/grpc/xds/XdsInitializationException.java'
159    exclude 'io/grpc/xds/XdsNameResolverProvider.java'
160    exclude 'io/grpc/xds/internal/**'
161    exclude 'io/grpc/xds/Internal*'
162}
163
164def prefixName = 'io.grpc.xds'
165tasks.named("shadowJar").configure {
166    archiveClassifier = null
167    dependencies {
168        include(project(':grpc-xds'))
169    }
170    // Relocated packages commonly need exclusions in jacocoTestReport and javadoc
171    relocate 'com.github.udpa', "${prefixName}.shaded.com.github.udpa"
172    relocate 'com.github.xds', "${prefixName}.shaded.com.github.xds"
173    relocate 'com.google.api.expr', "${prefixName}.shaded.com.google.api.expr"
174    relocate 'com.google.security', "${prefixName}.shaded.com.google.security"
175    // TODO: missing java_package option in .proto
176    relocate 'envoy.annotations', "${prefixName}.shaded.envoy.annotations"
177    relocate 'io.envoyproxy', "${prefixName}.shaded.io.envoyproxy"
178    relocate 'io.grpc.netty', 'io.grpc.netty.shaded.io.grpc.netty'
179    relocate 'io.netty', 'io.grpc.netty.shaded.io.netty'
180    // TODO: missing java_package option in .proto
181    relocate 'udpa.annotations', "${prefixName}.shaded.udpa.annotations"
182    relocate 'xds.annotations', "${prefixName}.shaded.xds.annotations"
183    exclude "**/*.proto"
184}
185
186def checkPackageLeakage = tasks.register("checkPackageLeakage") {
187    inputs.files(shadowJar).withNormalizer(CompileClasspathNormalizer)
188    outputs.file("${buildDir}/tmp/${name}") // Fake output for UP-TO-DATE checking
189    doLast {
190        def jarEntryPrefixName = prefixName.replaceAll('\\.', '/')
191        shadowJar.outputs.getFiles().each { jar ->
192            def package_leak_detected = false
193            JarFile jf = new JarFile(jar)
194            jf.entries().each { entry ->
195                if (entry.name.endsWith(".class") && !entry.name.startsWith(
196                        jarEntryPrefixName)) {
197                    package_leak_detected = true
198                    println "WARNING: package leaked, may need relocation: " +
199                            entry.name
200                }
201            }
202            jf.close()
203            if (package_leak_detected) {
204                throw new TaskExecutionException(shadowJar,
205                        new IllegalStateException("Resource leakage detected!"))
206            }
207        }
208    }
209}
210
211tasks.named("test").configure {
212    dependsOn checkPackageLeakage
213}
214
215tasks.named("jacocoTestReport").configure {
216    classDirectories.from = sourceSets.main.output.collect {
217        fileTree(dir: it,
218        exclude: [ // Exclusions here should generally also be relocated
219                '**/com/github/udpa/**',
220                '**/com/github/xds/**',
221                '**/com/google/api/expr/**',
222                '**/com/google/security/**',
223                '**/envoy/annotations/**',
224                '**/io/envoyproxy/**',
225                '**/udpa/annotations/**',
226                '**/xds/annotations/**',
227        ])
228    }
229}
230
231publishing {
232    publications {
233        maven(MavenPublication) {
234            // We want this to throw an exception if it isn't working
235            def originalJar = artifacts.find { dep -> dep.classifier == 'original'}
236            artifacts.remove(originalJar)
237
238            pom.withXml {
239                def dependenciesNode = new Node(null, 'dependencies')
240                project.configurations.pomDeps.allDependencies.each { dep ->
241                    if (dep.group == null && dep.name == 'unspecified') {
242                        // Ignore the thirdparty self-dependency
243                        return;
244                    }
245                    def dependencyNode = dependenciesNode.appendNode('dependency')
246                    dependencyNode.appendNode('groupId', dep.group)
247                    dependencyNode.appendNode('artifactId', dep.name)
248                    dependencyNode.appendNode('version', dep.version)
249                    dependencyNode.appendNode('scope', 'compile')
250                }
251                asNode().dependencies[0].replaceNode(dependenciesNode)
252            }
253        }
254    }
255}
256