Kotlin + Spring Boot + Gradle + IntelliJ 的 build.gradle.kts 笔记
Kotlin新手努力地写给自己的备忘录。
根据我个人为学习而创建的build.gradle.kts文件来写。
如果有错误,请指正,谢谢。
将随时更新。
构建.gradle.kts
插件
当Gralde构建项目时,在执行构建脚本(build.gradle)时需要指定插件。这个代码块主要用于指定构建脚本要使用的依赖关系,并且在此指定的插件不能在项目的编码中使用。您可以指定Gradle提供的插件。
plugins {
// Gradle Application Plugin: JVMアプリケーションのローカル実行やビルドを補助するためのプラグイン
application
// IDEA Plugin: IntelliJ IDEAが使用すファイルを生成し、プロジェクトをIDEAで開けるようにする。
// またいくつかのタスクも追加する。
idea
// kotlinメソッドはKotlin Teamが提供するプラグインの指定。
// プラグイン名の"org.jetbrains.kotlin."を省略して書く。
kotlin("jvm") version "1.8.21"
// version でプラグインのバージョンを指定する。
kotlin("plugin.spring") version "1.8.21"
kotlin("plugin.jpa") version "1.8.21"
kotlin("plugin.serialization") version "1.8.21"
// idメソッドはKotlin Team以外が提供するプラグインの指定。
// フルパスで書く必要がある。
id("org.springframework.boot") version "3.1.0"
id("io.spring.dependency-management") version "1.0.12.RELEASE"
id("com.palantir.git-version") version "3.0.0"
id("org.flywaydb.flyway") version "9.4.0"
}
脚本构建
Gradle构建脚本(也就是build.gradle.kts)用于指定其自身的依赖关系。
这仅仅是用于构建脚本使用的依赖,因此在此指定的内容不能在项目的编码中使用。
这里会放置repositories块和dependencies块,详细内容将在后面介绍。
指定来自除Gradle之外的仓库发布的插件。
buildscript{
// ビルド時に使うリポジトリはこっから取ってこいということ。
repositories {
mavenCentral()
}
// 使いたいリポジトリ
dependencies {
classpath("mysql:mysql-connector-java:8.0.32")
}
}
repositories
为了解决项目的依赖关系,需要指定一个外部仓库来获取库。
可以指定多个。
优先使用上面的仓库,如果上面的仓库没有所需的库,则从下面的仓库进行搜索获取。
repositories {
// 基本こいつで良さそう。
// 必要なライブラリが公開されているリポジトリを追加する。
mavenCentral()
}
dependencies
在编译时需指定所需的库。
dependencies {
classpath("mysql:mysql-connector-java:8.0.32")
}
此外,存储库和依赖项不仅在构建过程中使用,也在项目本身的依赖关系中使用(如下所述)。
所有项目
在根项目和子项目中描述共同的设置。
当多项目中需要统一设置时使用。
allprojects {
// groupとversionはプロジェクトを識別するためのもの。
// group: マルチプロジェクトの共通したパッケージ
group = "jp.co.hogehoge"
// プロジェクト自体のバージョン。gitVersion()でgitと連携させると手作業にならずに済む。
version = gitVersion()
repositories {
mavenCentral()
}
// プロジェクト全体で使用できるタスク
tasks {
// withType<>: 特定のタスクにアクセスする。
// configureEach {}: 指定されたプロジェクトの特定のタスクに変更を加える。
withType<KotlinCompile>().configureEach {
// "-Xjsr305=strict"とすることで、null安全を機能させることができる。
kotlinOptions.freeCompilerArgs = listOf("-Xjsr305=strict")
kotlinOptions.jvmTarget = Versions.jdk
}
withType<Test>().configureEach {
// テストにはJUnitを使うよって記述。
useJUnitPlatform()
}
// 以下buildタスクに関する設定。
// buildタスクは初期ではBootJar,Jarの両方が生成される。
// BootJar: sprigBootで使用されるGradleタスクの一つ。実行jarと呼ばれるファイルを生成する。
// 実行jar(bootjar)はSpring Boot特有の設定や構造を持ったJAR(Java Archive)ファイル。
// springbootを使用したMVCアプリケーションではこちらを使用する。
withType<BootJar>().configureEach {
archiveFileName.set("hoge-sample.jar")
if (this.project == rootProject || !this.project.name.contains("fuga")) {
enabled = false
}
}
// Jar: 通常のJavaプロジェクトで使用されるタスクの1つ。
// jarファイルはプロジェクトのソースコードとリソースを含み、依存関係を含まない自己完結型のjarファイルが生成される。
withType<Jar>().configureEach {
if (this.project == rootProject) {
enabled = false
} else {
archiveBaseName.set("${rootProject.name}-${this.project.name}")
}
}
}
}
子项目 (zǐ
对整个子项目进行设置。
此外,在Gradle的多项目中不需要创建根项目,全由子项目构成的项目也可以。
subprojects {
// サブプロジェクトのコーディングで使用するプラグイン
// Gradleプラグイン
// ビルドスクリプトの依存関係以外ではpluginsブロックではなくapplyブロックを使用する。
apply {
plugin("kotlin")
plugin("kotlin-spring")
plugin("kotlinx-serialization")
plugin("org.springframework.boot")
plugin("io.spring.dependency-management")
}
// サブプロジェクトのコーディングで使用するプラグイン
// Gradle以外のプラグイン
dependencies {
// implementation: コンパイルからビルドまで必要なプラグインを記述
implementation("org.jetbrains.kotlin:kotlin-stdlib")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core")
implementation("org.springframework.boot:spring-boot-gradle-plugin:2.4.1")
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.apache.commons:commons-lang3:3.12.0")
// runtimeOnly: 実行時のみ必要なプラグインを記述
runtimeOnly("com.mysql:mysql-connector-j")
// testImplementation: テストの実装から実行まで必要なプラグインを記述
testImplementation("org.springframework.boot:spring-boot-starter-test") {
// 不要な依存関係を除いている。
exclude(group = "org.junit.vintage", module = "junit-vintage-engine")
}
testImplementation("org.mockito.kotlin:mockito-kotlin:4.0.0")
// その他主要なもの
// compileOnly: コンパイル時には必要だが、実行時には必要ないプラグインを記述。
}
// configure<DependencyManagementExtension>: 依存関係管理の拡張機能の設定。一般にはmavenBom
// を使用する際に使われる。
configure<DependencyManagementExtension> {
imports {
// mavenBom
// 親子関係にあるプロジェクトがバージョンの違う同一のモジュールに依存している場合、
// 依存バージョンの違いからモジュールの競合が発生する。
// これを防ぐためにモジュールのバージョンを管理および更新するためのもの。
mavenBom(org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES)
}
}
}
项目
项目单位的设置
// サブモジュールの設定
project(":piyo-sample") {
// プロジェクトのビルドに必要な依存関係(Gradleリポジトリ)
apply {
plugin("idea")
}
// プロジェクトのビルドに必要な依存関係(Gradleリポジトリ以外)
dependencies {
implementation("org.springframework:spring-web")
}
// tasks.: 指定したタスクの設定
tasks.bootJar {
enabled = false
}
tasks.bootRun {
enabled = false
}
tasks.jar {
enabled = true
}
}
// メインとなるモジュールの設定
project(":hoge-sample") {
apply {
plugin("idea")
}
dependencies {
// サブモジュールの依存関係もここで記述する。
implementation(project(":piyo-sample"))
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.boot:spring-boot-starter-security")
implementation("org.springframework.boot:spring-boot-starter-aop")
implementation("org.springframework.boot:spring-boot-starter-validation")
implementation("org.springframework.boot:spring-boot-starter-actuator")
implementation("com.opencsv:opencsv:5.8")
testImplementation("org.springframework.security:spring-security-test")
testImplementation("org.dbunit:dbunit:2.7.3")
testImplementation("com.github.springtestdbunit:spring-test-dbunit:1.3.0")
}
// 以下の設定をしておくと、自動ビルド有効時ファイルが変更された時点で変更点が反映される。
idea {
module {
inheritOutputDirs = false
outputDir = file("$buildDir/classes/kotlin/main")
}
}
// バージョンやグループといったビルド情報の乗ったファイルが作成され、アプリケーションに載せられる。
springBoot {
buildInfo()
}
}