-
Notifications
You must be signed in to change notification settings - Fork 13
/
build.gradle.kts
347 lines (300 loc) · 9.72 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
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
import com.adarshr.gradle.testlogger.theme.ThemeType
import io.gitlab.arturbosch.detekt.Detekt
import io.gitlab.arturbosch.detekt.extensions.DetektExtension.Companion.DEFAULT_SRC_DIR_KOTLIN
import io.gitlab.arturbosch.detekt.extensions.DetektExtension.Companion.DEFAULT_TEST_SRC_DIR_KOTLIN
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
java
signing
kotlin("jvm") version "1.9.23"
id("com.adarshr.test-logger") version "3.2.0"
id("nebula.release") version "17.1.0"
id("maven-publish")
id("io.github.gradle-nexus.publish-plugin") version "1.3.0"
id("io.gitlab.arturbosch.detekt") version "1.23.4"
id("org.gradle.test-retry") version "1.5.2"
}
repositories {
mavenCentral()
}
dependencies {
implementation(kotlin("stdlib-jdk8"))
implementation(kotlin("reflect"))
api("com.google.code.gson:gson:2.10")
api("org.apache.httpcomponents:httpclient:4.5.13")
api("io.prometheus:prometheus-metrics-core:1.1.0")
testImplementation(kotlin("test"))
testImplementation("org.junit.jupiter:junit-jupiter:5.9.1")
testImplementation("org.reflections:reflections:0.10.2")
testImplementation("uk.co.jemos.podam:podam:7.2.11.RELEASE")
detektPlugins("io.gitlab.arturbosch.detekt:detekt-formatting:1.23.4")
}
configure<JavaPluginConvention> {
sourceCompatibility = JavaVersion.VERSION_11
}
java {
withSourcesJar()
withJavadocJar()
}
tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "11"
}
tasks.withType<Test> {
useJUnitPlatform()
testlogger {
showFullStackTraces = true
showStandardStreams = true
showPassedStandardStreams = false
showSkippedStandardStreams = false
showFailedStandardStreams = true
slowThreshold = Consts.SLOW_TESTS_LOGGING_THRESHOLD_MS
theme = ThemeType.STANDARD
}
retry {
maxRetries.set(Consts.MAX_TEST_RETRIES_COUNT)
}
}
tasks.withType<Detekt>().configureEach {
jvmTarget = "11"
}
val settingsProvider = SettingsProvider()
tasks {
// All checks were already made by workflow "On pull request" => no checks here
if (gradle.startParameter.taskNames.contains("final")) {
named("build").get().apply {
dependsOn.removeIf { it == "check" }
}
}
afterEvaluate {
// Publish artifacts to Maven Central before pushing new git tag to repo
named("release").get().apply {
dependsOn(named("publishToSonatype").get())
}
named("closeAndReleaseStagingRepository").get().apply {
dependsOn(named("final").get())
}
}
}
tasks.withType<Sign> {
doFirst {
settingsProvider.validateGPGSecrets()
}
dependsOn(tasks.getByName("build"))
}
tasks.withType<PublishToMavenRepository> {
doFirst {
settingsProvider.validateOssrhCredentials()
}
}
tasks.register(Tasks.PRINT_FINAL_RELEASE_NOTE_TASK_NAME) {
doLast {
printFinalReleaseNote(
groupId = PublicationSettings.GROUP_ID,
artifactId = PublicationSettings.ARTIFACT_ID,
sanitizedVersion = project.sanitizeVersion()
)
}
dependsOn(tasks.getByName("final"))
}
tasks.register(Tasks.PRINT_DEV_SNAPSHOT_RELEASE_NOTE_TASK_NAME) {
doLast {
printDevSnapshotReleaseNote(
groupId = PublicationSettings.GROUP_ID,
artifactId = PublicationSettings.ARTIFACT_ID,
sanitizedVersion = project.sanitizeVersion()
)
}
dependsOn(tasks.getByName("devSnapshot"))
}
detekt {
allRules = false
basePath = "$projectDir"
buildUponDefaultConfig = true
config = files("$projectDir/config/detekt.yml")
input = files(
"build.gradle.kts",
DEFAULT_SRC_DIR_KOTLIN,
DEFAULT_TEST_SRC_DIR_KOTLIN
)
parallel = true
reports {
html.enabled = true
sarif.enabled = true
txt.enabled = false
xml.enabled = false
}
}
publishing {
publications {
create<MavenPublication>("mavenJava") {
from(components["java"])
groupId = PublicationSettings.GROUP_ID
artifactId = PublicationSettings.ARTIFACT_ID
version = project.sanitizeVersion()
versionMapping {
usage("java-api") {
fromResolutionOf("runtimeClasspath")
}
usage("java-runtime") {
fromResolutionResult()
}
}
pom {
name.set(PublicationSettings.POM_NAME)
description.set(PublicationSettings.POM_DESCRIPTION)
url.set(PublicationSettings.POM_URL)
licenses {
license {
name.set(PublicationSettings.LICENSE_NAME)
url.set(PublicationSettings.LICENSE_URL)
}
}
developers {
developer {
id.set(PublicationSettings.DEVELOPER_ID)
name.set(PublicationSettings.DEVELOPER_NAME)
email.set(PublicationSettings.DEVELOPER_EMAIL)
}
}
scm {
connection.set(PublicationSettings.SCM_CONNECTION)
developerConnection.set(PublicationSettings.SCM_CONNECTION)
url.set(PublicationSettings.SCM_URL)
}
}
}
}
}
signing {
useInMemoryPgpKeys(settingsProvider.gpgSigningKey, settingsProvider.gpgSigningPassword)
sign(publishing.publications["mavenJava"])
}
nexusPublishing {
repositories {
sonatype {
useStaging.set(!project.isSnapshotVersion())
packageGroup.set(PublicationSettings.STAGING_PACKAGE_GROUP)
stagingProfileId.set(PublicationSettings.STAGING_PROFILE_ID)
username.set(settingsProvider.ossrhUsername)
password.set(settingsProvider.ossrhPassword)
}
}
}
// We want to change SNAPSHOT versions format from:
// <major>.<minor>.<patch>-dev.#+<branchname>.<hash> (local branch)
// <major>.<minor>.<patch>-dev.#+<hash> (github pull request)
// to:
// <major>.<minor>.<patch>-dev+<branchname>-SNAPSHOT
fun Project.sanitizeVersion(): String {
val version = version.toString()
return if (project.isSnapshotVersion()) {
val githubHeadRef = settingsProvider.githubHeadRef
if (githubHeadRef != null) {
// github pull request
version
.replace(Regex("-dev\\.\\d+\\+[a-f0-9]+$"), "-dev+$githubHeadRef-SNAPSHOT")
} else {
// local branch
version
.replace(Regex("-dev\\.\\d+\\+"), "-dev+")
.replace(Regex("\\.[a-f0-9]+$"), "-SNAPSHOT")
}
} else {
version
}
}
fun Project.isSnapshotVersion() = version.toString().contains("-dev.")
fun printFinalReleaseNote(groupId: String, artifactId: String, sanitizedVersion: String) {
println()
println("========================================================")
println()
println("New RELEASE artifact version were published:")
println(" groupId: $groupId")
println(" artifactId: $artifactId")
println(" version: $sanitizedVersion")
println()
println("Discover on Maven Central:")
println(" https://repo1.maven.org/maven2/${groupId.replace('.', '/')}/$artifactId/")
println()
println("Edit or delete artifacts on OSS Nexus Repository Manager:")
println(" https://oss.sonatype.org/#nexus-search;gav~$groupId~~~~")
println()
println("Control staging repositories on OSS Nexus Repository Manager:")
println(" https://oss.sonatype.org/#stagingRepositories")
println()
println("========================================================")
println()
}
fun printDevSnapshotReleaseNote(groupId: String, artifactId: String, sanitizedVersion: String) {
println()
println("========================================================")
println()
println("New developer SNAPSHOT artifact version were published:")
println(" groupId: $groupId")
println(" artifactId: $artifactId")
println(" version: $sanitizedVersion")
println()
println("Discover on Maven Central:")
println(" https://oss.sonatype.org/content/groups/public/${groupId.replace('.', '/')}/$artifactId/")
println()
println("Edit or delete artifacts on OSS Nexus Repository Manager:")
println(" https://oss.sonatype.org/#nexus-search;gav~$groupId~~~~")
println()
println("========================================================")
println()
}
class SettingsProvider {
val gpgSigningKey: String?
get() = System.getenv(GPG_SIGNING_KEY_PROPERTY)
val gpgSigningPassword: String?
get() = System.getenv(GPG_SIGNING_PASSWORD_PROPERTY)
val ossrhUsername: String?
get() = System.getenv(OSSRH_USERNAME_PROPERTY)
val ossrhPassword: String?
get() = System.getenv(OSSRH_PASSWORD_PROPERTY)
val githubHeadRef: String?
get() = System.getenv(GITHUB_HEAD_REF_PROPERTY)
fun validateGPGSecrets() = require(
value = !gpgSigningKey.isNullOrBlank() && !gpgSigningPassword.isNullOrBlank(),
lazyMessage = {
"Both $GPG_SIGNING_KEY_PROPERTY and $GPG_SIGNING_PASSWORD_PROPERTY environment variables must not be empty"
}
)
fun validateOssrhCredentials() = require(
value = !ossrhUsername.isNullOrBlank() && !ossrhPassword.isNullOrBlank(),
lazyMessage = {
"Both $OSSRH_USERNAME_PROPERTY and $OSSRH_PASSWORD_PROPERTY environment variables must not be empty"
}
)
companion object {
private const val GPG_SIGNING_KEY_PROPERTY = "GPG_SIGNING_KEY"
private const val GPG_SIGNING_PASSWORD_PROPERTY = "GPG_SIGNING_PASSWORD"
private const val OSSRH_USERNAME_PROPERTY = "OSSRH_USERNAME"
private const val OSSRH_PASSWORD_PROPERTY = "OSSRH_PASSWORD"
private const val GITHUB_HEAD_REF_PROPERTY = "GITHUB_HEAD_REF"
}
}
object PublicationSettings {
const val GROUP_ID = "com.ecwid.apiclient"
const val ARTIFACT_ID = "api-client"
const val POM_NAME = "Ecwid Rest API wrapper"
const val POM_DESCRIPTION = "Ecwid Rest API wrapper"
const val POM_URL = "https://github.com/Ecwid/ecwid-java-api-client"
const val DEVELOPER_ID = "opensource-bot"
const val DEVELOPER_NAME = "Ecwid Opensource Bot"
const val DEVELOPER_EMAIL = "opensource-bot@ecwid.com"
const val LICENSE_NAME = "The Apache License, Version 2.0"
const val LICENSE_URL = "https://www.apache.org/licenses/LICENSE-2.0.txt"
const val SCM_CONNECTION = "scm:git:git@github.com:Ecwid/ecwid-java-api-client.git"
const val SCM_URL = "https://github.com/Ecwid/ecwid-java-api-client.git"
const val STAGING_PACKAGE_GROUP = "com.ecwid"
const val STAGING_PROFILE_ID = "42242535548c99"
}
object Consts {
const val SLOW_TESTS_LOGGING_THRESHOLD_MS = 30_000L
const val MAX_TEST_RETRIES_COUNT = 3
}
object Tasks {
const val PRINT_FINAL_RELEASE_NOTE_TASK_NAME = "printFinalReleaseNote"
const val PRINT_DEV_SNAPSHOT_RELEASE_NOTE_TASK_NAME = "printDevSnapshotReleaseNote"
}