我使用Spring Boot 2(Kotlin)+ Gradle Kotlin DSL尝试实现了多模块

背景- 需要一個中文版本

我在Java中有很多机会使用SpringBoot,
而且Spring Initializr现在也可以选择Kotlin,
所以我打算有一天试着用Kotlin编写。
但实际尝试时,我遇到了一些困扰,
主要是关于gradle中多模块的写法,
所以我将其记录下来作为备忘。

Gradle Kotlin DSL 是什么?

可以使用Kotlin编写Gradle构建脚本。
有关详细信息,请参考这里。
在Spring Initializr中创建项目时,默认使用Kotlin DSL编写。

版本

softwareversionSpring Boot2.4.1JDKCorretto-11.0.9.12.1Kotlin1.4.21Gradle6.7.1

多模块结构

MultiModuleImage.png
.
├── build.gradle.kts
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── practice-batch
│   ├── build.gradle.kts
│   └── src
│       ├── main
│       │   ├── kotlin
│       │   │   └── com
│       │   │       └── example
│       │   │           └── practice
│       │   │               └── batch
│       │   │                   └── PracticeBatchApplication.kt
│       │   └── resources
│       │       └── application.yml
│       └── test
│           └── kotlin
│               └── com
│                   └── example
│                       └── practice
│                           └── batch
│                               └── PracticeBatchApplicationTests.kt
├── practice-core
│   ├── build.gradle.kts
│   └── src
│       ├── main
│       │   └── kotlin
│       └── test
│           ├── kotlin
│           │   └── com
│           │       └── example
│           │           └── practice
│           │               └── core
│           │                   ├── PracticeCoreApplication.kt
│           │                   └── PracticeCoreApplicationTests.kt
│           └── resources
│               └── application.yml
├── practice-web
│   ├── backend
│   │   └── src
│   │       ├── main
│   │       │   ├── kotlin
│   │       │   │   └── com
│   │       │   │       └── example
│   │       │   │           └── practice
│   │       │   │               └── web
│   │       │   │                   └── PracticeWebApplication.kt
│   │       │   └── resources
│   │       │       └── application.yml
│   │       └── test
│   │           └── kotlin
│   │               └── com
│   │                   └── example
│   │                       └── practice
│   │                           └── web
│   │                               └── PracticeWebApplicationTests.kt
│   └── build.gradle.kts
└── settings.gradle.kts

建筑脚本

构建.gradle.kts

根据您的要求,以下是对”ルートプロジェクトのビルドスクリプト”进行汉语的同义转述:
“构建根项目的脚本。”

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import org.springframework.boot.gradle.plugin.SpringBootPlugin
import org.springframework.boot.gradle.tasks.bundling.BootJar

plugins {
  id("java")
  id("org.springframework.boot") version "2.4.1"
  id("io.spring.dependency-management") version "1.0.10.RELEASE"
  kotlin("jvm") version "1.4.21"
  kotlin("plugin.spring") version "1.4.21"
}

tasks.getByName<BootJar>("bootJar") {
  enabled = false  // ルートプロジェクトで実行Jarを作成しない設定
}

tasks.getByName<Jar>("jar") {
  enabled = false  // ルートプロジェクトでJarを作成しない設定
}

allprojects {
  repositories {
    mavenCentral()
  }
}

subprojects {
  group = "com.example"
  version = "0.0.1-SNAPSHOT"

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

  dependencyManagement {
    imports {
      mavenBom(SpringBootPlugin.BOM_COORDINATES)
    }
  }

  java.sourceCompatibility = JavaVersion.VERSION_11
  java.targetCompatibility = JavaVersion.VERSION_11

  configurations {
    compileOnly {
      extendsFrom(configurations.annotationProcessor.get())
    }
  }

  dependencies {
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    // 以下、JUnit5の設定を入れておく
    testImplementation("org.springframework.boot:spring-boot-starter-test") {
      exclude("junit")
    }
    testImplementation("org.junit.jupiter:junit-jupiter-api")
    testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
  }

  tasks.withType<KotlinCompile> {
    kotlinOptions {
      freeCompilerArgs = listOf("-Xjsr305=strict")
      jvmTarget = "11"
    }
  }

  tasks.withType<Test> {
    useJUnitPlatform()
  }
}

设置.gradle.kts

rootProject.name = "practice-parent"

include(":practice-core")
include(":practice-batch")
include(":practice-web")
include(":practice-web:backend")

练习核心/build.gradle.kts

可以实施SpringBoot组件的共享库项目的构建脚本。

import org.springframework.boot.gradle.tasks.bundling.BootJar

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

dependencies {
  implementation("org.springframework.boot:spring-boot-starter")
}

tasks.getByName<BootJar>("bootJar") {
  enabled = false  // practice-coreで実行Jarを作成しない設定
}

tasks.getByName<Jar>("jar") {
  enabled = true
}

练习-网页/build.gradle.kts

如果有多个模块需要组合,可以提前准备好。

tasks.getByName<Jar>("jar") {
  enabled = false
}

练习-网页/后端/构建.gradle.kts

依赖于 Practice-core 的 Web 项目构建脚本。

import org.springframework.boot.gradle.tasks.bundling.BootJar

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

dependencies {
  implementation(project(":practice-core"))
  implementation("org.springframework.boot:spring-boot-starter-web")
}

tasks.getByName<BootJar>("bootJar") {
  // 実行Jar用のMainクラスを設定する
  mainClass.set("com.example.practice.web.PracticeWebApplication")
}

实践-批量/构建.gradle.kts

依赖于Practice-core的批处理项目的构建脚本

import org.springframework.boot.gradle.tasks.bundling.BootJar

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

dependencies {
  implementation(project(":practice-core"))
  implementation("org.springframework.boot:spring-boot-starter")
}

tasks.getByName<BootJar>("bootJar") {
  // 実行Jar用のMainクラスを設定する
  mainClass.set("com.example.practice.batch.PracticeBatchApplication")
}

代码 (daima)

在中国,将以下内容用中文翻译为:

对练习核心进行单元测试,路径为`practice-core/src/test/・・・/PracticeCoreApplication.kt`。

由于是一个库项目,所以在src/main目录下不需要包含具有主方法的类。
为了能够执行SpringBootTest,需要在src/test目录下放置具有主方法的类。
主方法应直接使用通过Spring Initializr创建的代码。

package com.example.practice.core

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication

@SpringBootApplication
class PracticeCoreApplication

fun main(args: Array<String>) {
  runApplication<PracticeCoreApplication>(*args)
}

练习-web/src/main/・・・/练习Web应用程序.kt

为了将SpringBoot应用程序作为启动,将带有主方法的类放置在src/main目录下。
为了能够通过java -jar执行,对practice-core更改了主方法的编写方式。

package com.example.practice.web

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.context.annotation.ComponentScan

@SpringBootApplication
@ComponentScan("com.example.practice")
class PracticeWebApplication {

  companion object {
    @JvmStatic fun main(args: Array<String>) {
      runApplication<PracticeWebApplication>(*args)
    }
  }
}

请原生地用中文转述以下内容,只需要一种选项:

请寻找位于practice-batch/src/main/・・・/PracticeBatchApplication.kt文件中的内容。

与PracticeWebApplication类似,因此省略不提。

总结

暂时使用这个版本进行构建就可以了。
作为多模块的起点,我认为已经足够了。
就这些了。

参考网站

    • マルチモジュールプロジェクトの作成

 

    • Kotlin で Spring Boot Web アプリケーションの作成

 

    kotlin×SpringBootで作ったアプリケーションの実行可能jarファイル作成方法
bannerAds