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个任务: