forked from discord-jda/JDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.gradle.kts
493 lines (407 loc) · 14.9 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
/*
* 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 Build_gradle.Pom
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import de.marcphilipp.gradle.nexus.InitializeNexusStagingRepository
import de.marcphilipp.gradle.nexus.NexusPublishExtension
import io.codearte.gradle.nexus.BaseStagingTask
import io.codearte.gradle.nexus.NexusStagingExtension
import org.apache.tools.ant.filters.ReplaceTokens
import java.time.Duration
// Don't remove this, its needed for reasons....
typealias Pom = org.gradle.api.publish.maven.MavenPom
plugins {
signing
`java-library`
`maven-publish`
id("io.codearte.nexus-staging") version "0.30.0"
id("de.marcphilipp.nexus-publish") version "0.4.0"
id("com.github.johnrengelman.shadow") version "7.1.2"
}
val javaVersion = JavaVersion.current()
val versionObj = Version(major = "5", minor = "0", revision = "0", classifier = "alpha.17")
val isCI = System.getProperty("BUILD_NUMBER") != null // jenkins
|| System.getenv("BUILD_NUMBER") != null
|| System.getProperty("GIT_COMMIT") != null // jitpack
|| System.getenv("GIT_COMMIT") != null
|| System.getProperty("GITHUB_ACTIONS") != null // Github Actions
|| System.getenv("GITHUB_ACTIONS") != null
// Check the commit hash and version information
val commitHash: String by lazy {
val commit = System.getenv("GIT_COMMIT") ?: System.getProperty("GIT_COMMIT")
// 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.substring(0, 7)
else
"DEV"
}
val previousVersion: Version by lazy {
val file = File(".version")
if (file.canRead())
Version.parse(file.readText().trim())
else
versionObj
}
val isNewVersion = previousVersion != versionObj
// Use normal version string for new releases and commitHash for other builds
project.version = "$versionObj" + if (isNewVersion) "" else "_$commitHash"
project.group = "net.dv8tion"
val archivesBaseName = "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
}
}
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
/* ABI dependencies */
//Code safety
api("com.google.code.findbugs:jsr305:3.0.2")
api("org.jetbrains:annotations:23.0.0")
//Logger
api("org.slf4j:slf4j-api:1.7.36")
//Web Connection Support
api("com.neovisionaries:nv-websocket-client:2.14")
api("com.squareup.okhttp3:okhttp:4.9.3")
//Opus library support
api("club.minnced:opus-java:1.1.1")
//Collections Utility
api("org.apache.commons:commons-collections4:4.4")
//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("net.java.dev.jna:jna:4.4.0")
/* Internal dependencies */
//General Utility
implementation("net.sf.trove4j:trove4j:3.0.3")
// Match the minor version of lavaplayers jackson dependency
implementation("com.fasterxml.jackson.core:jackson-core:2.13.2")
implementation("com.fasterxml.jackson.core:jackson-databind:2.13.2.2")
//Sets the dependencies for the examples
configurations["examplesImplementation"].withDependencies {
addAll(configurations["api"].allDependencies)
addAll(configurations["implementation"].allDependencies)
}
testImplementation("org.junit.jupiter:junit-jupiter:5.8.2")
}
val compileJava: JavaCompile by tasks
val shadowJar: ShadowJar by tasks
val javadoc: Javadoc by tasks
val jar: Jar by tasks
val build: Task by tasks
val clean: Task by tasks
val test: Test by tasks
val check: Task by tasks
shadowJar.archiveClassifier.set("withDependencies")
fun nullable(string: String?): String {
return if (string == null) "null"
else "\"$string\""
}
val sourcesForRelease = task<Copy>("sourcesForRelease") {
from("src/main/java") {
include("**/JDAInfo.java")
val tokens = mapOf(
"versionMajor" to versionObj.major,
"versionMinor" to versionObj.minor,
"versionRevision" to versionObj.revision,
"versionClassifier" to nullable(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>(mapOf("tokens" to tokens))
}
into("build/filteredSrc")
includeEmptyDirs = false
}
val generateJavaSources = task<SourceTask>("generateJavaSources") {
val javaSources = sourceSets["main"].allJava.filter {
it.name != "JDAInfo.java"
}.asFileTree
source = javaSources + fileTree(sourcesForRelease.destinationDir)
dependsOn(sourcesForRelease)
}
val noOpusJar = task<ShadowJar>("noOpusJar") {
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 = task<ShadowJar>("minimalJar") {
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("club/minnced/opus/util/*")
exclude("tomp2p/opuswrapper/*")
manifest.inheritFrom(jar.manifest)
}
val sourcesJar = task<Jar>("sourcesJar") {
archiveClassifier.set("sources")
from("src/main/java") {
exclude("**/JDAInfo.java")
}
from(sourcesForRelease.destinationDir)
dependsOn(sourcesForRelease)
}
val javadocJar = task<Jar>("javadocJar") {
dependsOn(javadoc)
archiveClassifier.set("javadoc")
from(javadoc.destinationDir)
}
tasks.withType<ShadowJar> {
exclude("*.pom")
}
tasks.withType<JavaCompile> {
val arguments = mutableListOf("-Xlint:deprecation", "-Xlint:unchecked")
options.encoding = "UTF-8"
options.isIncremental = true
if (javaVersion.isJava9Compatible) doFirst {
arguments += "--release"
arguments += "8"
}
doFirst {
options.compilerArgs = arguments
}
}
compileJava.apply {
source = generateJavaSources.source
dependsOn(generateJavaSources)
}
jar.apply {
archiveBaseName.set(project.name)
manifest.attributes(mapOf(
"Implementation-Version" to project.version,
"Automatic-Module-Name" to "net.dv8tion.jda"))
}
javadoc.apply {
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)
}
}
dependsOn(sourcesJar)
source = sourcesJar.source.asFileTree
exclude("MANIFEST.MF")
//### excludes ###
//jda internals
exclude("net/dv8tion/jda/internal")
//voice crypto
exclude("com/iwebpp/crypto")
}
build.apply {
dependsOn(jar)
dependsOn(javadocJar)
dependsOn(sourcesJar)
dependsOn(shadowJar)
dependsOn(noOpusJar)
dependsOn(minimalJar)
jar.mustRunAfter(clean)
shadowJar.mustRunAfter(sourcesJar)
}
test.apply {
useJUnitPlatform()
failFast = true
}
fun getProjectProperty(name: String) = project.properties[name] as? String
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 = if ("-" in string) string.substringAfter("-") else null
return Version(major, minor, revision, classifier)
}
}
override fun equals(other: Any?): Boolean {
if (other === this) return true
if (other !is Version) return false
return major == other.major
&& minor == other.minor
&& revision == other.revision
&& classifier == other.classifier
}
override fun toString(): String {
return "$major.$minor.$revision" + if (classifier != null) "-$classifier" else ""
}
}
////////////////////////////////////////
////////////////////////////////////////
//// ////
//// Publishing And Signing ////
//// ////
////////////////////////////////////////
////////////////////////////////////////
// Generate pom file for maven central
fun generatePom(pom: Pom) {
pom.packaging = "jar"
pom.name.set(project.name)
pom.description.set("Java wrapper for the popular chat & VOIP service: Discord https://discord.com")
pom.url.set("https://github.com/DV8FromTheWorld/JDA")
pom.scm {
url.set("https://github.com/DV8FromTheWorld/JDA")
connection.set("scm:git:git://github.com/DV8FromTheWorld/JDA")
developerConnection.set("scm:git:ssh:[email protected]:DV8FromTheWorld/JDA")
}
pom.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")
}
}
pom.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]")
}
}
}
// Publish
// 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("java") as AdhocComponentWithVariants
publishing {
publications {
register("Release", MavenPublication::class) {
from(components["java"])
artifactId = project.name
groupId = project.group as String
version = project.version as String
artifact(sourcesJar)
artifact(javadocJar)
generatePom(pom)
}
}
}
// Turn off sign tasks if we don't have a key
val canSign = getProjectProperty("signing.keyId") != null
if (canSign) {
signing {
sign(publishing.publications.getByName("Release"))
}
}
// Staging and Promotion
configure<NexusStagingExtension> {
username = getProjectProperty("ossrhUser") ?: ""
password = getProjectProperty("ossrhPassword") ?: ""
stagingProfileId = getProjectProperty("stagingProfileId") ?: ""
}
configure<NexusPublishExtension> {
nexusPublishing {
repositories.sonatype {
username.set(getProjectProperty("ossrhUser") ?: "")
password.set(getProjectProperty("ossrhPassword") ?: "")
stagingProfileId.set(getProjectProperty("stagingProfileId") ?: "")
}
// Sonatype is very slow :)
connectTimeout.set(Duration.ofMinutes(1))
clientTimeout.set(Duration.ofMinutes(10))
}
}
// This links the close/release tasks to the right repository (from the publication above)
val ossrhConfigured = getProjectProperty("ossrhUser") != null
val shouldPublish = isNewVersion && canSign && ossrhConfigured
// Turn off the staging tasks if we don't want to publish
tasks.withType<InitializeNexusStagingRepository> {
enabled = shouldPublish
}
tasks.withType<BaseStagingTask> {
enabled = shouldPublish
// We give each step an hour because it takes very long sometimes ...
numberOfRetries = 30 // 30 tries
delayBetweenRetriesInMillis = 2 * 60 * 1000 // 2 minutes
}
// Getting staging profile is fine though
tasks.getByName("getStagingProfile").enabled = ossrhConfigured
tasks.create("release") {
// Only close repository after release is published
val closeRepository by tasks
closeRepository.mustRunAfter(tasks.withType<PublishToMavenRepository>())
dependsOn(tasks.withType<PublishToMavenRepository>())
// Closes the sonatype repository and publishes to maven central
val closeAndReleaseRepository: Task by tasks
dependsOn(closeAndReleaseRepository)
// Builds all jars for publications
dependsOn(build)
enabled = shouldPublish
doLast { // Only runs when shouldPublish = true
println("Saving version $versionObj to .version")
val file = File(".version")
file.createNewFile()
file.writeText(versionObj.toString())
}
}
tasks.withType<PublishToMavenRepository> {
enabled = shouldPublish
}
// Gradle stop complaining please
tasks.withType<Copy> {
duplicatesStrategy = DuplicatesStrategy.INCLUDE
}