-
Notifications
You must be signed in to change notification settings - Fork 34
/
build.gradle.kts
256 lines (221 loc) · 9.07 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
import java.net.Socket
import org.apache.tools.ant.taskdefs.condition.Os
// The alias call in plugins scope produces IntelliJ false error which is suppressed here.
@Suppress("DSL_SCOPE_VIOLATION")
plugins {
java
`java-test-fixtures`
alias(libs.plugins.spotless)
alias(libs.plugins.kotlin.jvm) apply false
jacoco
}
// *******************************************************************************
// Task registration and configuration
// *******************************************************************************
fun skipNonCriticalTasks(tasks: TaskContainer) {
tasks.matching { it.name == "spotlessApply" }.configureEach { enabled = false }
tasks.matching { it.name == "spotlessKotlinApply" }.configureEach { enabled = false }
tasks.matching { it.name == "javadoc" }.configureEach { enabled = false }
tasks.matching { it.name == "javadocJar" }.configureEach { enabled = false }
tasks.matching { it.name == "sourcesJar" }.configureEach { enabled = false }
tasks.matching { it.name == "distTar" }.configureEach { enabled = false }
tasks.matching { it.name == "distZip" }.configureEach { enabled = false }
tasks.matching { it.name == "javadoc" }.configureEach { enabled = false }
tasks.matching { it.name == "shadowJar" }.configureEach { enabled = false }
tasks.matching { it.name == "shadowDistZip" }.configureEach { enabled = false }
tasks.matching { it.name == "shadowDistTar" }.configureEach { enabled = false }
tasks.matching { it.name == "bootDistTar" }.configureEach { enabled = false }
tasks.matching { it.name == "bootDistZip" }.configureEach { enabled = false }
}
fun isPortActive(host: String = "localhost", port: Int): Boolean {
return try {
Socket(host, port).use { _ -> true }
} catch (e: Exception) {
false
}
}
// The build task executed at GitHub Actions. This task is used to build the project and run the
// unit tests. The task is also used to generate the Jacoco test report.
tasks.register("runBuild") {
group = "github"
description = "Build the project, run jacocoTestReport, and skip specific tasks."
dependsOn("clean", "build", "jacocoTestReport")
subprojects {
if (name == "essential-tests" || name == "extended-tests") {
tasks.named("test") { enabled = false }
}
dependsOn(tasks.named("build"))
skipNonCriticalTasks(tasks)
}
}
// The runEssentialTests task is used to run the essential tests. The task is used to check if the
// AnchorPlatform server is running before running the tests. The task also skips the non-critical
// tasks.
tasks.register("runEssentialTests") {
group = "github"
description = "Run the essential tests."
if (!isPortActive(port = 8080)) {
println("************************************************************")
println(
"ERROR: The AnchorPlatform server is not running. Please start the server before running the tests.")
throw GradleException("AnchorPlatform server is not running.")
}
dependsOn(":essential-tests:test")
subprojects {
if (name == "essential-tests") {
skipNonCriticalTasks(tasks)
}
}
}
// The printVersionName task is used to print the version name of the project. This
// is useful for CI/CD pipelines to get the version string of the project.
tasks.register("printVersionName") { println(rootProject.version.toString()) }
// The updateGitHook task is used to copy the pre-commit.sh file to the .git/hooks
// directory. This is part of the efforts to force the Java/Kotlin code to be formatted
// before committing the code.
tasks.register<Copy>("updateGitHook") {
from("scripts/pre-commit.sh") { rename { it.removeSuffix(".sh") } }
into(".git/hooks")
doLast {
if (!Os.isFamily(Os.FAMILY_WINDOWS)) {
project.exec { commandLine("chmod", "+x", ".git/hooks/pre-commit") }
}
}
}
tasks { build { dependsOn("updateGitHook") } }
// *******************************************************************************
// Common configurations
// *******************************************************************************
subprojects {
apply(plugin = "java")
apply(plugin = "com.diffplug.spotless")
apply(plugin = "jacoco")
apply(plugin = "java-test-fixtures")
repositories {
mavenLocal()
mavenCentral()
maven { url = uri("https://packages.confluent.io/maven") }
maven { url = uri("https://repository.mulesoft.org/nexus/content/repositories/public/") }
maven { url = uri("https://jitpack.io") }
}
/** Specifies JDK-17 */
java { toolchain { languageVersion.set(JavaLanguageVersion.of(17)) } }
spotless {
java {
importOrder("java", "javax", "org.stellar")
removeUnusedImports()
googleJavaFormat()
}
kotlin { ktfmt("0.42").googleStyle() }
tasks.jacocoTestReport {
dependsOn(tasks.test) // tests are required to run before generating the report
reports {
xml.required.set(true)
csv.required.set(false)
html.required.set(false)
}
}
}
dependencies {
// `rootProject` is required here if we want to use the libs object in the root project.
implementation(rootProject.libs.spotbugs.annotations)
implementation(rootProject.libs.google.guava)
implementation(rootProject.libs.log4j.template.json)
// The common dependencies are declared here because we would like to have a uniform unit
// testing across all subprojects.
//
// We need to use the dependency string because the VERSION_CATEGORY feature isn't supported in
// subproject task as of today.
//
testImplementation(rootProject.libs.bundles.junit)
testImplementation(rootProject.libs.jsonassert)
testFixturesImplementation(rootProject.libs.bundles.junit)
testFixturesImplementation(rootProject.libs.jsonassert)
testAnnotationProcessor(rootProject.libs.lombok)
}
/**
* This is to fix the Windows default cp-1252 character encoding that may potentially cause
* compilation error
*/
tasks {
compileJava {
options.encoding = "UTF-8"
options.compilerArgs.add("-parameters")
/** Enforces google-java-format at Java compilation. */
dependsOn("spotlessApply")
}
compileTestJava { options.encoding = "UTF-8" }
javadoc { options.encoding = "UTF-8" }
test {
useJUnitPlatform()
systemProperty(
"junit.jupiter.testclass.order.default",
"org.junit.jupiter.api.ClassOrderer\$OrderAnnotation")
exclude("**/AnchorPlatformCustodyEnd2EndTest**")
exclude("**/AnchorPlatformCustodyApiRpcEnd2EndTest**")
testLogging {
events("SKIPPED", "FAILED")
showExceptions = true
showStandardStreams = true
exceptionFormat = org.gradle.api.tasks.testing.logging.TestExceptionFormat.FULL
}
}
processResources {
doFirst {
val existingFile = file("$buildDir/resources/main/metadata.properties")
existingFile.delete()
}
filesMatching("**/metadata.properties") {
// This is to replace the %APP_VERSION_TOKEN% in the metadata.properties file.
filter { line -> line.replace("%APP_VERSION_TOKEN%", rootProject.version.toString()) }
}
}
}
configurations {
all {
exclude(group = "ch.qos.logback", module = "logback-classic")
exclude(group = "org.apache.logging.log4j", module = "log4j-to-slf4j")
exclude(group = "org.slf4j", module = "slf4j-log4j12")
exclude(group = "org.slf4j", module = "slf4j-simple")
exclude(group = "commons-logging", module = "commons-logging")
}
}
tasks.withType<Jar> {
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
}
}
allprojects {
group = "org.stellar.anchor-sdk"
version = "3.0.0"
tasks.jar {
manifest {
attributes(
mapOf(
"Implementation-Title" to project.name, "Implementation-Version" to project.version))
}
}
}
// *******************************************************************************
// print the gradle script usages
tasks.register("printUsage") {
doLast {
val green = "\u001B[32m"
val bold = "\u001B[1m"
// ANSI escape code to reset
val reset = "\u001B[0m"
println(
"""
${green}${bold}Usage: ./gradlew <task>${reset}
Available custom tasks:
- ${bold}printVersionName${reset}: Prints the version of the project.
- ${bold}updateGitHook${reset}: Updates the git hook to format the code before committing.
- ${bold}startAllServers${reset}: Starts all the servers based on the `default` test configuration.
- ${bold}startServersWithTestProfile${reset}: Starts the servers based on the test configuration specified by the TEST_PROFILE_NAME environment variable.
- ${bold}dockerComposeStart${reset}: Runs docker-compose up to start Postgres, Kafka, etc.
- ${bold}dockerComposeStop${reset}: Runs docker-compose down to stop Postgres, Kafka, etc.
- ${bold}anchorTest${reset}: Runs stellar anchor tests. Set `TEST_HOME_DOMAIN` and `TEST_SEPS` environment variables to customize the tests.
"""
.trimIndent())
}
}
defaultTasks("printUsage")