generated from stardust-enterprises/kotlin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.gradle.kts
357 lines (292 loc) · 10.2 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
import java.net.URL
import java.time.OffsetDateTime
import java.time.format.DateTimeFormatter
plugins {
with(Plugins) {
// Language Plugins
`java-library`
kotlin("jvm") version KOTLIN
// Git Repo Information
id("org.ajoberstar.grgit") version GRGIT
// Code Quality
id("org.jlleitschuh.gradle.ktlint") version KTLINT
// Documentation Generation
id("org.jetbrains.dokka") version DOKKA
// Maven Publication
id("io.github.gradle-nexus.publish-plugin") version NEXUS_PUBLISH
`maven-publish`
signing
}
}
// What JVM version should this project compile to
val targetVersion = "1.8"
// What JVM version this project is written in
val sourceVersion = "1.8"
// Which source-sets to add.
val additionalSourceSets: Array<String> = arrayOf(
"api"
)
// Maven Repositories
repositories {
mavenLocal()
mavenCentral()
Repositories.mavenUrls.forEach(::maven)
}
// Project Dependencies
dependencies {
with(Dependencies) {
kotlinModules.forEach {
implementation("org.jetbrains.kotlin", "kotlin-$it", KOTLIN)
}
listOf("asm", "asm-tree").forEach {
implementation("org.ow2.asm", it, ASM)
}
implementation("codes.som.anthony", "koffee", KOFFEE) {
exclude(group = "org.ow2.asm")
}
testImplementation("org.jetbrains.kotlin", "kotlin-test", KOTLIN)
}
}
configurations {
// Makes all the configurations use the same Kotlin version.
all {
resolutionStrategy.eachDependency {
if (requested.group == "org.jetbrains.kotlin") {
useVersion(Dependencies.KOTLIN)
}
}
}
}
group = Coordinates.GROUP
version = Coordinates.VERSION
// Generate the additional source sets
additionalSourceSets.forEach(::createSourceSet)
fun createSourceSet(name: String, sourceRoot: String = "src/$name") {
sourceSets {
val main by sourceSets
val test by sourceSets
val sourceSet = create(name) {
java.srcDir("$sourceRoot/kotlin")
resources.srcDir("$sourceRoot/resources")
this.compileClasspath += main.compileClasspath
this.runtimeClasspath += main.runtimeClasspath
}
arrayOf(main, test).forEach {
it.compileClasspath += sourceSet.output
it.runtimeClasspath += sourceSet.output
}
}
}
// The latest commit ID
val buildRevision: String = grgit.log()[0].id ?: "dev"
// Disable unneeded rules
ktlint {
this.disabledRules.addAll(
"no-wildcard-imports",
"filename"
)
}
tasks {
test {
useJUnitPlatform()
}
// Configure JVM versions
compileKotlin {
kotlinOptions {
jvmTarget = targetVersion
freeCompilerArgs = listOf(
"-opt-in=kotlin.RequiresOptIn",
)
}
}
compileJava {
targetCompatibility = targetVersion
sourceCompatibility = sourceVersion
}
dokkaHtml {
val moduleFile = File(projectDir, "MODULE.temp.md")
run {
// In order to have a description on the rendered docs, we have to have
// a file with the # Module thingy in it. That's what we're
// automagically creating here.
doFirst {
moduleFile.writeText("# Module ${Coordinates.NAME}\n${Coordinates.DESC}")
}
doLast {
moduleFile.delete()
}
}
moduleName.set(Coordinates.NAME)
dokkaSourceSets.configureEach {
displayName.set("${Coordinates.NAME} on ${Coordinates.GIT_HOST}")
includes.from(moduleFile.path)
skipDeprecated.set(false)
includeNonPublic.set(false)
skipEmptyPackages.set(true)
reportUndocumented.set(true)
suppressObviousFunctions.set(true)
// Link the source to the documentation
sourceLink {
localDirectory.set(file("src"))
remoteUrl.set(URL("https://${Coordinates.GIT_HOST}/${Coordinates.REPO_ID}/tree/trunk/src"))
}
// External documentation link template
// externalDocumentationLink {
// url.set(URL("https://javadoc.io/doc/net.java.dev.jna/jna/5.10.0/"))
// }
}
}
// The original artifact, we just have to add the API source output and the
// LICENSE file.
jar {
fun normalizeVersion(versionLiteral: String): String {
val regex = Regex("(\\d+\\.\\d+\\.\\d+).*")
val match = regex.matchEntire(versionLiteral)
require(match != null) {
"Version '$versionLiteral' does not match version pattern, e.g. 1.0.0-QUALIFIER"
}
return match.groupValues[1]
}
val buildTimeAndDate = OffsetDateTime.now()
val buildDate = DateTimeFormatter.ISO_LOCAL_DATE.format(buildTimeAndDate)
val buildTime = DateTimeFormatter.ofPattern("HH:mm:ss.SSSZ").format(buildTimeAndDate)
val javaVersion = System.getProperty("java.version")
val javaVendor = System.getProperty("java.vendor")
val javaVmVersion = System.getProperty("java.vm.version")
with(Coordinates) {
mapOf(
"Created-By" to "$javaVersion ($javaVendor $javaVmVersion)",
"Build-Date" to buildDate,
"Build-Time" to buildTime,
"Build-Revision" to buildRevision,
"Specification-Title" to project.name,
"Specification-Version" to normalizeVersion(project.version.toString()),
"Specification-Vendor" to VENDOR,
"Implementation-Title" to NAME,
"Implementation-Version" to VERSION,
"Implementation-Vendor" to VENDOR,
"Bundle-Name" to NAME,
"Bundle-Description" to DESC,
"Bundle-DocURL" to "https://$GIT_HOST/$REPO_ID",
"Bundle-Vendor" to VENDOR,
"Bundle-SymbolicName" to "$GROUP.$NAME"
).forEach { (k, v) ->
manifest.attributes[k] = v
}
}
additionalSourceSets.forEach {
from(sourceSets[it].output)
}
from("LICENSE")
}
additionalSourceSets.forEach {
// Custom artifact, only including the output of
// the source set and the LICENSE file.
create(it + "Jar", Jar::class) {
group = "build"
archiveClassifier.set(it)
from(sourceSets[it].output)
from("LICENSE")
}
}
// Source artifact, including everything the 'main' does but not compiled.
create("sourcesJar", Jar::class) {
group = "build"
archiveClassifier.set("sources")
from(sourceSets["main"].allSource)
additionalSourceSets.forEach {
from(sourceSets[it].allSource)
}
this.manifest.from(jar.get().manifest)
from("LICENSE")
}
// The Javadoc artifact, containing the Dokka output and the LICENSE file.
create("javadocJar", Jar::class) {
group = "build"
val dokkaHtml = getByName("dokkaHtml")
archiveClassifier.set("javadoc")
dependsOn(dokkaHtml)
from(dokkaHtml)
from("LICENSE")
}
afterEvaluate {
// Task priority
val publishToSonatype by getting
val closeAndReleaseSonatypeStagingRepository by getting
closeAndReleaseSonatypeStagingRepository
.mustRunAfter(publishToSonatype)
// Wrapper task since calling both one after the other in IntelliJ
// seems to cause some problems.
create("releaseToSonatype") {
group = "publishing"
dependsOn(
publishToSonatype,
closeAndReleaseSonatypeStagingRepository
)
}
}
}
// Define the default artifacts' tasks
val defaultArtifactTasks = arrayOf(
tasks["sourcesJar"],
tasks["javadocJar"]
).also { arr ->
additionalSourceSets.forEach { set ->
arr.plus(tasks[set + "Jar"])
}
}
// Declare the artifacts
artifacts {
defaultArtifactTasks.forEach(::archives)
}
publishing.publications {
// Sets up the Maven integration.
create("mavenJava", MavenPublication::class.java) {
from(components["java"])
defaultArtifactTasks.forEach(::artifact)
with(Coordinates) {
pom {
name.set(NAME)
description.set(DESC)
url.set("https://$GIT_HOST/$REPO_ID")
with(Pom) {
licenses {
licenses.forEach {
license {
name.set(it.name)
url.set(it.url)
distribution.set(it.distribution)
}
}
}
developers {
developers.forEach {
developer {
id.set(it.id)
name.set(it.name)
}
}
}
}
scm {
connection.set("scm:git:git://$GIT_HOST/$REPO_ID.git")
developerConnection.set("scm:git:ssh://$GIT_HOST/$REPO_ID.git")
url.set("https://$GIT_HOST/$REPO_ID")
}
}
}
// Configure the signing extension to sign this Maven artifact.
signing {
isRequired = project.properties["signing.keyId"] != null
sign(this@create)
}
}
}
// Configure publishing to Maven Central
nexusPublishing.repositories.sonatype {
nexusUrl.set(uri("https://s01.oss.sonatype.org/service/local/"))
snapshotRepositoryUrl.set(uri("https://s01.oss.sonatype.org/content/repositories/snapshots/"))
// Skip this step if environment variables NEXUS_USERNAME or NEXUS_PASSWORD aren't set.
username.set(properties["NEXUS_USERNAME"] as? String ?: return@sonatype)
password.set(properties["NEXUS_PASSWORD"] as? String ?: return@sonatype)
}