Spring Boot Application Deployment Issue

こ雲淡風輕ζ 提交于 2020-05-17 03:43:47

问题


Hi Friends,

I'm new to the spring boot framework. i have completed simple spring boot application so i'm going to deploy spring application in my server (Windows 10 64 Bits)

I'm Using Gradle as my project build tool

I'm Trying to get Jar / War File Using the following Commands

./gradlew war

./gradlew bootWar

./gradlew bootJar

I'm Successfully Getting Jar/War Files But I'm Try to Run the file using following Command

java -jar application.jar

But i'm getting the Following Error:

Exception in thread "main" java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:109)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:58)
        at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:88)
Caused by: java.lang.NoClassDefFoundError: org/springframework/boot/SpringApplication
        at com.darefriends.daregames.DaregamesApplicationKt.main(DaregamesApplication.kt:17)
        ... 8 more
Caused by: java.lang.ClassNotFoundException: org.springframework.boot.SpringApplication
        at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(LaunchedURLClassLoader.java:129)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        ... 9 more

Gradle File :

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



plugins {
    `java`
    `idea`
    id("org.springframework.boot") version "2.3.0.RC1"
    id("io.spring.dependency-management") version "1.0.9.RELEASE"
    war
    kotlin("jvm") version "1.3.72"
    kotlin("plugin.spring") version "1.3.72"
    kotlin("plugin.jpa") version "1.3.72"
}



group = "com.darefriends"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_1_8

repositories {
    mavenCentral()
    maven { url = uri("https://repo.spring.io/milestone") }
}

idea{
    module{
        inheritOutputDirs = false
        outputDir = file("$buildDir/classes/kotlin/main")
    }
}

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-data-jdbc")
    implementation("org.springframework.boot:spring-boot-starter-data-jpa")
    implementation("org.springframework.boot:spring-boot-starter-jooq")
    implementation("org.springframework.boot:spring-boot-starter-web")
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    implementation("org.liquibase:liquibase-core")
    implementation("mysql:mysql-connector-java")
    implementation("io.springfox:springfox-swagger2:2.9.2")
    implementation("io.springfox:springfox-swagger-ui:2.9.2")
    developmentOnly("org.springframework.boot:spring-boot-devtools")
    providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
    testImplementation("org.springframework.boot:spring-boot-starter-test") {
        exclude(group = "org.junit.vintage", module = "junit-vintage-engine")
    }
}


tasks.withType<Test> {
    useJUnitPlatform()
}

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

I d'not Know the What is issue. I'm Using Kotlin as My Development Language

Any One Please Help me to Fix this issue. Thanks in Advance..


回答1:


I created a quick barebones project using your exact configs and I noted that there always exists a build issue whenever the following dependency line is used;

developmentOnly("org.springframework.boot:spring-boot-devtools")

A little Background:

Before Spring Boot 2.3.0.RC1 - or at least to my knowledge as at version 2.2.7.RELEASE - there were issues that arose from the use of the developmentOnly dependency tag. The work around was to define developmentOnly in your build.gradle file as follows before including the dependency;

val developmentOnly = configurations.create("developmentOnly")
configurations.runtimeClasspath.get().extendsFrom(developmentOnly)

However with Spring Boot 2.3.0.RC1 (which is the version you are using) a fix was introduced to automatically take care of defining developmentOnly configuration. See: https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.3.0-RC1-Release-Notes#automatic-creation-of-developmentonly-configuration

Now with this fix came another complication - Used to exclude devtools, the developmentOnly configuration excludes some jar files from generated fat jars (which is what your executable jar also is). See: https://github.com/spring-projects/spring-boot/issues/21288.

Possible Solutions:

To resolve this issue and move ahead, I have two major suggestions


Option-1 - (With Spring Boot 2.3.0.RC1) - Just entirely get rid of this line;

    developmentOnly("org.springframework.boot:spring-boot-devtools")

And if you really do want to have some control over when and how devtools is used within your project, my suggestion would be to follow the instruction in Spring boot's official documentation here https://docs.spring.io/spring-boot/docs/current/gradle-plugin/reference/html/#packaging-executable-configuring-excluding-devtools which states as follows;

By default, Spring Boot’s Devtools module, org.springframework.boot:spring-boot-devtools, will be excluded from an executable jar or war. If you want to include Devtools in your archive set the excludeDevtools property to false:

In kotlin:

// if generating a jar file
tasks.getByName<BootJar>("bootJar") {
    isExcludeDevtools = false
}

// if generating a war file
tasks.getByName<BootWar>("bootWar") {
    isExcludeDevtools = false
}

Obviously, you can toggle this property as you wish.


Option-2 - Just keep the following line

    developmentOnly("org.springframework.boot:spring-boot-devtools")

But downgrade your spring boot version to the latest release version - at this moment, I think that's perhaps Spring Boot 2.2.7.RELEASE then use the popular work around by including the following definition of developmentOnly anywhere before your dependencies{...} block.

val developmentOnly = configurations.create("developmentOnly")
configurations.runtimeClasspath.get().extendsFrom(developmentOnly)


来源:https://stackoverflow.com/questions/61754163/spring-boot-application-deployment-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!