java 来自GradleKotlin的Sping Boot jar总是在启动后关闭

tv6aics1  于 2023-03-21  发布在  Java
关注(0)|答案(1)|浏览(105)

我正在使用Sping Boot 与Gradle和Kotlin,Web服务器在开发中运行得非常好,但是当构建和运行jar文件时,Spring总是自动退出:

❯ java -jar build/libs/krm-backend-0.0.1-SNAPSHOT.jar --debug

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v3.0.4)

11:26:06.966 [main] INFO com.expleo.krm.KrmApplicationKt - Starting KrmApplicationKt v0.0.1-SNAPSHOT using Java 17.0.6 with PID 13303 (/home/guillaume-expleo/Bureau/KRM/kubernetes-resource-manager/backend/krm-backend/build/libs/krm-backend-0.0.1-SNAPSHOT.jar started by guillaume-expleo in /home/guillaume-expleo/Bureau/KRM/kubernetes-resource-manager/backend/krm-backend)
11:26:06.968 [main] DEBUG com.expleo.krm.KrmApplicationKt - Running with Spring Boot v0.0.1-SNAPSHOT, Spring v0.0.1-SNAPSHOT
11:26:06.969 [main] INFO com.expleo.krm.KrmApplicationKt - No active profile set, falling back to 1 default profile: "default"
11:26:06.970 [main] DEBUG org.springframework.boot.SpringApplication - Loading source class com.expleo.krm.KrmApplication
11:26:07.014 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@475e586c
11:26:07.018 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
11:26:07.167 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerProcessor'
11:26:07.168 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory'
11:26:07.168 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
11:26:07.169 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
11:26:07.172 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalPersistenceAnnotationProcessor'
11:26:07.174 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'krmApplication'
11:26:07.178 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.boot.autoconfigure.AutoConfigurationPackages'
11:26:07.214 [main] INFO com.expleo.krm.KrmApplicationKt - Started KrmApplicationKt in 0.364 seconds (process running for 0.622)
11:26:07.225 [SpringApplicationShutdownHook] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@475e586c, started on Wed Mar 15 11:26:07 CET 2023

build.gradle.kts文件:

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    id("org.springframework.boot") version "3.0.4"
    id("io.spring.dependency-management") version "1.1.0"
    id("org.asciidoctor.jvm.convert") version "2.4.0"
    kotlin("jvm") version "1.8.10"
    kotlin("plugin.spring") version "1.8.10"
    kotlin("plugin.jpa") version "1.8.10"
}

group = "com.expleo"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_17

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

repositories {
    mavenCentral()
}

extra["snippetsDir"] = file("build/generated-snippets")

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-web")
    implementation("org.springframework.boot:spring-boot-starter-data-jpa")
    implementation("org.springframework.boot:spring-boot-starter-data-rest")
    implementation("org.springframework.boot:spring-boot-starter-jdbc")
    implementation("org.springframework.boot:spring-boot-starter-oauth2-client")
    implementation("org.springframework.boot:spring-boot-starter-security")
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
    implementation("org.flywaydb:flyway-core")
    implementation("org.jetbrains.kotlin:kotlin-reflect")

//  compileOnly("org.jetbrains.kotlin:kotlin-gradle-plugin")
//  implementation("org.springframework.session:spring-session-jdbc")
//  compileOnly("org.projectlombok:lombok")
    developmentOnly("org.springframework.boot:spring-boot-devtools")
//  runtimeOnly("org.postgresql:postgresql")
    runtimeOnly("com.h2database:h2")
    annotationProcessor("org.springframework.boot:spring-boot-configuration-processor")
    annotationProcessor("org.projectlombok:lombok")
    testImplementation("org.springframework.boot:spring-boot-starter-test")
    testImplementation("org.springframework.restdocs:spring-restdocs-mockmvc")
    testImplementation("org.springframework.security:spring-security-test")
}

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

tasks.withType<Test> {
    useJUnitPlatform()
}

tasks.withType<Jar> {
    manifest {
        attributes["Main-Class"] = "com.expleo.krm.KrmApplicationKt"
    }
    // To avoid the duplicate handling strategy error
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE

    // To add all of the dependencies
    from(sourceSets.main.get().output)

    dependsOn(configurations.runtimeClasspath)
    from({
        configurations.runtimeClasspath.get().filter { it.name.endsWith("jar") }.map { zipTree(it) }
    })
}

KrmApplication.kt

package com.expleo.krm

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

@SpringBootApplication
class KrmApplication

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

我尝试了我找到的所有方法,比如添加org.springframework.boot:spring-boot-starter-web,或者使用war,但都没有成功。我尝试了用Gradle jar、bootJar和Build进行构建。
你有办法吗?
谢谢。

j9per5c4

j9per5c41#

最后,简单地删除Jar类型的这个自定义任务解决了这个问题:

/*
tasks.withType<Jar> {
    manifest {
        attributes["Main-Class"] = "com.expleo.krm.KrmApplicationKt"
    }
    // To avoid the duplicate handling strategy error
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE

    // To add all of the dependencies
    from(sourceSets.main.get().output)

    dependsOn(configurations.runtimeClasspath)
    from({
        configurations.runtimeClasspath.get().filter { it.name.endsWith("jar") }.map { zipTree(it) }
    })
}
*/

现在我的jar正常启动,并继续运行!

相关问题