-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbuild.gradle
521 lines (452 loc) · 14.9 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/*
* Copyright 2017-2025 O2 Czech Republic, a.s.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
buildscript {
repositories {
mavenCentral()
}
}
plugins {
id "com.github.hierynomus.license" version "0.16.1"
id 'com.github.johnrengelman.shadow' version '7.1.2'
id "org.sonarqube" version "4.0.0.2929"
}
version = '0.15-SNAPSHOT'
group = 'cz.o2.proxima'
configureRepositores(project)
subprojects {
version = rootProject.version
group = rootProject.group
configureRepositores(it)
apply plugin: 'cz.o2.proxima.java-conventions'
license {
header project.file("license-header.txt")
mapping("java", "SLASHSTAR_STYLE")
mapping("gradle", "SLASHSTAR_STYLE")
mapping("txt", "SCRIPT_STYLE")
mapping("proto", "SLASHSTAR_STYLE")
mapping("avsc", "JAVADOC_STYLE")
mapping("yaml", "SCRIPT_STYLE")
mapping("conf", "SLASHSTAR_STYLE")
mapping("cql", "SLASHSTAR_STYLE")
}
configurations {
// generic rules
compileAnnotationProcessor
provided
compileClasspath.extendsFrom compileAnnotationProcessor
annotationProcessor.extendsFrom compileAnnotationProcessor
testAnnotationProcessor.extendsFrom annotationProcessor
testCompileOnly.extendsFrom testAnnotationProcessor
compileOnly.extendsFrom provided
testImplementation.extendsFrom provided
// for shadow
intoShadow
implementation.extendsFrom intoShadow
}
compileJava {
options.javaModuleVersion = provider { version }
}
tasks.register("configureJar") {
doLast {
includeModuleName(project, jar)
}
}
tasks.register("verifyDependencies") {
doLast {
def banned = [/j2objc-annotations/, /javax.annotation/, /checkerframework/]
def runtimeDeps = project.configurations.runtimeClasspath.files - project.configurations.intoShadow.files
def invalid = runtimeDeps.findAll({ banned.find({ pattern -> it =~ pattern }) })
if (!invalid.isEmpty()) {
throw new IllegalArgumentException("Illegal dependencies ${invalid} in ${project.description}")
}
}
}
jar.dependsOn configureJar
configurations.all {
resolutionStrategy {
eachDependency {
if (it.target.group == "io.grpc") {
it.useVersion("${grpc_version}")
}
}
}
}
}
sonar {
properties {
property "sonar.sourceEncoding", "UTF-8"
property 'sonar.coverage.jacoco.xmlReportPaths', "${projectDir.path}/build/reports/jacoco/test/jacocoTestReport.xml"
}
}
if (project.hasProperty("with-coverage")) {
subprojects {
apply plugin: 'jacoco'
jacocoTestReport {
reports {
xml.required = true
}
}
test {
finalizedBy jacocoTestReport
}
}
}
def collectSources(p, target) {
def collectSingle = {
def path = it.path
def targetPath = "${target}/${path.substring(path.indexOf("cz/"))}"
def tp = java.nio.file.Path.of(targetPath)
def sp = java.nio.file.Path.of(path)
if (!java.nio.file.Files.exists(tp)) {
if (!java.nio.file.Files.exists(tp.getParent())) {
java.nio.file.Files.createDirectories(tp.getParent())
}
java.nio.file.Files.copy(sp, tp)
}
}
p.sourceSets.main.allJava.each {
if (it.path.contains("cz/o2")) {
collectSingle(it)
}
}
}
task javadocAggregate(type: Javadoc) {
group = "Documentation"
description = "Generate aggregated javadoc"
dependsOn subprojects.collectMany { [it.tasks["compileJava"], it.tasks["compileTestJava"]] }
def target = "${buildDir}/docs/collected"
subprojects.each { collectSources(it, target) }
source files(target)
classpath = files(subprojects.collect { it.sourceSets.main.compileClasspath } + subprojects.collect { it.sourceSets.test.compileClasspath })
destinationDir = file("${buildDir}/docs/apidocs")
failOnError = true
exclude "cz/o2/proxima/example/**"
}
import com.github.jengelman.gradle.plugins.shadow.transformers.Transformer
import com.github.jengelman.gradle.plugins.shadow.transformers.TransformerContext
import shadow.org.apache.tools.zip.ZipEntry
import shadow.org.apache.tools.zip.ZipOutputStream
import org.gradle.api.file.FileTreeElement
import org.codehaus.plexus.util.IOUtil
@groovy.transform.CompileStatic
class RenamingTransformer implements Transformer {
private Closure<String> renameClosure = { null }
private final Map<String, String> transformedPaths = [:]
private final Map<String, byte[]> contents = [:]
@Override
boolean canTransformResource(FileTreeElement element) {
renameClosure(element.relativePath.pathString) != null
}
@Override
void transform(TransformerContext context) {
def path = context.path
def outputPath = renameClosure(path)
if (outputPath != null) {
transformedPaths[path] = outputPath
contents[path] = getBytes(context.is)
}
}
@Override
boolean hasTransformedResource() { !transformedPaths.isEmpty() }
@Override
void modifyOutputStream(ZipOutputStream os, boolean preserveFileTimestamps) {
def zipWriter = new OutputStreamWriter(os, "UTF-8")
transformedPaths.each { path, renamedPath ->
ZipEntry entry = new ZipEntry(renamedPath)
entry.time = TransformerContext.getEntryTimestamp(preserveFileTimestamps, entry.time)
os.putNextEntry(entry)
IOUtil.copy(new ByteArrayInputStream(contents[path]), zipWriter)
zipWriter.flush()
os.closeEntry()
}
}
private byte[] getBytes(InputStream in) {
ByteArrayOutputStream baos = new ByteArrayOutputStream()
IOUtil.copy(in, baos)
baos.toByteArray()
}
}
@groovy.transform.CompileStatic
def renamingTransformer() {
RenamingTransformer.class
}
def configureRepositores(project) {
project.with {
repositories {
mavenCentral()
maven {
url = uri('https://packages.confluent.io/maven/')
}
if (project.version.endsWith("-SNAPSHOT")) {
maven {
url = uri("https://oss.sonatype.org/content/repositories/snapshots")
}
}
mavenLocal()
}
}
}
def registerTestsJar(Project project) {
registerTestsJar(project, {})
}
def registerTestsJar(Project project, Closure configureClosure) {
def tasks = project.tasks
tasks.register('testsJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) {
archiveClassifier = 'tests'
from project.sourceSets.test.output
configurations = [project.configurations.testRuntimeClasspath]
}
def testsJar = project.tasks["testsJar"]
project.configurations.create("testsJar") {
outgoing {
artifact(testsJar)
}
}
testsJar.exclude "*.jar"
if (tasks.findByPath("shadowJar")) {
// apply all relocations from shadowJar to testsJar
project.shadowJar.relocators.each {
testsJar.relocate it
}
}
configureClosure.delegate = testsJar
configureClosure()
}
def processDependency(def dependenciesNode, def it, def scope) {
def dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', it.group)
dependencyNode.appendNode('artifactId', it.name)
dependencyNode.appendNode('version', it.version)
dependencyNode.appendNode('scope', scope)
if (it.artifacts.find { a -> a.classifier }) {
dependencyNode.appendNode("classifier", it.artifacts.find { a -> a.classifier }?.classifier)
}
}
def asModuleName(name) {
// cut anything before ':'
def jar = name.substring(name.indexOf(':') + 1)
if (!jar.startsWith("proxima")) {
return null;
}
def stripped = jar.substring("proxima".length() + 1)
return "cz.o2.proxima.${stripped.replace("-", ".")}"
}
def isAlreadyModule(project) {
return project.sourceSets.main.allJava.findIndexOf({ it.path.endsWith("module-info.java") }) != -1
}
def updateModulePath(project) {
updateModulePath(project, { true })
}
def updateModulePath(project, Closure<Boolean> includeClosure) {
project.compileJava {
doFirst {
def modules = classpath.filter(includeClosure)
options.compilerArgs += ['--module-path', modules.asPath]
classpath = classpath.filter { !includeClosure(it) }
}
}
project.javadoc {
def cp = classpath.files as List
options.modulePath = cp.findAll(includeClosure)
classpath = files(cp.findAll { !includeClosure(it) })
}
}
def includeModuleName(project, delegate) {
def moduleName = project.description ? asModuleName(project.description) : null
if (moduleName && !isAlreadyModule(project)) {
delegate.manifest {
attributes("Automatic-Module-Name": asModuleName(project.description))
}
}
}
def enableShadowJar(project) {
enableShadowJar(project, { true })
}
def enableShadowJar(project, Closure<Boolean> allowedEntry) {
project.with {
apply plugin: 'com.github.johnrengelman.shadow'
jar {
dependsOn shadowJar
enabled = false
}
tasks.register("configureShadow") {
doLast {
includeModuleName(project, shadowJar)
}
}
shadowJar {
dependsOn configureShadow
archiveClassifier = null
dependencies {
exclude "META-INF/maven/**"
}
mergeServiceFiles()
configurations = [ project.configurations.intoShadow ]
if (isAlreadyModule(project)) {
excludes.remove("module-info.class")
}
exclude "org/checkerframework/**"
}
tasks.create("validateShadowJar") {
dependsOn shadowJar
doLast {
def jarFile = configurations.shadowJar.outgoing.artifacts.files.singleFile
def zip = new java.util.zip.ZipFile(jarFile)
def disallowed = zip.entries().findAll { !it.isDirectory() && !allowedEntry(it.name) }.collect { it }
if (!disallowed.isEmpty()) {
throw new IllegalArgumentException("Disallowed entries ${disallowed} in ${jarFile.path}")
}
}
}
shadowJar.finalizedBy validateShadowJar
configurations.create("shadowJar") {
outgoing {
artifact(shadowJar)
}
}
configurations["default"].outgoing.artifacts.clear()
}
}
def publishArtifacts(project, scope) {
if (!project.hasProperty(scope)) {
return
}
project.with {
publishing {
publications {
maven(MavenPublication) { publication ->
if (project.hasProperty("shadowJar")) {
artifact(shadowJar)
//project.shadow.component(publication)
} else {
artifact(jar)
}
pom {
name = "${project.group}:${project.name}"
description = "Proxima platform's module ${project.name}"
url = "https://github.com/O2-Czech-Republic/proxima-platform.git"
scm {
connection = "scm:git:[email protected]:O2-Czech-Republic/proxima-platform.git"
developerConnection = "scm:git:[email protected]:O2-Czech-Republic/proxima-platform.git"
url = "https://github.com/O2-Czech-Republic/proxima-platform.git"
}
developers {
developer {
id = "je-ik"
name = "Jan Lukavský"
email = "[email protected]"
timezone = "Europe/Prague"
}
}
licenses {
license {
name = 'The Apache License, Version 2.0'
url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
}
}
}
pom.withXml {
def dependenciesNode = asNode()['dependencies'][0]
if (dependenciesNode == null) {
dependenciesNode = asNode().appendNode('dependencies')
}
configurations.compileClasspath.allDependencies
.findAll { isExplicitScope(configurations, it) }
.each {
processDependency(dependenciesNode, it, "compile")
}
configurations.runtimeClasspath.allDependencies
.findAll { !configurations.compileClasspath.allDependencies.contains(it) }
.findAll { isExplicitScope(configurations, it) }
.each {
processDependency(dependenciesNode, it, "runtime")
}
configurations.testRuntimeClasspath.allDependencies
.findAll { !configurations.compileClasspath.allDependencies.contains(it)
&& !configurations.runtimeClasspath.allDependencies.contains(it) }
.findAll { isExplicitScope(configurations, it) }
.each {
processDependency(dependenciesNode, it, "test")
}
}
}
}
repositories {
def configureCredentials = {
if (project.hasProperty("sonatypeUsername")) {
username sonatypeUsername
password sonatypePassword
}
}
if (project.version.endsWith("-SNAPSHOT")) {
maven {
name = "sonatype"
url = "https://oss.sonatype.org/content/repositories/snapshots"
configureCredentials.delegate = credentials
configureCredentials()
}
} else {
maven {
name = "sonatype"
url = "https://oss.sonatype.org/service/local/staging/deploy/maven2/"
configureCredentials.delegate = credentials
configureCredentials()
}
}
}
}
if (project.hasProperty("publish")) {
java {
withJavadocJar()
withSourcesJar()
}
javadoc {
dependsOn compileJava, compileTestJava
failOnError = false
options.addBooleanOption('html5', true)
}
tasks.named("sourcesJar") {
dependsOn compileJava, compileTestJava
archiveClassifier = "sources"
}
tasks.named("javadoc") {
doLast {
options {
modulePath = configurations["compileClasspath"].incoming.files.files as List
}
}
}
if (tasks.findByPath("testsJar")) {
project.publishing.publications.maven.artifact(tasks.getByPath("testsJar"))
}
project.publishing.publications.maven.artifact(sourcesJar)
project.publishing.publications.maven.artifact(javadocJar)
if (!project.version.endsWith("-SNAPSHOT") && !project.hasProperty("noSigning")) {
apply plugin: "signing"
signing {
useGpgCmd()
publishing.publications.each { signing.sign(it) }
}
}
}
}
}
def isExplicitScope(configurations, dependency) {
!configurations.intoShadow.allDependencies.contains(dependency)
&& !configurations.compileOnly.allDependencies.contains(dependency)
&& !configurations.compileAnnotationProcessor.allDependencies.contains(dependency)
&& !configurations.provided.allDependencies.contains(dependency)
}