7
5

More than 5 years have passed since last update.

Gradle 5.0 で作れるようになった build.gradle.kts と settings.gradle.kts

Posted at

Gradle 5.0 から正式に Kotlin DSL が取り込まれました。

特筆すべき違いは、gradle init で Kotlin のプロジェクト を作成することができ、 build.gradle.kts, settings.gradle.kts も出力できるということです。

build.gradle.kts, settings.gradle.kts はそれぞれ、 Groovy で書いていた build.gradle, settings.gradle の Kotlin 版 です。

実際のところどうなるのか

プロジェクト作成

gradle init の出力を、順に説明します。

まずは gradle init を実行します。 このときの gradle のバージョンは 5.0 です。

gradle init

するとプロジェクトの形式を尋ねられます。 ここでは 6 kotlin-appliaiton を選びます。 7 kotlin-library を選んでも、 gradle init の手順は変わりません。

Starting a Gradle Daemon (subsequent builds will be faster)       

Select type of project to generate:
  1: basic                                  
  2: groovy-application                     
  3: groovy-library                         
  4: java-application                       
  5: java-library                           
  6: kotlin-application                     
  7: kotlin-library                         
  8: scala-library
Enter selection (default: basic) [1..8] 6

次に ビルドスクリプト を Groovy にするか Kotlin にするか()かれますので、 Kotlin にします。

Select build script DSL:
  1: groovy
  2: kotlin
Enter selection (default: kotlin) [1..2] 2

最後にプロジェクトの基本情報を尋ねられますので、随時変更してプロジェクトファイルを作成します。

Project name (default: sample): 
Source package (default: sample):

BUILD SUCCESSFUL in 33s
2 actionable tasks: 2 executed

これでプロジェクトの基本ファイルが作成されました。

作成されるファイル

作成されるファイルは、 application を選んだ場合と library を選んだ場合とで少し異なります。
tree の結果を掲載します。

application
.
├── build.gradle.kts
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── settings.gradle.kts
└── src
    ├── main
    │   ├── kotlin
    │   │   └── sample
    │   │       └── App.kt
    │   └── resources
    └── test
        ├── kotlin
        │   └── sample
        │       └── AppTest.kt
        └── resources

11 directories, 8 files
library
.
├── build.gradle.kts                                                                                                                                                                                                                                                                                                                                                        
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── settings.gradle.kts
└── src
    ├── main
    │   ├── kotlin
    │   │   └── sample
    │   │       └── Library.kt
    │   └── resources
    └── test
        ├── kotlin
        │   └── sample
        │       └── LibraryTest.kt
        └── resources

11 directories, 8 files

application の場合は main関数 も作られますが、 library の場合は main関数 が作られません。

Kotlin のビルドスクリプトは次のようになります。

build.gradle.kts

/*
 * This file was generated by the Gradle 'init' task.
 *
 * This generated file contains a sample Kotlin library project to get you started.
 */

plugins {
    // Apply the Kotlin JVM plugin to add support for Kotlin on the JVM
    id("org.jetbrains.kotlin.jvm").version("1.3.10")
}

repositories {
    // Use jcenter for resolving your dependencies.
    // You can declare any Maven/Ivy/file repository here.
    jcenter()
}

dependencies {
    // Use the Kotlin JDK 8 standard library
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")

    // Use the Kotlin test library
    testImplementation("org.jetbrains.kotlin:kotlin-test")

    // Use the Kotlin JUnit integration
    testImplementation("org.jetbrains.kotlin:kotlin-test-junit")
}

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/5.0/userguide/multi_project_builds.html
 */

rootProject.name = "sample"
7
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
5