1. 程式人生 > >Gradle基礎:11:使用Kotlin的Gradle

Gradle基礎:11:使用Kotlin的Gradle

在這裡插入圖片描述
Gradle預設方式使用groovy作為DSL的支援方式,kotlin也作為支援方式之一,這篇文章將介紹一下如何在Gradle中使用Kotlin DSL。

使用Kotlin的init

gradle init也可以直接指定kotlin的方式生成相關的檔案,使用方式如下

使用命令:gradle init --dsl kotlin

liumiaocn:kotlin liumiao$ gradle init --dsl kotlin
Starting a Gradle Daemon (subsequent builds will be faster)

BUILD SUCCESSFUL in 3s
2 actionable tasks: 2 executed
liumiaocn:kotlin liumiao$ ls
build.gradle.kts    gradle              gradlew             gradlew.bat         settings.gradle.kts
liumiaocn:kotlin liumiao$ tree .
.
├── build.gradle.kts
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
└── settings.gradle.kts

2 directories, 6 files
liumiaocn:kotlin liumiao$

相關的介紹可以參看如下文章:

相較於groovy方式,build和settings的字尾稍有變化

liumiaocn:kotlin liumiao$ cat settings.gradle.kts 
/*
 * This file was generated by the Gradle 'init' task.
 *
 * The settings file is used to specify which projects to include in your build.
 * 
 * Detailed information about configuring a multi-project build in Gradle can be found
 * in the user guide at https://docs.gradle.org/4.10.2/userguide/multi_project_builds.html
 */

rootProject.name = "kotlin"
liumiaocn:kotlin liumiao$ 
liumiaocn:kotlin liumiao$ cat build.gradle.kts 
/*
 * This file was generated by the Gradle 'init' task.
 *
 * This is a general purpose Gradle build.
 * Learn how to create Gradle builds at https://guides.gradle.org/creating-new-gradle-builds/
 */
liumiaocn:kotlin liumiao$ 

構建示例

這篇文章會通過對上文的例子進行重新改寫,還是如下4個任務:

groovy 方式的構建指令碼

println "[phase:configuration] build.gradle ..."

task compile {
  group 'compile'
  description 'compile task'
  println "[phase:configuration] compile"
  doFirst {
    println "[phase:execution] compile :doFirst()"
  }
}

tasks.create(name: 'test',dependsOn: compile) {
  group 'test'
  description 'test task'
  println "[phase:configuration] test"
  doLast {
    println "[phase:execution] test:doLast()"
  }
}

tasks.create("packaging") {
  group 'packaging'
  description 'packaging task'
  dependsOn test
  enabled   false
  println "[phase:configuration] packaging"
  doLast {
    println "[phase:execution] packaging:doLast()"
  }
}

class Install extends DefaultTask{
  String installObjectName

  @TaskAction
  void checkObject() {
    println "[phase:execution] install:checkObject   (${installObjectName})"
  }

  @TaskAction
  void installObject() {
    println "[phase:execution] install:installObject (${installObjectName})"
  }
}

task install(type: Install) {
  group 'install'
  description 'install task'
  installObjectName 'test.jar'

  println "[phase:configuration] install"
  doFirst {
    println "[phase:execution] install:doFirst()"
  }
  doLast {
    println "[phase:execution] install:doLast()"
  }
}
install.dependsOn packaging
install.onlyIf { packaging.enabled }

詳細的說明可以參看前面的文章:

kotlin方式的構建指令碼

只要基本符合kotlin的語法,很容易就可以將groovy的方式改成kotlin,比如可以改成如下:

liumiaocn:kotlin liumiao$ cat build.gradle.kts 
tasks.create("compile") {
  group = "compile"
  description = "compile task"
  println("[phase:configuration] compile")
  doFirst {
    println("[phase:execution] compile :doFirst()")
  }
}

tasks.create("test") {
  group = "test"
  description = "test task"
  println("[phase:configuration] test")
  doLast {
    println("[phase:execution] test:doLast()")
  }
}.dependsOn("compile")

tasks.create("packaging") {
  group = "packaging"
  description = "packaging task"
  enabled =  true
  println("[phase:configuration] packaging")
  doLast {
    println("[phase:execution] packaging:doLast()")
  }
}.dependsOn("test")

open class Install: DefaultTask() {
  lateinit var installObjectName: String

  @TaskAction
  fun checkObject() {
    println("[phase:execution] install:checkObject   (${installObjectName})")
  }

  @TaskAction
  fun installObject() {
    println("[phase:execution] install:installObject (${installObjectName})")
  }
}

tasks.create<Install>("install") {
  group = "install"
  description = "install task"
  installObjectName = "test.jar"
  var isSkip = false

  println("[phase:configuration] install")
  doFirst {
    println("[phase:execution] install:doFirst()")
  }
  doLast {
    println("[phase:execution] install:doLast()")
  }
  onlyIf { !isSkip }
}.dependsOn("packaging")
liumiaocn:kotlin liumiao$

執行確認

liumiaocn:kotlin liumiao$ gradle install

> Configure project :
[phase:configuration] compile
[phase:configuration] test
[phase:configuration] packaging
[phase:configuration] install

> Task :compile
[phase:execution] compile :doFirst()

> Task :test
[phase:execution] test:doLast()

> Task :packaging
[phase:execution] packaging:doLast()

> Task :install
[phase:execution] install:doFirst()
[phase:execution] install:checkObject   (test.jar)
[phase:execution] install:installObject (test.jar)
[phase:execution] install:doLast()

BUILD SUCCESSFUL in 0s
4 actionable tasks: 4 executed
liumiaocn:kotlin liumiao$

總結

這篇文章介紹瞭如何使用kotlin代替groovy在gradle中進行使用,無論是kotlin還是groovy都有很大的靈活性,使用者可根據情況進行自行選擇。