forked from TEAMMATES/teammates
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.gradle
628 lines (558 loc) · 20.2 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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
apply plugin: "idea"
apply plugin: "war"
// apply plugin: "com.google.cloud.tools.appengine"
// see https://github.com/GoogleCloudPlatform/app-gradle-plugin/issues/125
project.pluginManager.apply com.google.cloud.tools.gradle.appengine.standard.AppEngineStandardPlugin
apply plugin: "checkstyle"
apply plugin: "pmd"
apply plugin: "com.github.spotbugs"
apply plugin: "jacoco"
apply plugin: "cz.habarta.typescript-generator"
def appengineVersion = "1.9.+"
def checkstyleVersion = "8.28"
def pmdVersion = "6.20.0"
def spotbugsVersion = "3.1.12"
def jacocoVersion = "0.8.5"
buildscript {
repositories {
jcenter()
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "com.google.cloud.tools:appengine-gradle-plugin:2.2.0"
classpath "gradle.plugin.com.github.spotbugs:spotbugs-gradle-plugin:2.0.0"
classpath("cz.habarta.typescript-generator:typescript-generator-gradle-plugin:2.19.577") {
exclude group: "org.gradle"
}
classpath "com.google.guava:guava:28.2-jre"
}
}
configurations {
staticAnalysis
}
repositories {
jcenter()
}
def objectify = "com.googlecode.objectify:objectify:5.1.22"
def testng = "org.testng:testng:7.1.1"
dependencies {
staticAnalysis("com.puppycrawl.tools:checkstyle:${checkstyleVersion}")
staticAnalysis("net.sourceforge.pmd:pmd-java:${pmdVersion}")
staticAnalysis("com.github.spotbugs:spotbugs:${spotbugsVersion}")
staticAnalysis("org.jacoco:org.jacoco.build:${jacocoVersion}")
annotationProcessor(objectify)
compile("com.google.appengine:appengine-api-1.0-sdk:${appengineVersion}")
// This dependency needs to be resolved individually
// because the main dependency (appengine-gcs-client) does not have its transitive dependencies up-to-date
compile("com.google.appengine.tools:appengine-gcs-client:0.8") {
// Use the newer servlet library instead
exclude group: "javax.servlet", module: "servlet-api"
}
compile("com.google.api-client:google-api-client-appengine:1.30.7") {
// Use the newer servlet library instead
exclude group: "javax.servlet", module: "servlet-api"
}
compile("com.google.apis:google-api-services-storage:v1-rev20191011-1.30.3")
compile("com.google.code.gson:gson:2.8.6")
compile("com.google.guava:guava:28.2-jre")
compile(objectify)
compile("com.googlecode.owasp-java-html-sanitizer:owasp-java-html-sanitizer:20191001.1")
compile("com.mailjet:mailjet-client:4.2.1")
compile("com.sendgrid:sendgrid-java:4.4.1")
compile("com.sun.jersey:jersey-client:1.19.4")
compile("com.sun.jersey:jersey-core:1.19.4")
compile("com.sun.jersey.contribs:jersey-multipart:1.19.4")
compile("javax.servlet:javax.servlet-api:3.1.0")
compile("org.jsoup:jsoup:1.12.1")
testAnnotationProcessor(testng)
testCompile("com.google.appengine:appengine-api-stubs:${appengineVersion}")
testCompile("com.google.appengine:appengine-remote-api:${appengineVersion}")
testCompile("com.google.appengine:appengine-testing:${appengineVersion}")
testCompile("com.tngtech.archunit:archunit:0.11.0")
testCompile("org.httpunit:httpunit:1.7.3")
testCompile("org.seleniumhq.selenium:selenium-java:3.141.59")
testCompile(testng)
// For access to Google APIs such as HTTP transport, authentication and JSON parsing
testCompile("com.google.api-client:google-api-client:1.30.7")
// For supporting authorization code flow locally
testCompile("com.google.oauth-client:google-oauth-client-jetty:1.30.5")
// For using Gmail API
testCompile("com.google.apis:google-api-services-gmail:v1-rev20191113-1.30.3")
// For using JMeter APIs
testCompile("org.apache.jmeter:ApacheJMeter_core:5.2.1")
testCompile("org.apache.jmeter:ApacheJMeter_http:5.2.1")
}
sourceSets {
main {
java {
srcDir "src/main/java"
include "**/*.java"
}
resources {
srcDir "src/main/resources"
exclude "**/*.java"
}
}
test {
java {
srcDir "src/test/java"
srcDir "src/e2e/java"
srcDir "src/client/java"
include "**/*.java"
}
resources {
srcDir "src/test/resources"
srcDir "src/e2e/resources"
srcDir "src/client/resources"
exclude "**/*.java"
}
}
}
tasks.withType(cz.habarta.typescript.generator.gradle.GenerateTask) {
jsonLibrary = "jackson2"
optionalAnnotations = [
"javax.annotation.Nullable"
]
outputFileType = "implementationFile"
outputKind = "module"
indentString = " "
jackson2Configuration = [
fieldVisibility: "ANY",
getterVisibility: "NONE",
isGetterVisibility: "NONE",
setterVisibility: "NONE",
creatorVisibility: "NONE",
]
mapEnum = "asEnum"
nonConstEnums = true
}
task generateApiOutputFormat(type: cz.habarta.typescript.generator.gradle.GenerateTask) {
classPatterns = [
"teammates.ui.webapi.output.**",
"teammates.common.datatransfer.questions.**"
]
removeTypeNameSuffix = "Data"
outputFile = "src/web/types/api-output.ts"
}
task generateApiRequestFormat(type: cz.habarta.typescript.generator.gradle.GenerateTask) {
classPatterns = [
"teammates.ui.webapi.request.**"
]
outputFile = "src/web/types/api-request.ts"
}
task generateApiEndpoints(type: cz.habarta.typescript.generator.gradle.GenerateTask) {
classPatterns = [
"teammates.ui.webapi.endpoints.**"
]
jsonLibrary = 'jackson2'
jackson2Configuration = [
'enumsUsingToString': true
]
outputFile = "src/web/types/api-endpoints.ts"
}
generateApiOutputFormat.dependsOn classes
generateApiRequestFormat.dependsOn classes
generateApiEndpoints.dependsOn classes
task generateTypes {
dependsOn generateApiOutputFormat
dependsOn generateApiRequestFormat
dependsOn generateApiEndpoints
}
// SETUP TASKS
task createConfigs {
description "Sets up the project by obtaining necessary files and configurations points."
group "Setup"
doLast {
def templatesToCopy = [
"gradle.template.properties",
"src/main/resources/build.template.properties",
"src/web/environments/config.template.ts",
"src/test/resources/test.template.properties",
"src/e2e/resources/test.template.properties",
"src/client/resources/client.template.properties",
"src/main/webapp/WEB-INF/appengine-web.template.xml"
]
templatesToCopy.findAll {
!(new File(it.replace(".template", ""))).exists()
}.each {
filename -> copy {
from filename
into filename.startsWith("src/") ? new File(filename).getParent() : "${projectDir}"
rename '(.*).template(.*)', '$1$2'
}
}
}
}
// IDE SETUP TASKS (MAINTENANCE MODE ONLY)
// Use a different buildDir for IntelliJ builds to avoid conflict with CLI builds.
// IDE detection is achieved by checking `idea.paths.selector`, a system property that IntelliJ sets.
// Note that this property is not documented and may change anytime.
if (System.getProperty('idea.paths.selector') != null) {
// Nest the buildDir inside IntelliJ's default project compiler output directory
buildDir = new File(projectDir, 'out/build')
}
idea {
module {
// Exclude the CLI build directory
excludeDirs += [file('build')]
}
}
task downloadDependencies {
doFirst {
configurations.compile.resolve()
}
}
task downloadTestDependencies {
doFirst {
configurations.testCompile.resolve()
}
}
task downloadLinters {
doFirst {
configurations.staticAnalysis.resolve()
}
}
// RUN SERVER TASKS
compileJava.options.encoding = "UTF-8"
compileTestJava.options.encoding = "UTF-8"
appengine {
tools {
if (project.properties['cloud.sdk.home'] != null) {
cloudSdkHome = project.properties['cloud.sdk.home']
}
}
run {
port = 8080
jvmFlags = ["-Xss2m", "-Dfile.encoding=UTF-8",
// Absolute paths are not supported, the following is relative to the project directory
// These only specify the datastore/blobstore paths, but search indexes are still generated in WEB-INF/appengine-generated
"-Ddatastore.backing_store=../../appengine-generated/local_db.bin",
"-Dblobstore.backing_store=../../appengine-generated"]
automaticRestart = true
}
deploy {
String buildPropertiesPath = "${projectDir}/src/main/resources/build.properties"
boolean propsExists = new File(buildPropertiesPath).exists()
Properties props = new Properties()
if (propsExists) {
file(buildPropertiesPath).withReader {
props.load(it)
}
}
projectId = propsExists ? props['app.id'] : null
version = propsExists ? props['app.version'] : null
stopPreviousVersion = false
promote = false
}
}
// STATIC ANALYSIS TASKS
def isWindows = System.getProperty("os.name").toLowerCase().contains("windows")
checkstyle {
toolVersion = checkstyleVersion
configFile = file("static-analysis/teammates-checkstyle.xml")
}
pmd {
toolVersion = pmdVersion
consoleOutput = true
ruleSetFiles = files("static-analysis/teammates-pmd.xml", "static-analysis/teammates-pmdMain.xml")
ruleSets = []
}
spotbugs {
reportLevel = "low"
toolVersion = spotbugsVersion
includeFilter = file("$rootProject.projectDir/static-analysis/teammates-spotbugs.xml")
}
tasks.withType(com.github.spotbugs.SpotBugsTask) {
reports {
xml.enabled = false
html.enabled = false
emacs.enabled = true
}
}
spotbugsMain {
finalizedBy "printSpotbugsMainViolations"
}
task printSpotbugsMainViolations {
onlyIf {
file("${buildDir}/reports/spotbugs/main.emacs").exists()
}
doFirst {
print new File("${buildDir}/reports/spotbugs/main.emacs").getText("UTF-8")
}
}
spotbugsTest {
finalizedBy "printSpotbugsTestViolations"
}
task printSpotbugsTestViolations {
onlyIf {
file("${buildDir}/reports/spotbugs/test.emacs").exists()
}
doFirst {
print new File("${buildDir}/reports/spotbugs/test.emacs").getText("UTF-8")
}
}
task lintMain {
dependsOn checkstyleMain, pmdMain, spotbugsMain
}
task lintTest {
dependsOn checkstyleTest, pmdTest, spotbugsTest
}
import org.gradle.internal.serialize.PlaceholderException
def checkTestNgFailureClosure = { descriptor, result ->
if (result.exception instanceof PlaceholderException
&& result.exception.toString().startsWith("org.gradle.api.internal.tasks.testing.TestSuiteExecutionException")) {
result.exception.printStackTrace()
throw new GradleException("Detected TestNG failure")
}
}
// Displays full exception; to be run after the test fails after the last retry
def afterTestClosure = { descriptor, result ->
if (result.resultType == TestResult.ResultType.FAILURE && result.exception != null && result.exception.getMessage() != null) {
println ""
def msg = result.exception.getMessage()
def shouldDisplayAsDiff = msg.indexOf("<<[expected>") != -1
if (shouldDisplayAsDiff) {
def expectedFileName = "expected-${descriptor.getClassName()}"
def actualFileName = "actual-${descriptor.getClassName()}"
file("${expectedFileName}").text = msg[(msg.indexOf("<<[expected>") + 12)..(msg.indexOf("</expected]>>") - 1)]
file("${actualFileName}").text = msg[(msg.indexOf("<<[actual>") + 10)..(msg.indexOf("</actual]>>") - 1)]
def diffCommand = isWindows ? "FC" : "diff"
def process = "${diffCommand} ${expectedFileName} ${actualFileName}".execute()
println process.getText()
process.waitFor()
if (!isAppVeyor && !isTravis) { // CI does not allow deleting files
def deleteCommand = isWindows ? "del" : "rm"
"${deleteCommand} ${expectedFileName}".execute()
"${deleteCommand} ${actualFileName}".execute()
}
} else {
println "${result.exception.getClass().getName()}: ${result.exception.getMessage()}"
}
for (StackTraceElement ste : result.exception.getStackTrace()) {
if (ste.getClassName().contains("NativeMethodAccessorImpl")) {
// Everything after this line is the internal workings of TestNG, not important for us
println "\t..."
break
}
println "\tat ${ste.getClassName()}.${ste.getMethodName()}(${ste.getFileName()}:${ste.getLineNumber()})"
}
}
}
task architectureTest(type: Test) {
useTestNG()
options.suites "src/test/resources/testng-architecture.xml"
options.useDefaultListeners = true
ignoreFailures false
maxHeapSize = "1g"
reports.html.enabled = false
reports.junitXml.enabled = false
jvmArgs "-Xss2m", "-Dfile.encoding=UTF-8"
afterTest afterTestClosure
afterSuite checkTestNgFailureClosure
testLogging {
events "passed"
}
}
architectureTest.dependsOn testClasses
architectureTest.shouldRunAfter lintMain, lintTest
task lint {
description "Runs the entire static analysis tasks for back-end."
group "Static analysis"
dependsOn lintMain, lintTest, architectureTest
}
// TEST TASKS
def numOfTestRetries = 3
def isTravis = System.getenv("TRAVIS") != null
def isAppVeyor = System.getenv("APPVEYOR") != null
test {
useTestNG()
options.useDefaultListeners = true
ignoreFailures false
maxHeapSize = "1g"
reports.html.enabled = false
reports.junitXml.enabled = false
jvmArgs "-Xss2m", "-Dfile.encoding=UTF-8"
afterTest afterTestClosure
afterSuite checkTestNgFailureClosure
testLogging {
events "passed"
}
}
task lnpTests(type: Test) {
systemProperty "random.testing.seed", new Random().nextInt() // re-run tests even if they are up-to-date
useTestNG()
options.suites "src/e2e/resources/testng-lnp.xml"
options.useDefaultListeners = true
ignoreFailures false
maxHeapSize = "1g"
reports.html.enabled = false
reports.junitXml.enabled = false
jvmArgs "-Xss2m", "-Dfile.encoding=UTF-8"
afterTest afterTestClosure
afterSuite checkTestNgFailureClosure
testLogging {
events "passed", "failed", "skipped"
}
// Display the L&P test results in the build
def outputCache = new LinkedList<String>()
beforeTest { TestDescriptor td -> outputCache.clear() } // clear output cache before the test starts
onOutput { TestDescriptor td, TestOutputEvent toe -> // put output in the cache
outputCache.add(toe.getMessage())
while (outputCache.size() > 2) outputCache.remove() // keep only last 2 result lines
}
afterTest { TestDescriptor td ->
if (outputCache.size() > 0) {
println(" L&P results for ${td.className}:")
outputCache.each { print(" > $it") }
}
}
}
task failedTests(type: Test) {
useTestNG()
options.suites "test-output/testng-failed.xml"
options.outputDirectory = file("build/reports/test-failed")
options.useDefaultListeners = true
ignoreFailures false
maxHeapSize = "1g"
reports.html.enabled = false
reports.junitXml.enabled = false
jvmArgs "-Xss2m", "-Dfile.encoding=UTF-8"
afterTest afterTestClosure
afterSuite checkTestNgFailureClosure
testLogging {
events "passed"
}
}
task componentTests {
description "Runs the full unit test suite and retries failed test up to ${numOfTestRetries} times."
group "Test"
}
task e2eTests {
description "Runs the full E2E test suite and retries failed test up to ${numOfTestRetries} times."
group "Test"
}
(1..numOfTestRetries + 1).each { id ->
def isFirstTry = id == 1
def isLastRetry = id == numOfTestRetries + 1
task "componentTestTry${id}"(type: Test) {
useTestNG()
options.suites isFirstTry ? "src/test/resources/testng-component.xml" : file("${buildDir}/reports/component-test-try-${id - 1}/testng-failed.xml")
options.outputDirectory = file("${buildDir}/reports/component-test-try-${id}")
options.useDefaultListeners = true
ignoreFailures = !isLastRetry
maxHeapSize = "1g"
reports.html.enabled = false
reports.junitXml.enabled = false
if (isTravis) {
jvmArgs "-Xss2m", "-Dfile.encoding=UTF-8", "-Djava.io.tmpdir=" + System.getenv("TRAVIS_BUILD_DIR")
} else {
jvmArgs "-Xss2m", "-Dfile.encoding=UTF-8"
}
testLogging {
events "passed"
}
if (isLastRetry) {
afterTest afterTestClosure
} else if (isFirstTry) {
afterSuite checkTestNgFailureClosure
}
onlyIf {
isFirstTry || file("${buildDir}/reports/component-test-try-${id - 1}/testng-failed.xml").exists()
}
}
componentTests.dependsOn "componentTestTry${id}"
task "e2eTestTry${id}"(type: Test) {
useTestNG()
options.suites isFirstTry ? "src/e2e/resources/testng-e2e.xml" : file("${buildDir}/reports/e2e-test-try-${id - 1}/testng-failed.xml")
options.outputDirectory = file("${buildDir}/reports/e2e-test-try-${id}")
options.useDefaultListeners = true
ignoreFailures = !isLastRetry
maxHeapSize = "1g"
reports.html.enabled = false
reports.junitXml.enabled = false
if (isTravis) {
jvmArgs "-Xss2m", "-Dfile.encoding=UTF-8", "-Djava.io.tmpdir=" + System.getenv("TRAVIS_BUILD_DIR")
} else {
jvmArgs "-Xss2m", "-Dfile.encoding=UTF-8"
}
testLogging {
events "passed"
}
if (isLastRetry) {
afterTest afterTestClosure
} else if (isFirstTry) {
afterSuite checkTestNgFailureClosure
}
finalizedBy "killFirefox${id}", "killChromedriver${id}"
onlyIf {
isFirstTry || file("${buildDir}/reports/e2e-test-try-${id - 1}/testng-failed.xml").exists()
}
}
e2eTests.dependsOn "e2eTestTry${id}"
task "killFirefox${id}"(type: Exec) {
doFirst {
if (isWindows) {
commandLine "taskkill", "/f", "/im", "firefox.exe"
} else {
commandLine "pkill", "firefox"
}
// Silence output for this task
standardOutput = new ByteArrayOutputStream()
errorOutput = standardOutput
}
ignoreExitValue = true
outputs.upToDateWhen { false }
}
task "killChromedriver${id}"(type: Exec) {
doFirst {
if (isWindows) {
commandLine "taskkill", "/f", "/im", "chromedriver.exe"
} else {
commandLine "pkill", "chromedriver"
}
// Silence output for this task
standardOutput = new ByteArrayOutputStream()
errorOutput = standardOutput
}
ignoreExitValue = true
outputs.upToDateWhen { false }
}
}
// COVERAGE TASKS
jacoco {
toolVersion = jacocoVersion
}
task jacocoMerge(type: JacocoMerge) {
destinationFile = file("${buildDir}/jacocoMerge/jacocoMerge.exec")
executionData fileTree("${buildDir}/jacoco").files
}
task jacocoReport(type: JacocoReport) {
description "Runs coverage session from available test run data."
group "Test"
sourceDirectories.from files(sourceSets.main.java.srcDirs)
classDirectories.from files(sourceSets.main.output)
executionData jacocoMerge.destinationFile
reports {
xml.enabled true
html.enabled true
}
afterEvaluate {
classDirectories.from files(classDirectories.files.collect {
fileTree(dir: it, exclude: ["**/*.jar"])
})
}
dependsOn jacocoMerge
}
// Helper methods
import groovy.xml.StreamingMarkupBuilder
import java.nio.charset.StandardCharsets
void writeXmlToPath(Node xml, String pathToWrite) {
new File(pathToWrite).withWriter(StandardCharsets.UTF_8.name()) { out ->
out << new StreamingMarkupBuilder().bind { mkp.xmlDeclaration() }
def xmlNodePrinter = new XmlNodePrinter(new PrintWriter(out))
xmlNodePrinter.preserveWhitespace = true
xmlNodePrinter.print(xml)
}
}