forked from discord-jda/JDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.gradle.kts
504 lines (409 loc) · 14.3 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
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
/*
* Copyright 2015 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//to build everything: "gradlew build"
//to build and upload everything: "gradlew release"
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import io.github.gradlenexus.publishplugin.AbstractNexusStagingRepositoryTask
import org.apache.tools.ant.filters.ReplaceTokens
import java.time.Duration
plugins {
signing
`java-library`
`maven-publish`
id("io.github.gradle-nexus.publish-plugin") version "2.0.0"
id("com.github.johnrengelman.shadow") version "8.1.1"
}
////////////////////////////////////
// //
// Project Configuration //
// //
////////////////////////////////////
val javaVersion = JavaVersion.current()
val versionObj = Version(major = "5", minor = "2", revision = "1", classifier = null)
val isGithubAction = System.getProperty("GITHUB_ACTION") != null || System.getenv("GITHUB_ACTION") != null
val isCI = System.getProperty("BUILD_NUMBER") != null // jenkins
|| System.getenv("BUILD_NUMBER") != null
|| System.getProperty("GIT_COMMIT") != null // jitpack
|| System.getenv("GIT_COMMIT") != null
|| isGithubAction // Github Actions
// Check the commit hash and version information
val commitHash: String by lazy {
val commit = System.getenv("GIT_COMMIT") ?: System.getProperty("GIT_COMMIT") ?: System.getenv("GITHUB_SHA")
// We only set the commit hash on CI builds since we don't want dirty local repos to set a wrong commit
if (isCI && commit != null)
commit.take(7)
else
"DEV"
}
val previousVersion: Version by lazy {
val file = layout.projectDirectory.file(".version").asFile
if (file.canRead())
Version.parse(file.readText().trim())
else
versionObj
}
val signingKey: String? by project
val signingKeyId: String? by project
val ossrhUser: String? by project
val ossrhPassword: String? by project
val stagingProfile: String? by project
val ossrhConfigured = ossrhUser != null && ossrhPassword != null
val canSign = signingKey != null && signingKeyId != null
val shouldPublish = canSign && ossrhConfigured && isGithubAction
// Use normal version string for new releases and commitHash for other builds
if (shouldPublish) {
project.version = "$versionObj"
} else {
project.version = "${versionObj}_$commitHash"
}
project.group = "org.redlance.thirdparty"
base {
archivesName.set("JDA")
}
java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
configure<SourceSetContainer> {
register("examples") {
java.srcDir("src/examples/java")
compileClasspath += sourceSets["main"].output
runtimeClasspath += sourceSets["main"].output
}
}
////////////////////////////////////
// //
// Dependency Configuration //
// //
////////////////////////////////////
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
/* ABI dependencies */
//Code safety
compileOnly(libs.findbugs)
compileOnly(libs.jetbrains.annotations)
//Logger
api(libs.slf4j)
//Web Connection Support
api(libs.websocket.client)
api(libs.okhttp)
//Opus library support
api(libs.opus)
//Collections Utility
api(libs.commons.collections)
//we use this only together with opus-java
// if that dependency is excluded it also doesn't need jna anymore
// since jna is a transitive runtime dependency of opus-java we don't include it explicitly as dependency
compileOnly(libs.jna)
/* Internal dependencies */
//General Utility
implementation(libs.trove4j)
implementation(libs.bundles.jackson)
//Audio crypto libraries
implementation(libs.tink)
//Sets the dependencies for the examples
configurations["examplesImplementation"].withDependencies {
addAll(configurations["api"].allDependencies)
addAll(configurations["implementation"].allDependencies)
addAll(configurations["compileOnly"].allDependencies)
}
testImplementation(libs.junit)
testImplementation(libs.reflections)
testImplementation(libs.mockito)
testImplementation(libs.assertj)
testImplementation(libs.commons.lang3)
testImplementation(libs.logback.classic)
testImplementation(libs.archunit)
}
////////////////////////////////////
// //
// Build Task Configuration //
// //
////////////////////////////////////
val jar by tasks.getting(Jar::class) {
archiveBaseName.set(project.name)
manifest.attributes(
"Implementation-Version" to project.version,
"Automatic-Module-Name" to "net.dv8tion.jda")
}
val shadowJar by tasks.getting(ShadowJar::class) {
archiveClassifier.set("withDependencies")
exclude("*.pom")
}
val sourcesForRelease by tasks.creating(Copy::class) {
from("src/main/java") {
include("**/JDAInfo.java")
val tokens = mapOf(
"versionMajor" to versionObj.major,
"versionMinor" to versionObj.minor,
"versionRevision" to versionObj.revision,
"versionClassifier" to nullableReplacement(versionObj.classifier),
"commitHash" to commitHash
)
// Allow for setting null on some strings without breaking the source
// for this, we have special tokens marked with "!@...@!" which are replaced to @...@
filter { it.replace(Regex("\"!@|@!\""), "@") }
// Then we can replace the @...@ with the respective values here
filter<ReplaceTokens>("tokens" to tokens)
}
into("build/filteredSrc")
includeEmptyDirs = false
}
val generateJavaSources by tasks.creating(SourceTask::class) {
val javaSources = sourceSets["main"].allJava.filter {
it.name != "JDAInfo.java"
}.asFileTree
source = javaSources + fileTree(sourcesForRelease.destinationDir)
dependsOn(sourcesForRelease)
}
val noOpusJar by tasks.creating(ShadowJar::class) {
dependsOn(shadowJar)
archiveClassifier.set(shadowJar.archiveClassifier.get() + "-no-opus")
configurations = shadowJar.configurations
from(sourceSets["main"].output)
exclude("natives/**") // ~2 MB
exclude("com/sun/jna/**") // ~1 MB
exclude("club/minnced/opus/util/*")
exclude("tomp2p/opuswrapper/*")
manifest.inheritFrom(jar.manifest)
}
val minimalJar by tasks.creating(ShadowJar::class) {
dependsOn(shadowJar)
minimize()
archiveClassifier.set(shadowJar.archiveClassifier.get() + "-min")
configurations = shadowJar.configurations
from(sourceSets["main"].output)
exclude("natives/**") // ~2 MB
exclude("com/sun/jna/**") // ~1 MB
exclude("com/google/crypto/tink/**") // ~2 MB
exclude("com/google/gson/**") // ~300 KB
exclude("com/google/protobuf/**") // ~2 MB
exclude("google/protobuf/**")
exclude("club/minnced/opus/util/*")
exclude("tomp2p/opuswrapper/*")
manifest.inheritFrom(jar.manifest)
}
val sourcesJar by tasks.creating(Jar::class) {
archiveClassifier.set("sources")
from("src/main/java") {
exclude("**/JDAInfo.java")
}
from(sourcesForRelease.destinationDir)
dependsOn(sourcesForRelease)
}
val javadoc by tasks.getting(Javadoc::class) {
isFailOnError = isCI
options.memberLevel = JavadocMemberLevel.PUBLIC
options.encoding = "UTF-8"
(options as? StandardJavadocDocletOptions)?.let { opt ->
opt.author()
opt.tags("incubating:a:Incubating:")
opt.links(
"https://docs.oracle.com/javase/8/docs/api/",
"https://takahikokawasaki.github.io/nv-websocket-client/")
if (JavaVersion.VERSION_1_8 < javaVersion) {
opt.addBooleanOption("html5", true) // Adds search bar
opt.addStringOption("-release", "8")
}
// Fix for https://stackoverflow.com/questions/52326318/maven-javadoc-search-redirects-to-undefined-url
if (javaVersion in JavaVersion.VERSION_11..JavaVersion.VERSION_12) {
opt.addBooleanOption("-no-module-directories", true)
}
// Java 13 changed accessibility rules.
// On versions less than Java 13, we simply ignore the errors.
// Both of these remove "no comment" warnings.
if (javaVersion >= JavaVersion.VERSION_13) {
opt.addBooleanOption("Xdoclint:all,-missing", true)
} else {
opt.addBooleanOption("Xdoclint:all,-missing,-accessibility", true)
}
opt.overview = "$projectDir/overview.html"
}
dependsOn(sourcesJar)
source = sourcesJar.source.asFileTree
exclude("MANIFEST.MF")
//### excludes ###
//jda internals
exclude("net/dv8tion/jda/internal")
//voice crypto
exclude("com/iwebpp/crypto")
}
val javadocJar by tasks.creating(Jar::class) {
dependsOn(javadoc)
archiveClassifier.set("javadoc")
from(javadoc.destinationDir)
}
tasks.withType<JavaCompile> {
options.encoding = "UTF-8"
options.isIncremental = true
val args = mutableListOf("-Xlint:deprecation", "-Xlint:unchecked")
if (javaVersion.isJava9Compatible) {
args.add("--release")
args.add("8")
}
doFirst {
options.compilerArgs = args
}
}
val compileJava by tasks.getting(JavaCompile::class) {
dependsOn(generateJavaSources)
source = generateJavaSources.source
}
val build by tasks.getting(Task::class) {
dependsOn(jar)
dependsOn(javadocJar)
dependsOn(sourcesJar)
dependsOn(shadowJar)
dependsOn(noOpusJar)
dependsOn(minimalJar)
jar.mustRunAfter(tasks.clean)
shadowJar.mustRunAfter(sourcesJar)
}
val test by tasks.getting(Test::class) {
useJUnitPlatform()
failFast = true
}
////////////////////////////////////
// //
// Publishing And Signing //
// //
////////////////////////////////////
// Generate pom file for maven central
fun MavenPom.populate() {
packaging = "jar"
name.set(project.name)
description.set("Java wrapper for the popular chat & VOIP service: Discord https://discord.com")
url.set("https://github.com/discord-jda/JDA")
scm {
url.set("https://github.com/discord-jda/JDA")
connection.set("scm:git:git://github.com/discord-jda/JDA")
developerConnection.set("scm:git:ssh:[email protected]:discord-jda/JDA")
}
licenses {
license {
name.set("The Apache Software License, Version 2.0")
url.set("http://www.apache.org/licenses/LICENSE-2.0.txt")
distribution.set("repo")
}
}
developers {
developer {
id.set("Minn")
name.set("Florian Spieß")
email.set("[email protected]")
}
developer {
id.set("DV8FromTheWorld")
name.set("Austin Keener")
email.set("[email protected]")
}
}
}
// Skip fat jar publication (See https://github.com/johnrengelman/shadow/issues/586)
components.java.withVariantsFromConfiguration(configurations.shadowRuntimeElements.get()) { skip() }
val SoftwareComponentContainer.java
get() = components.getByName<AdhocComponentWithVariants>("java")
publishing {
publications {
register<MavenPublication>("Release") {
from(components["java"])
artifactId = project.name
groupId = project.group as String
version = project.version as String
artifact(sourcesJar)
artifact(javadocJar)
pom.populate()
}
}
}
signing {
useInMemoryPgpKeys(signingKeyId, signingKey, "")
sign(publishing.publications.getByName("Release"))
isRequired = shouldPublish
}
nexusPublishing {
repositories.sonatype {
username.set(ossrhUser)
password.set(ossrhPassword)
stagingProfileId.set(stagingProfile)
}
connectTimeout.set(Duration.ofMinutes(1))
clientTimeout.set(Duration.ofMinutes(10))
transitionCheckOptions {
maxRetries.set(100)
delayBetween.set(Duration.ofSeconds(5))
}
}
////////////////////////////////////
// //
// Release Task Configuration //
// //
////////////////////////////////////
val rebuild by tasks.creating(Task::class) {
group = "build"
dependsOn(build)
dependsOn(tasks.clean)
build.mustRunAfter(tasks.clean)
}
val publishingTasks = tasks.withType<PublishToMavenRepository> {
enabled = shouldPublish
mustRunAfter(rebuild)
dependsOn(rebuild)
}
tasks.withType<AbstractNexusStagingRepositoryTask> {
enabled = shouldPublish
}
val release by tasks.creating(Task::class) {
group = "publishing"
enabled = shouldPublish
dependsOn(publishingTasks)
}
afterEvaluate {
val closeAndReleaseStagingRepositories by tasks.getting
closeAndReleaseStagingRepositories.apply {
release.dependsOn(this)
mustRunAfter(publishingTasks)
}
}
////////////////////////////////////
// //
// Helpers //
// //
////////////////////////////////////
fun nullableReplacement(string: String?): String {
return if (string == null) "null"
else "\"$string\""
}
data class Version(
val major: String,
val minor: String,
val revision: String,
val classifier: String? = null
) {
companion object {
fun parse(string: String): Version {
val (major, minor, revision) = string.substringBefore("-").split(".")
val classifier = string.substringAfter("-").takeIf { "-" in string }
return Version(major, minor, revision, classifier)
}
}
override fun toString(): String {
return "$major.$minor.$revision" + if (classifier != null) "-$classifier" else ""
}
}