-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathbuild.gradle
458 lines (404 loc) · 15.8 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
import groovy.json.JsonOutput
plugins {
// Observing higher memory usage with task-tree plugin. Disabling except if needed
// id "com.dorongold.task-tree" version "2.1.1"
id "com.diffplug.spotless" version '7.0.2'
id 'io.freefair.lombok' version '8.6' apply false
id 'jacoco'
id 'com.github.johnrengelman.shadow' version '8.1.1' apply false
id 'me.champeau.jmh' version '0.7.2' apply false
}
allprojects {
repositories {
mavenCentral()
}
}
// Ensure Capture Proxy and Dependencies are built with JDK 11 for On Node Install
// Cache dependencies during the configuration phase
ext.captureProxyDependencies = []
gradle.projectsEvaluated {
def captureProxyProject = rootProject.project(":TrafficCapture:trafficCaptureProxyServer")
captureProxyDependencies = captureProxyProject.configurations.collectMany { configuration ->
configuration.dependencies.findAll { it instanceof ProjectDependency }.collect { it.dependencyProject.path }
}
// Add the TrafficCaptureProxyServer project itself
captureProxyDependencies << captureProxyProject.path
}
// Modify sourceCompatibility during the execution phase
gradle.taskGraph.whenReady { taskGraph ->
allprojects {
tasks.withType(JavaCompile).configureEach {
if (project.path in rootProject.captureProxyDependencies) {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
} else {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
}
}
}
// Define version properties
ext {
// support -Dbuild.version, but include default
buildVersion = System.getProperty("build.version", "0.1.0")
// support -Dbuild.snapshot=false, but default to true
buildSnapshot = System.getProperty("build.snapshot", "true") == "true"
finalVersion = buildSnapshot ? "${buildVersion}-SNAPSHOT" : buildVersion
}
allprojects {
version = finalVersion
// This should eventually change, see https://opensearch.atlassian.net/browse/MIGRATIONS-2167
group = 'org.opensearch.migrations.trafficcapture'
tasks.withType(Jar).tap {
configureEach {
manifest {
attributes(
'SPDX-License-Identifier': 'Apache-2.0'
)
}
}
}
}
subprojects { subproject ->
subproject.afterEvaluate {
if (subproject.plugins.hasPlugin('java') && subproject.name != 'commonDependencyVersionConstraints') {
subproject.dependencies {
implementation project(":commonDependencyVersionConstraints")
annotationProcessor project(":commonDependencyVersionConstraints")
if (subproject.plugins.hasPlugin('java-test-fixtures')) {
testFixturesImplementation project(":commonDependencyVersionConstraints")
}
}
}
}
}
task buildDockerImages() {
dependsOn(':TrafficCapture:dockerSolution:buildDockerImages')
dependsOn(':DocumentsFromSnapshotMigration:buildDockerImages')
}
def commonExclusions = ['**/build/**', '**/node_modules/**', '**/opensearch-cluster-cdk/**', '**/cdk.out/**']
spotless {
format 'misc', {
target fileTree('.') {
include '**/*.gradle', '.gitattributes', '.gitignore'
exclude commonExclusions
}
trimTrailingWhitespace()
leadingTabsToSpaces()
endWithNewline()
}
yaml {
target fileTree('.') {
include '**/*.yml'
exclude commonExclusions
}
trimTrailingWhitespace()
leadingTabsToSpaces()
endWithNewline()
}
json {
target fileTree('.') {
include '*.json'
exclude commonExclusions
}
prettier()
endWithNewline()
}
}
subprojects {
apply plugin: "com.diffplug.spotless"
apply plugin: 'jacoco'
apply plugin: 'java'
apply plugin: 'maven-publish'
java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(17))
vendor.set(JvmVendorSpec.AMAZON)
}
}
// See https://github.com/diffplug/spotless/tree/main/plugin-gradle#java for some documentation,
// though what '#' does is still undocumented from what I can tell
spotless {
java {
target "**/*.java"
targetExclude '**/build/**', ".gradle/**"
importOrder(
'javax',
'java',
'org.opensearch',
'',
'\\#')
leadingTabsToSpaces()
endWithNewline()
removeUnusedImports()
}
}
tasks.withType(Test) {
// Getting javadoc to compile is part of the test suite to ensure we are able to publish our artifacts
dependsOn project.javadoc
}
if (!sourceSets.test.allSource.files.isEmpty()) {
tasks.withType(Test) {
testLogging {
events "passed", "skipped", "failed"
exceptionFormat "full"
showExceptions true
showCauses true
showStackTraces true
}
maxParallelForks = gradle.startParameter.maxWorkerCount
// Provide way to exclude particular tests from CLI
// e.g. ../gradlew test -PexcludeTests=**/KafkaProtobufConsumerLongTermTest*
if (project.hasProperty('excludeTests')) {
exclude project.property('excludeTests')
}
useJUnitPlatform()
// Disable parallel test execution, see MIGRATIONS-1666
systemProperty 'junit.jupiter.execution.parallel.enabled', 'false'
systemProperty 'log4j2.contextSelector', 'org.apache.logging.log4j.core.selector.BasicContextSelector'
// Verify assertions in tests
jvmArgs = ['-ea', '-XX:+HeapDumpOnOutOfMemoryError']
jacoco {
enabled = true
destinationFile = layout.buildDirectory.file("jacoco/${project.path.replace(':', '-')}-${project.name}-${name}.exec").get().asFile
}
}
// Mutually exclusive tests to avoid duplication
tasks.named('test') {
systemProperty 'migrationLogLevel', 'TRACE'
useJUnitPlatform {
excludeTags('longTest', 'isolatedTest')
}
}
tasks.register('slowTest', Test) {
systemProperty 'migrationLogLevel', 'DEBUG'
useJUnitPlatform {
includeTags 'longTest'
excludeTags 'isolatedTest'
}
}
tasks.register('isolatedTest', Test) {
maxParallelForks = 1
useJUnitPlatform {
includeTags 'isolatedTest'
}
}
tasks.register('fullTest') {
dependsOn test
dependsOn slowTest
dependsOn isolatedTest
}
} else {
tasks.withType(Test) {
jacoco {
enabled = false
}
}
}
task javadocJar(type: Jar, dependsOn: javadoc) {
archiveClassifier.set('javadoc')
from javadoc.destinationDir
}
task sourcesJar(type: Jar) {
archiveClassifier.set('sources')
from sourceSets.main.allSource
duplicatesStrategy = DuplicatesStrategy.WARN
}
def excludedProjectPaths = [
':RFS',
':TrafficCapture',
':TrafficCapture:dockerSolution',
]
if (!(project.path in excludedProjectPaths)) {
publishing {
publications {
mavenJava(MavenPublication) {
versionMapping {
allVariants {
// Test fixtures are published as a separate jar in maven
// This ensures dependencies that are only declared in test
// fixtures have a version number in the pom
if (project.plugins.hasPlugin('java-test-fixtures')) {
fromResolutionOf('testFixturesRuntimeClasspath')
}
fromResolutionResult()
}
}
from components.java
artifact javadocJar
artifact sourcesJar
pom {
name = project.name
description = 'Everything opensearch migrations'
url = 'http://github.com/opensearch-project/opensearch-migrations'
licenses {
license {
name = 'The Apache License, Version 2.0'
url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
}
}
developers {
developer {
name = "OpenSearch"
url = "https://github.com/opensearch-project/opensearch-migrations"
}
}
scm {
connection = "scm:git@github.com:opensearch-project/opensearch-migrations.git"
developerConnection = "scm:git@github.com:opensearch-project/opensearch-migrations.git"
url = "git@github.com:opensearch-project/opensearch-migrations.git"
}
}
pom.withXml {
def pomFile = asNode()
// Find all dependencies in the POM file
def dependencies = pomFile.dependencies.dependency
// Iterate over each dependency and check if the version is missing
dependencies.each { dependency ->
def version = dependency.version.text()
if (version == null || version.trim().isEmpty() || version.trim() == 'unspecified') {
def groupId = dependency.groupId.text()
def artifactId = dependency.artifactId.text()
throw new GradleException("Dependency ${groupId}:${artifactId} is missing a version in the pom.xml")
}
}
}
// Suppress POM metadata warnings for test fixtures
suppressPomMetadataWarningsFor('testFixturesApiElements')
suppressPomMetadataWarningsFor('testFixturesRuntimeElements')
}
}
repositories {
maven { url = "${rootProject.buildDir}/repository"}
maven {
url "https://aws.oss.sonatype.org/content/repositories/snapshots"
name = 'staging'
}
}
}
}
// Utility task to allow copying required libraries into a 'dependencies' folder for security scanning
tasks.register('copyDependencies', Sync) {
duplicatesStrategy = DuplicatesStrategy.WARN
from configurations.runtimeClasspath
into "${buildDir}/dependencies"
}
def testsWithJacoco = project.tasks.withType(Test).matching { it.jacoco && it.jacoco.enabled }
jacocoTestReport {
dependsOn = testsWithJacoco
executionData.from testsWithJacoco*.jacoco.destinationFile
reports {
xml.required = true
}
}
}
tasks.register('jacocoAggregateReport', JacocoReport) {
group = 'Verification'
description = 'Generates an aggregate report from exec files in build/jacocoMerged/*.exec over the whole project'
// Find all merged .exec files
executionData.setFrom(fileTree(dir: "${buildDir}", includes: [
"jacocoMerged/**/*.exec"
]))
// Get all subprojects with Java plugin
def javaProjects = subprojects.findAll { it.plugins.hasPlugin('java') }
// Collect all class directories from Java subprojects
classDirectories.setFrom(
files(javaProjects.collect { project ->
project.sourceSets.main.output.classesDirs.filter { dir ->
!dir.path.contains('captureProtobufs') &&
!dir.path.contains('trafficCaptureProxyServerTest')
}
})
)
// Collect all source directories from Java subprojects
sourceDirectories.setFrom(
files(javaProjects.collect { project ->
project.sourceSets.main.allSource.srcDirs
})
)
reports {
xml.required = true
xml.destination file("${buildDir}/reports/jacoco/mergedReport/jacocoMergedReport.xml")
html.required = true
html.destination file("${buildDir}/reports/jacoco/mergedReport/html")
}
}
gradle.projectsEvaluated {
List<Task> isolatedTestsTasks = []
List<Task> sharedProcessTestsTasks = []
subprojects { subproject ->
subproject.tasks.withType(Test).all { task ->
if (task.name == "isolatedTest") {
isolatedTestsTasks.add(task)
} else {
sharedProcessTestsTasks.add(task)
}
}
}
isolatedTestsTasks.sort { task -> task.project.name }
// Create a sequential dependency chain
Task previousTask = null
isolatedTestsTasks.each { task ->
sharedProcessTestsTasks.forEach {task.mustRunAfter(it) }
if (previousTask != null) {
task.mustRunAfter(previousTask)
}
previousTask = task
}
tasks.register("allTests") {
dependsOn sharedProcessTestsTasks
dependsOn isolatedTestsTasks
}
}
task mergeJacocoReports {
def jacocoReportTasks = subprojects.collect { it.tasks.withType(JacocoReport).matching { it.name == "jacocoTestReport" } }.flatten()
dependsOn jacocoReportTasks
// Create a Sync task to collect all exec files
def syncJacocoExecFiles = tasks.create("syncJacocoExecFiles", Sync) {
from jacocoReportTasks.collect { it.executionData }
into "${buildDir}/jacocoMerged/"
duplicatesStrategy = DuplicatesStrategy.FAIL
}
// Make sure sync task runs after all report tasks
syncJacocoExecFiles.mustRunAfter jacocoReportTasks
dependsOn syncJacocoExecFiles
// Finalize with jacocoAggregateReport
finalizedBy jacocoAggregateReport
}
task listPublishedArtifacts {
doLast {
subprojects.each { proj ->
def publishingExtension = proj.extensions.findByType(PublishingExtension)
if (publishingExtension) {
publishingExtension.publications.each { publication ->
if (publication instanceof MavenPublication) {
println "${publication.groupId}.${publication.artifactId}"
}
}
}
}
}
}
tasks.register("listTestTasksAsJson") {
doLast {
def testTasks = []
subprojects.each { subproject ->
def projectPath = subproject.path
// Collect test tasks without JaCoCo enabled
subproject.tasks.withType(Test).findAll {
!(it.extensions.findByType(JacocoTaskExtension)?.enabled ?: false)
}.each {
testTasks << "${projectPath}:${it.name}"
}
// Add jacocoTestReport if any test task has JaCoCo enabled
if (subproject.tasks.withType(Test).any {
it.extensions.findByType(JacocoTaskExtension)?.enabled ?: false
}) {
testTasks << "${projectPath}:jacocoTestReport"
}
}
// Print as a clean JSON list
println JsonOutput.prettyPrint(JsonOutput.toJson(testTasks))
}
}