-以下基于Gradle 4.3.1-
几周前,我开始迁移大部分 Groovy -based 摇动 .build
scripts to 科特林 -backed 摇动 .build.kts
scripts using the 科特林 DSL . Why would I do that? 科特林 is my language of choice and I love the idea of using a single language to do all my work. I never learned programming with Groovy and only know the bloody basics, which always makes me think: "This can't be the best way to do things...". 科特林 , on the other hand, is a language I use on a daily basis and therefore I know how to use the language appropriately. Additionally, 科特林 is a statically-typed language, whereas Groovy isn't. IDEs are having hard times offering code completion and error detection at compile time when a Groovy build script is being edited. As for the 科特林 DSL , 这个 isn't true. Especially IntelliJ knows how to help us with 科特林 development, even in 摇动 .build.kts
files. All these reasons made me take a deeper look at the new style Gradle offers.
次要障碍
It can sometimes be a bit tedious to rewrite your 摇动 .build
into 摇动 .build.kts
files, especially in the IDE with all its caches malfunctioning during that process. I often had to reopen my project or even reimport it before IntelliJ understood what was going on. It also often helps to use "Refresh all Gradle projects" button in the Gradle view.
让我们来看看
下面的代码片段显示了工作示例的第一部分。它取材于我的一个项目,该项目是基于以下内容的Kotlin Web应用程序: Vert.x 工具包。了解有关该技术的更多信息 这个 我之前写的帖子。
The script first defines a few global variables, mostly containing version numbers, which are used throughout the build file. Next, we can observe the plugins
block that simply defines a few plugins used for the build. Most importantly, the 科特林 Gradle plugin for JVM applications is included, which we can do with the DSL-specific function kotlin(module: String)
, that takes its module
argument and appends it to "org.jetbrains.kotlin."
, which then is put into the id(plugin: String)
method, the default api for applying plugins. Last but not least, we can see the listing of dependencies
, which again provides a kotlin
convenience method we can use to reduce redundant declarations. A similar approach can be seen with the definition of the io.vertx
dependencies. In order to only once write the "io.vertx.vertx"
String, which is part of every single Vert.x dependency, it's used as a receiver of let
. A first example of real idiomatic code within the build script.
//imports
//taken from the `plugins` defined later in the file
val kotlinVersion = plugins.getPlugin(KotlinPluginWrapper::class.java).kotlinPluginVersion
val kotlinCoroutinesVersion = "0.19.3"
val vertxVersion = "3.5.0" //
val nexusRepo = "http://x.x.x.x:8080/nexus/content/repositories/releases"
plugins {
kotlin("jvm").version("1.2.0")
application
java
`maven-publish`
}
dependencies {
compile(kotlin("stdlib", kotlinVersion))
compile(kotlin("reflect", kotlinVersion))
compile("org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlinCoroutinesVersion")
"io.vertx:vertx".let { v ->
compile("$v-lang-kotlin:$vertxVersion")
compile("$v-lang-kotlin-coroutines:$vertxVersion")
compile("$v-web:$vertxVersion")
compile("$v-mongo-client:$vertxVersion")
compile("$v-health-check:$vertxVersion")
compile("$v-web-templ-thymeleaf:$vertxVersion")
}
compile("org.slf4j:slf4j-api:1.7.14")
compile("ch.qos.logback:logback-classic:1.1.3")
compile("com.fasterxml.jackson.module:jackson-module-kotlin:2.9.0.pr3")
testCompile(kotlin("test", kotlinVersion))
testCompile(kotlin("test-junit", kotlinVersion))
testCompile("io.vertx:vertx-unit:$vertxVersion")
testCompile("org.mockito:mockito-core:2.6.2")
testCompile("junit:junit:4.11")
}
// Part 2
}
The second part of the example project starts with defining repositories
, which are used to find dependencies and plugins declared earlier. Again, we see an example of simplifying the code with the help of using the language: The custom Maven repositories are defined using the functional method forEach
, and thus shortens the boilerplate. After that, the plugins are being configured, which for instance is necessary for enabling coroutine
support or defining the application properties. Finally, we can observe a sequence of task configurations that control the behavior of single build steps, e.g. tests
.
// ...Part 1
repositories {
mavenCentral()
jcenter()
listOf("//www.seasar.org/maven/maven2/",
"//plugins.gradle.org/m2/",
nexusRepo).forEach {
maven { url = uri(it) }
}
}
kotlin {
experimental.coroutines = Coroutines.ENABLE
}
application {
group = "de.swirtz"
version = "1.0.0"
applicationName = " 摇动 -kotlindsl"
mainClassName = "de.swirtz.ApplicationKt"
}
publishing {
repositories {
maven {
url = uri(nexusRepo)
}
}
if (!project.hasProperty("jenkins")) {
println("Property 'jenkins' not set. Publishing only to MavenLocal")
} else {
(publications) {
"maven"(MavenPublication::class) {
from(components["java"])
}
}
}
}
tasks {
withType {
kotlinOptions.jvmTarget = "1.8"
}
withType {
testLogging.showStandardStreams = true
}
withType {
manifest {
attributes["Main-Class"] = application.mainClassName
}
from(configurations.runtime.map { if (it.isDirectory) it else zipTree(it) })
}
withType {
finalizedBy("publishToMavenLocal")
}
}
结果
We've seen a rather simple build script written with the Gradle 科特林 DSL . I made use of a few idiomatic 科特林 functions in order to show the power of such .kts
files. Especially for 科特林 developers, it can make much sense to completely switch to the shown approach. IntelliJ does support the creation of new build.gradle.kts
files 通过 default when you open the "New" option in "Project" view.
在某些情况下,这会让您想向别人寻求帮助。我建议直接在相应的Kotlin Slack频道中进行宣传: 摇动 .
希望我能启发您尝试一下!祝你好运ðŸ™,
整个脚本的要点
Simon是总部位于德国的软件工程师,具有7年为JVM和JavaScript编写代码的经验。他’他对尽可能多地学习新事物充满热情,并且是科特林(Kotlin)的自发狂热者。
[…在几周前发布的最新博客文章中,我了解了Gradle 科特林 DSL 的用法以及它如何帮助描述构建脚本。在另一篇较早的文章中,我介绍了一些[…]
[…在几周前发布的最新博客文章中,我了解了Gradle 科特林 DSL 的用法以及它如何帮助描述构建脚本。在另一篇较早的文章中,我介绍了一些[…]
[…] That’也是与Gradle 科特林 DSL 结合使用的Gradle构建文件的格式,例如gradle.build.kts。 Gradle展示了一个特殊的领域特定语言示例,它可以[…]
[…]表现出色且易于管理。另一种解决方案是使用用Kotlin DSL编写的Gradle脚本,并在build.gradle.kts文件中定义自定义任务,该文件显然可以保存和运行Kotlin […]