Gradle基础:11:使用Kotlin的Gradle

版权声明:本文为博主原创文章,未经博主允许欢迎转载,但请注明出处。 https://blog.csdn.net/liumiaocn/article/details/84478356

在这里插入图片描述
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个任务:

编译: compile
测试:test
打包:packaging
安装:install

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都有很大的灵活性,使用者可根据情况进行自行选择。

阅读更多

没有更多推荐了,返回首页