-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.gradle.kts
319 lines (279 loc) · 10.7 KB
/
build.gradle.kts
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
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
import com.github.benmanes.gradle.versions.updates.DependencyUpdatesTask
plugins {
war
jacoco
id("io.freefair.lombok")
id("com.github.ben-manes.versions")
}
repositories {
// Use Maven Central for resolving dependencies.
mavenCentral()
}
val antJUnit: Configuration by configurations.creating
dependencies {
implementation(libs.jandex)
implementation(libs.jackson)
implementation(libs.weld)
testImplementation(libs.junit)
testImplementation(libs.hamcrest)
testImplementation(libs.tomcat)
testImplementation(libs.jasper)
testImplementation(libs.selenium)
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
// This dependency is used internally, and not exposed to consumers on their
// own compile classpath.
providedCompile(libs.servlet)
antJUnit(libs.antJunit)
}
jacoco {
toolVersion = "0.8.11"
}
// Set inputs and outputs for the frontend tasks. This enables Gradle to cache
// the results instead of executing these tasks unconditionally on every build.
//
// The vite.config.js file in src/main/frontend specifies:
//
// - The output directory, build/webapp
// - The default test results directory, build/test-results/test-frontend
// - The coverage directory, build/reports/frontend/coverage
//
// The vite.config.ci-browser.js file, used by frontendTest, also specifies
// the build/test-results/test-frontend-browser directory.
val frontendDir = project.layout.projectDirectory.dir("src/main/frontend")
val frontendSources = fileTree(frontendDir) {
exclude("node_modules", ".*", "*.md")
}
val frontendOutputDir = project.layout.buildDirectory.dir("webapp").get()
fun frontendCmd(task: Exec, vararg commands: String) {
task.workingDir(frontendDir)
task.commandLine("pnpm", *commands)
}
val frontendInstall = tasks.register<Exec>("frontendInstall") {
description = "Install src/main/frontend npm packages"
inputs.files(frontendDir.files("package.json", "pnpm-lock.yaml"))
outputs.upToDateWhen { true }
frontendCmd(this, "install")
}
val frontendBuild = tasks.register<Exec>("frontendBuild") {
description = "Build src/main/frontend JavaScript into build/webapp"
dependsOn(frontendInstall)
inputs.files(frontendSources.filter { !it.name.endsWith(".test.js") })
outputs.dir(frontendOutputDir)
frontendCmd(this, "build")
}
val frontendTest = tasks.register<Exec>("frontendTest") {
description = "Test frontend JavaScript in src/main/frontend"
inputs.files(frontendSources)
frontendCmd(this, "test-ci")
val resultsDir = java.testResultsDir.get()
outputs.dirs(
resultsDir.dir("test-frontend"),
resultsDir.dir("test-frontend-browser")
)
}
// Configure the "war" task generated by the Gradle War plugin to depend upon
// the frontend build and to include its output files from build/webapp. This is
// in addition to the files within src/main/webapp, which the task includes by
// default.
//
// - https://docs.gradle.org/current/userguide/war_plugin.html
val war = tasks.named("war")
tasks.war {
dependsOn(frontendBuild)
from(frontendOutputDir)
}
// The small/medium/large test schema is implemented via JUnit5 composite tags
// and custom Test tasks. See:
//
// - https://mike-bland.com/2023/08/31/the-test-pyramid-and-the-chain-reaction.html
// - https://junit.org/junit5/docs/current/user-guide/#writing-tests-meta-annotations
// - https://docs.gradle.org/current/userguide/java_testing.html#test_grouping
//
// This doesn't use the incubating JVM Test Suite plugin because it doesn't
// support includeTags():
//
// - https://docs.gradle.org/current/userguide/jvm_test_suite_plugin.html#jvm_test_suite_plugin
val testClasses = tasks.named("testClasses")
val webappInputs = project.layout.projectDirectory.dir("src/main/webapp")
val setCommonTestOptions = { testTask: Test ->
testTask.reports { junitXml.apply { isOutputPerTestCase = true } }
testTask.testLogging {
showStandardStreams = project.hasProperty("testoutput")
}
}
val setLargerTestOptions = { testTask: Test ->
testTask.group = "verification"
testTask.dependsOn(testClasses)
setCommonTestOptions(testTask)
// Based on advice from:
// - https://docs.gradle.org/current/userguide/upgrading_version_8.html#test_task_default_classpath
@Suppress("UnstableApiUsage")
val test by testing.suites.existing(JvmTestSuite::class)
testTask.testClassesDirs = files(test.map {
@Suppress("UnstableApiUsage")
it.sources.output.classesDirs
})
testTask.classpath = files(test.map {
@Suppress("UnstableApiUsage")
it.sources.runtimeClasspath
})
}
val smallTests = tasks.named<Test>("test") {
description = "Runs small unit tests annotated with @SmallTest."
useJUnitPlatform { includeTags("small") }
setCommonTestOptions(this)
}
val mediumCoverageTests = tasks.register<Test>("test-medium-coverage") {
description = "Runs medium integration tests annotated with " +
"@MediumCoverageTest."
setLargerTestOptions(this)
dependsOn(frontendBuild)
inputs.dir(webappInputs)
useJUnitPlatform { includeTags("medium & coverage") }
shouldRunAfter(smallTests, frontendTest)
}
val mediumTests = tasks.register<Test>("test-medium") {
description = "Runs medium integration tests annotated with @MediumTest."
setLargerTestOptions(this)
dependsOn(frontendBuild)
inputs.dir(webappInputs)
useJUnitPlatform { includeTags("medium & !coverage") }
shouldRunAfter(smallTests, mediumCoverageTests, frontendTest)
extensions.configure(JacocoTaskExtension::class) {
isEnabled = false
}
}
val largeTests = tasks.register<Test>("test-large") {
description = "Runs large system tests annotated with @LargeTest."
setLargerTestOptions(this)
dependsOn(war)
useJUnitPlatform { includeTags("large") }
shouldRunAfter(mediumCoverageTests, mediumTests, frontendTest)
extensions.configure(JacocoTaskExtension::class) {
isEnabled = false
}
}
val allTestSizes = arrayOf(
smallTests, mediumCoverageTests, mediumTests, largeTests
)
val allTests = tasks.register<Task>("test-all") {
description = "Runs the frontend, small, medium, and large test suites, " +
"in that order."
group = "verification"
dependsOn(frontendTest, allTestSizes)
}
tasks.named("check") {
dependsOn(allTests)
}
// Used to emit paths of JUnit and coverage report files relative to the root
// directory of the project repository.
val relativeToRootDir = fun(absPath: java.nio.file.Path): java.nio.file.Path {
return rootDir.toPath().relativize(absPath)
}
// JUnit emits a single test result file containing a single <testsuite>
// element for each test class. The dorny/test-reporter GitHub action from
// .github/workflows/publish-test-results.yaml requires a single, consolidated
// file containing a <testsuites> element.
//
// The "merge-test-reports" task, defined below, uses this mergeTestReports
// function to create the consolidated <testsuites> file. The implementation
// is based on:
//
// - https://blog.lehnerpat.com/post/2018-09-10/merging-per-suite-junit-reports-into-single-file-with-gradle-kotlin/
// - https://docs.gradle.org/current/userguide/ant.html
val mergeTestReports = fun(resultsDir: File) {
val taskName = resultsDir.name
// The `frontendTest` output is already merged. Trying to merge it again
// results in an empty file, so skip it.
if (taskName.startsWith("test-frontend")) return
val reportTaskName = "merged-report-$taskName"
val aggregatorClass = "org.apache.tools.ant.taskdefs.optional." +
"junit.XMLResultAggregator"
ant.withGroovyBuilder {
"taskdef"(
"name" to reportTaskName,
"classname" to aggregatorClass,
"classpath" to antJUnit.asPath
)
reportTaskName("todir" to resultsDir) {
"fileset"(
"dir" to resultsDir,
"includes" to "TEST-*.xml")
}
}
logger.quiet("merged test reports: " +
relativeToRootDir(resultsDir.toPath()) +
"/TESTS-TestSuites.xml")
}
task("merge-test-reports") {
description = "Merges all JUnit XML results files for each test size into" +
" a single file."
group = "verification"
shouldRunAfter(allTestSizes)
doLast {
val resultsDir = java.testResultsDir.asFile.get()
if (resultsDir.exists()) {
resultsDir.listFiles().filter{ d: File -> d.isDirectory }.forEach {
dir: File -> mergeTestReports(dir)
}
}
}
}
// Emits the path to a JaCoCo coverage report file if the type of report
// (html, xml, or csv) is required. A report is "required" if the task's
// "report {}" configuration block sets, for example, `html.required = true`.
val emitReportLocation = fun(report: Report) {
if (report.required.get()) {
val location = report.outputLocation.get().asFile.toPath()
logger.quiet("coverage report: " + relativeToRootDir(location))
}
}
// Adds custom configuration to the jacocoTestReport task generated by the
// Gradle JaCoCo plugin.
tasks.named<JacocoReport>("jacocoTestReport") {
shouldRunAfter(smallTests, mediumCoverageTests)
executionData(mediumCoverageTests.get())
doLast {
reports.forEach { r -> emitReportLocation(r) }
}
}
// Generates an XML file suitable to upload to Coveralls.io from
// .github/workflows/run-tests.yaml:
//
// - strcalc/build/reports/jacoco/jacocoXmlTestReport/jacocoXmlTestReport.xml
tasks.register<JacocoReport>("jacocoXmlTestReport") {
shouldRunAfter(smallTests, mediumCoverageTests)
executionData(smallTests.get(), mediumCoverageTests.get())
sourceSets(sourceSets.main.get())
reports {
html.required = false
xml.required = true
}
doLast {
reports.forEach { r -> emitReportLocation(r) }
}
}
// Utility function used by the ben-manes/gradle-versions-plugin tasks,
// configured below. See:
//
// - https://github.com/ben-manes/gradle-versions-plugin#rejectversionsif-and-componentselection
fun isNonStable(version: String): Boolean {
val stableKeyword = listOf("RELEASE", "FINAL", "GA").any {
version.uppercase().contains(it)
}
val regex = "^[0-9,.v-]+(-r)?$".toRegex()
val isStable = stableKeyword || regex.matches(version)
return isStable.not()
}
tasks.withType<DependencyUpdatesTask> {
revision = "release"
gradleReleaseChannel = "current"
rejectVersionIf {
isNonStable(candidate.version) && !isNonStable(currentVersion)
}
}