7
5

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 5 years have passed since last update.

【SpringBoot】Gradle Kotlin DSLでビルドしてハロワするまで

Last updated at Posted at 2019-03-01

追記

現状ではSpring InitializrでKotlin DSLなSpring Bootのプロジェクトを生成できるようになっているため、今からプロジェクトを始める場合はそちらで生成することをおすすめします。

やること

以下の資料を参考にしながらSpringBootをKotlin-DSLで動かします。
build.gradle.ktsに移行しよう - DroidKaigi 2019

この記事で使ったリポジトリは以下です。
k163377/kts-test: Spring Boot on Kotlin DSL

感想

ググれば案外情報が出てくるので、調べれば気合で色々なんとかなりました。

手順

以下の手順で進めていきます。

  1. Spring Initializrによる初期化
  2. settings.gradleの書き換え
  3. build.gradleの書き換え
  4. コントローラーを追加してハロワ

環境はIntelliJ IDEA 2018.3.2 (Ultimate Edition)でやりました。

Spring Initializrによる初期化

解説記事は他に沢山あるのでやりません。
今回は「Generate a Gradle Project with Kotlin and Spring Boot 2.1.3」で出力しました。

settings.gradleの書き換え

セッションの5:30辺りを参考に、settings.gradleの書き換えを行います。

settings.gradle(書き換え前)
pluginManagement {
	repositories {
		gradlePluginPortal()
	}
}
rootProject.name = 'kts-test'

'"に書き換え、ファイル名をsettings.gradle.ktsとするだけで完了です。

settings.gradle.kts(書き換え後)
pluginManagement {
	repositories {
		gradlePluginPortal()
	}
}
rootProject.name = "kts-test"

build.gradleを書き換え

気合でゴニョゴニョしたので詳細は書けません(ごめんなさい)。

一応セッションの11:36辺りを参考に、「'([^']+)'"$1"」置換はやりました。
その後は気合で書き換えました。

build.gradle(書き換え前)
plugins {
	id 'org.springframework.boot' version '2.1.3.RELEASE'
	id 'org.jetbrains.kotlin.jvm' version '1.2.71'
	id 'org.jetbrains.kotlin.plugin.spring' version '1.2.71'
}

apply plugin: 'io.spring.dependency-management'

group = 'com.wrongwrong'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
	mavenCentral()
}

dependencies {
	implementation 'org.springframework.boot:spring-boot-starter'
	implementation 'org.jetbrains.kotlin:kotlin-reflect'
	implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
	testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

compileKotlin {
	kotlinOptions {
		freeCompilerArgs = ['-Xjsr305=strict']
		jvmTarget = '1.8'
	}
}

compileTestKotlin {
	kotlinOptions {
		freeCompilerArgs = ['-Xjsr305=strict']
		jvmTarget = '1.8'
	}
}
build.gradle.kts書き換え後
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
	id("org.springframework.boot") version "2.1.3.RELEASE"
	id("org.jetbrains.kotlin.jvm") version "1.2.71"
	id("org.jetbrains.kotlin.plugin.spring") version "1.2.71"
}

apply(plugin = "io.spring.dependency-management")

group = "com.wrongwrong"
version = "0.0.1-SNAPSHOT"
val sourceCompatibility = "1.8"

repositories {
	mavenCentral()
}

dependencies {
	implementation("org.springframework.boot:spring-boot-starter")
	implementation("org.jetbrains.kotlin:kotlin-reflect")
	implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
	testImplementation("org.springframework.boot:spring-boot-starter-test")
}

// KotlinCompileに関しては2通りの書き方をしているが、統一性の観点から恐らく下に合わせる方がよい
tasks.withType<KotlinCompile> {
	kotlinOptions {
		freeCompilerArgs = listOf("-Xjsr305=strict")
		jvmTarget = "1.8"
	}
}

val compileTestKotlin: KotlinCompile by tasks
compileTestKotlin.kotlinOptions {
	freeCompilerArgs = listOf("-Xjsr305=strict")
	jvmTarget = "1.8"
}

コントローラーを追加してハロワ

追加するコントローラーは以前自分が書いた記事のものを流用します。

MyController.kt
import org.springframework.ui.Model
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping("my")
class MyController{
    @GetMapping
    fun myGetTest(model: Model): String{
        return "hello from spring boot"
    }
}

以下2つのdependencyが無ければコンパイルが通らない or WARNが出るので追加します。

追加するdependency
implementation(group = "org.springframework.boot", name = "spring-boot-starter-web")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")

これを動かした上でhttp://localhost:8080/myへアクセスすることでハロワできます。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?