こちらのプログラムを改造しました。
Kotlin: Gradle で使う
プログラム
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
import java.time.format.DateTimeFormatter
import kotlinx.serialization.*
import kotlinx.serialization.json.*
// ------------------------------------------------------------------
class App {
val greeting: String
get() {
return "本日は晴天なり!"
}
}
fun main() {
println(" *** 開始 ***")
println(LocalDate.now())
println(LocalDateTime.now())
println(App().greeting)
@Serializable
data class Data(
val key: String,
val name: String,
val population:Int,
val date_mod: String)
val date1: String = "2022-11-04"
val datax = Data("t0921","宇都宮",92145,date1)
val json = Json.encodeToString(serializer<Data>(), datax)
println(json)
println(" *** 終了 ***")
}
// ------------------------------------------------------------------
設定ファイル
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.9.0"
id("org.jetbrains.kotlin.plugin.serialization") version "1.9.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")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.0")
// 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")
}
実行結果
> Task :app:run
*** 開始 ***
2023-10-17
2023-10-17T15:22:29.999678881
本日は晴天なり!
{"key":"t0921","name":"宇都宮","population":92145,"date_mod":"2022-11-04"}
*** 終了 ***
BUILD SUCCESSFUL in 655ms
2 actionable tasks: 1 executed, 1 up-to-date