You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何在Spring Boot Gradle Kotlin的build.gradle.kts中添加bootRun任务?

Set Up bootRun Task in Your Spring Boot Kotlin Gradle Project

Hey there! Let's walk through getting your Spring Boot Kotlin project configured with the bootRun task in your build.gradle.kts file. Here's a step-by-step breakdown with a complete, ready-to-use example:

1. Add Required Plugins

First, you need to include core Spring Boot and Kotlin plugins—these handle dependency management, Kotlin compilation, and automatically provide the bootRun task. Update your plugins block like this:

plugins {
    id("org.springframework.boot") version "3.2.0" // Use a version compatible with your coreServiceVersion
    id("io.spring.dependency-management") version "1.1.4"
    kotlin("jvm") version "1.9.20" // Match Kotlin version to your Spring Boot version
    kotlin("plugin.spring") version "1.9.20" // Enables Spring-specific Kotlin features like nullable annotations
}

2. Configure the bootRun Task

The bootRun task comes built-in with the Spring Boot plugin, but you can customize it (like setting the main class explicitly). Add this block after your dependencies:

tasks.bootRun {
    // Kotlin apps generate a main class with a "Kt" suffix (e.g., Application.kt → ApplicationKt)
    mainClass.set("com.yourpackage.YourApplicationKt")
    // Optional: Add JVM arguments if needed (e.g., memory settings)
    jvmArgs = listOf("-Xmx512m")
}

Pro tip: Replace com.yourpackage.YourApplicationKt with the actual fully qualified name of your Kotlin app's main class.

3. Update Dependencies

Fill in your dependencies block with Spring Boot starters and Kotlin-specific dependencies:

val coreServiceVersion = "3.0.0"

dependencies {
    // Core Spring Boot starter (adjust based on your project needs, e.g., web, data-jpa)
    implementation("org.springframework.boot:spring-boot-starter-web")
    // Kotlin essentials
    implementation(kotlin("stdlib-jdk8"))
    implementation(kotlin("reflect"))
    // Uncomment if you need to include your core service dependency
    // implementation("com.yourgroup:core-service:$coreServiceVersion")
    // Test dependencies
    testImplementation("org.springframework.boot:spring-boot-starter-test")
    testImplementation(kotlin("test-junit5"))
}

Complete build.gradle.kts Example

Putting it all together, your final file will look like this:

plugins {
    id("org.springframework.boot") version "3.2.0"
    id("io.spring.dependency-management") version "1.1.4"
    kotlin("jvm") version "1.9.20"
    kotlin("plugin.spring") version "1.9.20"
}

val coreServiceVersion = "3.0.0"

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-web")
    implementation(kotlin("stdlib-jdk8"))
    implementation(kotlin("reflect"))
    // implementation("com.yourgroup:core-service:$coreServiceVersion")
    testImplementation("org.springframework.boot:spring-boot-starter-test")
    testImplementation(kotlin("test-junit5"))
}

tasks.bootRun {
    mainClass.set("com.yourpackage.YourApplicationKt")
    // jvmArgs = listOf("-Xmx512m")
}

// Optional: Tweak Kotlin compilation settings
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
    kotlinOptions {
        jvmTarget = "17" // Match your project's JDK version
        freeCompilerArgs = listOf("-Xjsr305=strict") // Enable strict nullability checks
    }
}

tasks.test {
    useJUnitPlatform()
}

How to Run

Once everything is set up, start your app from the command line with:

./gradlew bootRun

Or use your IDE's Gradle tool window to execute the bootRun task directly.

内容的提问来源于stack exchange,提问作者Rakshith R Pai

火山引擎 最新活动