0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Kotlin: Gradle で使う

Last updated at Posted at 2023-10-17

Gradle のバージョン

$ gradle --version

------------------------------------------------------------
Gradle 7.2
------------------------------------------------------------

Build time:   2021-08-17 09:59:03 UTC
Revision:     a773786b58bb28710e3dc96c4d1a7063628952ad

Kotlin:       1.5.21
Groovy:       3.0.8
Ant:          Apache Ant(TM) version 1.10.9 compiled on September 27 2020
JVM:          16.0.1 (Private Build 16.0.1+9-Ubuntu-120.04)
OS:           Linux 6.2.0-34-generic amd64

プロジェクトの作成

mkdir ex04
cd ex04
gradle init
$ gradle init

Select type of project to generate:
  1: basic
  2: application
  3: library
  4: Gradle plugin
Enter selection (default: basic) [1..4] 2

Select implementation language:
  1: C++
  2: Groovy
  3: Java
  4: Kotlin
  5: Scala
  6: Swift
Enter selection (default: Java) [1..6] 4

Split functionality across multiple subprojects?:
  1: no - only one application project
  2: yes - application and library projects
Enter selection (default: no - only one application project) [1..2] 1

Select build script DSL:
  1: Groovy
  2: Kotlin
Enter selection (default: Kotlin) [1..2] 2

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

> Task :init
Get more help with your project: https://docs.gradle.org/7.2/samples/sample_building_kotlin_applications.html

BUILD SUCCESSFUL in 20s
2 actionable tasks: 2 executed

Build

./gradlew build
./gradlew

実行

./gradlew run
$ ./gradlew run

> Task :app:run
Hello World!

BUILD SUCCESSFUL in 474ms
2 actionable tasks: 1 executed, 1 up-to-date

ソースコードの修正

app/src/main/kotlin/ex04/App.kt
/*
 * This Kotlin source file was generated by the Gradle 'init' task.
 */
package ex04

class App {
    val greeting: String
        get() {
            return "本日は晴天なり!"
        }
}

fun main() {
    println(App().greeting)
}

実行

./gradlew run

実行結果

> Task :app:run
本日は晴天なり!

BUILD SUCCESSFUL in 3s
2 actionable tasks: 2 executed

更にソースコードを改造

app/src/main/kotlin/ex04/App.kt
// ------------------------------------------------------------------
/*
	app/src/main/kotlin/ex04/App.kt

					Oct/17/2023
*/
// ------------------------------------------------------------------
package ex04

import java.time.LocalDate
import java.time.LocalDateTime

// ------------------------------------------------------------------
class App {
    val greeting: String
        get() {
            return "本日は晴天なり!"
        }
}

fun main() {
	println(" *** 開始 ***")
	println(LocalDate.now())
	println(LocalDateTime.now())
	println(App().greeting)
	println(" *** 終了 ***")
}
// ------------------------------------------------------------------

実行結果

> Task :app:run
 *** 開始 ***
2023-10-17
2023-10-17T14:31:56.545742612
本日は晴天なり!
 *** 終了 ***

BUILD SUCCESSFUL in 575ms

参考: 生成された設定ファイル

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 manual at https://docs.gradle.org/7.2/userguide/multi_project_builds.html
 */

rootProject.name = "ex04"
include("app")
app/build.gradle.kts
/*
 * This file was generated by the Gradle 'init' task.
 *
 * This generated file contains a sample Kotlin application project to get you started.
 * For more details take a look at the 'Building Java & JVM projects' chapter in the Gradle
 * User Manual available at https://docs.gradle.org/7.2/userguide/building_java_projects.html
 */

plugins {
    // Apply the org.jetbrains.kotlin.jvm Plugin to add support for Kotlin.
    id("org.jetbrains.kotlin.jvm") version "1.5.0"

    // Apply the application plugin to add support for building a CLI application in Java.
    application
}

repositories {
    // Use Maven Central for resolving dependencies.
    mavenCentral()
}

dependencies {
    // Align versions of all Kotlin components
    implementation(platform("org.jetbrains.kotlin:kotlin-bom"))

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

    // This dependency is used by the application.
    implementation("com.google.guava:guava:30.1.1-jre")

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

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

application {
    // Define the main class for the application.
    mainClass.set("ex04.AppKt")
}
0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?