-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbuild.gradle.kts
505 lines (456 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
import term.*
import BuildInfo.*
import plugins.*
import org.gradle.kotlin.dsl.*
import org.gradle.jvm.tasks.Jar
import co.riiid.gradle.ReleaseTask
import kotlinx.coroutines.experimental.*
import us.kirchmeier.capsule.task.*
import us.kirchmeier.capsule.manifest.CapsuleManifest
import us.kirchmeier.capsule.spec.ReallyExecutableSpec
import org.jetbrains.kotlin.gradle.dsl.Coroutines.ENABLE
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import org.gradle.api.internal.HasConvention
import org.apache.tools.ant.filters.ReplaceTokens
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
import org.junit.platform.gradle.plugin.JUnitPlatformPlugin
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import com.github.benmanes.gradle.versions.updates.DependencyUpdatesTask
import io.spring.gradle.dependencymanagement.DependencyManagementPlugin
import org.jetbrains.dokka.gradle.*
import org.jetbrains.kotlin.gradle.plugin.KotlinBasePluginWrapper
buildscript {
repositories {
jcenter()
maven { setUrl(kotlinEapRepoSysProp) }
}
dependencies {
// Require only for EAPs
listOf("kotlin-gradle-plugin", "kotlin-allopen", "kotlin-noarg").forEach {
classpath("org.jetbrains.kotlin:$it:$kotlinSysProp")
}
classpath("org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlinxSysProp")
classpath("org.junit.platform:junit-platform-gradle-plugin:$junitSysProp")
}
}
val appVersion by project
val appAuthor by project
val javaVersion: JavaVersion by extra { JavaVersion.VERSION_1_8 }
val kotlinVersion: String by extra { kotlinSysProp }
val kotlinxVersion: String by extra { kotlinxSysProp }
val wrapperVersion: String by extra { wrapperSysProp }
val kotlinEAPRepo: String by extra { kotlinEapRepoSysProp }
val kotlinxRepo: String by extra { kotlinxRepoSysProp }
val springBootVersion: String by extra { springBootSysProp }
printHeader(appVersion)
plugins {
id("com.gradle.build-scan") version buildScanSysProp
application
java
idea
maven
jacoco
`help-tasks`
// For stable versions,
// kotlin(it, kotlinSysProp)
id("org.springframework.boot") version springBootSysProp
id("com.github.johnrengelman.shadow") version shadowSysProp
id("com.github.ben-manes.versions") version versionsSysProp
id("org.jlleitschuh.gradle.ktlint") version ktlintSysProp
id("org.jetbrains.dokka") version dokkaSysProp
id("us.kirchmeier.capsule") version "1.0.2"
id("com.dorongold.task-tree") version "1.3"
id("co.riiid.gradle") version "0.4.2"
}
/**
* Apply third party plugins.
*/
apply {
project.rootDir.listFiles().filter {
it != project.buildFile && it.name.endsWith(".kts")
}.forEach {
from(it.name)
}
kotlinPlugins.forEach { plugin(it) }
plugin<BookPlugin>()
plugin<JUnitPlatformPlugin>()
plugin<DependencyManagementPlugin>()
}
group = "io.sureshg"
version = appVersion
description = "Gradle Kotlin DSL starter!"
/**
* Configure java compiler options.
*/
java {
sourceCompatibility = javaVersion
targetCompatibility = javaVersion
}
/**
* Configure application plugin
*/
application {
applicationName = rootProject.name
mainClassName = "${project.group}.MainKt"
}
/**
* Enable coroutines.
*/
kotlin {
experimental.coroutines = ENABLE
}
/**
* Enable build scan
*/
buildScan {
setLicenseAgreementUrl("https://gradle.com/terms-of-service")
setLicenseAgree("yes")
tag(appVersion.toString())
link("GitHub", githubRepo.url)
buildFinished {
value("Build Result", failure?.message ?: "")
}
}
/**
* Custom plugin.
*/
books.invoke {
"quickStart" {
sourceFile = file("quick-start.pdf")
}
"userGuide" {
}
"developerGuide" {
}
}
/**
* A Configuration represents a group of artifacts and their dependencies.
*/
configurations {
"testConfig" {
configurations.compile.extendsFrom(this)
isTransitive = false
}
}
/**
* Maven pom config. Can use [GenerateMavenPom] if you are
* using maven-publish plugin .
*/
maven {
mvnpom {
withXml {
val deps = asNode().appendNode("dependencies")
configurations.compile.dependencies.forEach {
with(deps.appendNode("dependency")) {
appendNode("groupId", it.group)
appendNode("artifactId", it.name)
appendNode("version", it.version)
}
}
}
}
}
task("generatePom") {
doLast {
println("Generating the Maven POM file.".fg256())
maven.pom().writeTo("build/resources/main/META-INF/maven/${project.group}/${project.name}/pom.xml")
}
tasks.getByName("jar").dependsOn(this)
description = "Generates the Maven POM file for ${project.name} v$appVersion"
}
/**
* Java code coverage metrics.
*/
tasks.withType<JacocoReport> {
reports {
xml.isEnabled = true
html.isEnabled = false
csv.isEnabled = false
}
val jacocoTestReport by tasks
jacocoTestReport.dependsOn("test")
}
/**
* Enable java incremental compilation.
*/
tasks.withType<JavaCompile> {
options.isIncremental = true
}
/**
* Configure kotlin compiler options.
*/
tasks.withType<KotlinCompile> {
kotlinOptions {
jvmTarget = javaVersion.toString()
// javaParameters = true
}
}
/**
* Dependency version check. The current resolution strategy
* would disallow (only if "DisallowRC" env variable is set)
* release candidates as upgradable versions.
*/
tasks.withType<DependencyUpdatesTask> {
revision = "milestone"
outputFormatter = "plain"
description = "Displays the $revision dependency updates for ${project.name} v$appVersion"
if (System.getenv("DisallowRC").toBoolean()) {
resolutionStrategy = closureOf<ComponentSelectionRules> {
all { selection: ComponentSelection ->
val rcs = listOf("alpha", "beta", "rc", "cr", "m")
val rejected = rcs.any {
selection.candidate.version.matches("/(?i).*[.-]$it[.\\d-]*/".toRegex())
}
if (rejected) selection.reject("Release candidate.")
}
}
}
}
repositories {
maven { setUrl(kotlinEAPRepo) }
maven { setUrl(kotlinxRepo) }
jcenter()
mavenCentral()
}
/**
* Configures subproject's buildscript repo .A `buildscript` block is ignored
* unless it’s at the top level, the [ScriptHandler] instance available via
* the `buildscript` property however, can always be configured.
*/
subprojects {
buildscript.repositories {
jcenter()
mavenCentral()
}
}
dependencies {
compile(kotlin("stdlib-jre8", kotlinVersion))
compile(kotlin("reflect", kotlinVersion))
compile("org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlinxVersion")
compile("org.jetbrains.kotlinx:kotlinx-collections-immutable:0.1")
compile("com.squareup.retrofit2:retrofit:2.3.0")
compile("net.jodah:failsafe:1.0.4")
compile("com.squareup.moshi:moshi:1.5.0")
compile("com.github.jnr:jnr-posix:3.0.41")
compile("ru.gildor.coroutines:kotlin-coroutines-retrofit:0.5.1")
compile("org.springframework.boot:spring-boot-starter")
// compile("net.bytebuddy:byte-buddy:1.7.0")
compileOnly("com.google.code.findbugs:jsr305:3.0.2")
testCompile("org.springframework.boot:spring-boot-starter-test")
testCompile("org.mockito:mockito-core:2.8.47")
testCompile("org.junit.jupiter:junit-jupiter-api:5.0.0-M4")
testRuntime("org.junit.jupiter:junit-jupiter-engine:5.0.0-M4")
"testConfig"("com.google.code.findbugs:jsr305:3.0.2")
}
/**
* Kotlin Plugin version.
*/
val Project.kotlinPluginVersion get() = plugins.filterIsInstance<KotlinBasePluginWrapper>().firstOrNull()?.kotlinPluginVersion
/**
* Source set configuration
*/
val SourceSet.kotlin: SourceDirectorySet get() = (this as HasConvention).convention.getPlugin<KotlinSourceSet>().kotlin
tasks.withType<JavaCompile> {
doFirst {
sourceSets {
main {
println("${name.capitalize()} => Java : ${java.srcDirs}, Kotlin: ${kotlin.srcDirs}, Resource: ${resources.srcDirs}".dot.fg256())
}
test {
println("${name.capitalize()} => Java : ${java.srcDirs}, Kotlin: ${kotlin.srcDirs}, Resource: ${resources.srcDirs}".dot.fg256())
}
}
}
}
/**
* Add jar manifests.
*/
tasks.withType<Jar> {
manifest {
attributes(mapOf(
Author.attr to appAuthor,
Date.attr to buildDateTime,
JDK.attr to sysProp("java.version"),
BuildTarget.attr to javaVersion,
OS.attr to "${sysProp("os.name")} ${sysProp("os.version")}",
KotlinVersion.attr to kotlinVersion,
CreatedBy.attr to "Gradle ${gradle.gradleVersion}",
AppVersion.attr to appVersion,
Title.attr to application.applicationName,
Vendor.attr to project.group,
MainClass.attr to application.mainClassName))
}
}
/**
* Auto expand gradle properties.
*/
tasks.withType<ProcessResources> {
filesMatching("application.yaml") {
expand(project.properties)
}
onlyIf { file("src/main/resources/application.yaml").exists() }
}
/**
* Make executable
*/
val capsuleTask = task<FatCapsule>("makeExecutable") {
val minJavaVer = javaVersion.toString()
val appName = application.applicationName
val appMainClass = application.mainClassName
archiveName = appName
reallyExecutable(closureOf<ReallyExecutableSpec> { regular() })
capsuleManifest(closureOf<CapsuleManifest> {
premainClass = "Capsule"
mainClass = "Capsule"
applicationName = appName
applicationClass = appMainClass
applicationVersion = version
jvmArgs = listOf("-client", "-Djava.security.egd=file:/dev/./urandom")
args = listOf("$*")
minJavaVersion = minJavaVer
})
description = "Create $archiveName executable."
dependsOn("clean", "shadowJar")
doLast {
archivePath.setExecutable(true)
val size = archivePath.length().toBinaryPrefixString()
println("Executable File: ${archivePath.absolutePath.bold} (${size.bold})".done)
}
}
/**
* Creates fat-jar/uber-jar.
*/
val shadowTasks = tasks.withType<ShadowJar> {
classifier = ""
version = ""
description = "Create a fat JAR of ${project.name} v$appVersion and runtime dependencies."
doLast {
val size = archivePath.length().toBinaryPrefixString()
println("FatJar: ${archivePath.path.bold} (${size.bold})".done)
}
}
/**
* Generate doc using dokka.
*/
tasks.withType<DokkaTask> {
val src = "src/main"
val out = "$projectDir/docs"
val format = DokkaFormat.Html
doFirst {
println("Cleaning doc directory ${out.bold}...".cyan)
project.delete(fileTree(out) {
exclude("logos/**", "templates/**")
})
}
moduleName = ""
sourceDirs = files(src)
outputFormat = format.type
outputDirectory = out
skipEmptyPackages = true
jdkVersion = javaVersion.majorVersion.toInt()
includes = listOf("README.md", "CHANGELOG.md")
val mapping = LinkMapping().apply {
dir = src
url = "${githubRepo.url}/blob/master/$src"
suffix = "#L"
}
linkMappings = arrayListOf(mapping)
description = "Generate ${project.name} v$appVersion docs in ${format.desc} format."
doLast {
println("Generated ${format.desc} format docs to ${outputDirectory.bold}".done)
}
}
/**
* Generate ReadMe for the project.
*/
task<Copy>("generateReadMe") {
val version = appVersion.toString()
val tokens = mapOf("version" to version,
"versionBadge" to version.replace("-", "--"),
"kotlin" to kotlinVersion,
"kotlinBadge" to kotlinVersion.replace("-", "--"),
"changelogUrl" to githubRepo.changelogUrl(tag = version),
"project" to project.name)
inputs.properties(tokens)
from("docs/templates")
into(projectDir)
rename("_TMPL", "")
include("*.md")
filter<ReplaceTokens>("tokens" to tokens)
filteringCharset = "UTF-8"
description = "Generate README.md for ${application.applicationName.capitalize()} v$version release."
}
/**
* Prepare the docs and release version.
*/
task("prepareRelease") {
description = "Prepare ${application.applicationName.capitalize()} v$version release."
doLast {
println("Make sure to commit the doc changes before doing ${"./gradlew githubRelease".bold}".dot.cyan)
}
dependsOn("generateReadMe", "dokka")
}
/**
* Set Github token and publish.
*/
github {
val tag = version.toString()
baseUrl = "https://api.github.com"
owner = githubRepo.user
repo = githubRepo.repo
tagName = tag
targetCommitish = "master"
name = "${application.applicationName.capitalize()} v$version"
val changelog = githubRepo.changelogUrl(branch = targetCommitish, tag = tag)
body = ":mega: $name release. Check [CHANGELOG.md]($changelog) for details. :tada:"
setAssets(capsuleTask.archivePath.path, shadowTasks.first().archivePath?.path)
}
tasks.withType<ReleaseTask> {
doFirst {
github.token = getEnv("GITHUB_TOKEN")
}
doLast {
println("Published github release ${github.name}.".done)
println("Release URL: ${githubRepo.releaseUrl().bold}")
}
description = "Publish Github release ${github.name}"
dependsOn("makeExecutable")
}
/**
* A tasks using coroutines.
*/
task("fib") {
description = "A fibonacci task using kotlin generators."
doLast {
fib().take(10).forEach(::println)
}
}
task("async") {
description = "An async task using kotlin coroutines."
doLast {
runBlocking {
launch(CommonPool) {
delay(2000)
println("Gradle Kotlin DSL!")
}
print("Hello, ")
delay(2000)
}
println("By ${appAuthor ?: "Suresh"}")
}
}
/**
* Generate Gradle Kotlin DSL wrapper.
*/
task<Wrapper>("wrapper") {
description = "Generate Gradle Kotlin DSL wrapper ${wrapperVersion.bold}"
distributionType = Wrapper.DistributionType.ALL
distributionUrl = gradleKotlinDslUrl(wrapperVersion)
doFirst {
println(description)
}
}
/**
* Set default task
*/
defaultTasks("clean", "tasks", "--all")