简体   繁体   中英

How to build a .war file for deployment using gradle

So I've been playing around with spring boot for the first time and it works wonderfully, except I can't figure out how to build a .war file to deploy to my personal server. I've read a dozen or more blogs documentation on the Spring website but I just don't get how to build the .war.

I'm using intellij, kotlin, and gradle. Any help pointing me in the right direction would be greatly appreciated.

Here is what my gradle file looks like:

import io.spring.gradle.dependencymanagement.dsl.DependencyManagementExtension
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import org.springframework.boot.gradle.tasks.bundling.BootJar
import org.springframework.boot.gradle.tasks.bundling.BootWar

plugins {
    kotlin("plugin.jpa") version "1.2.71"
    id("org.springframework.boot") version "2.1.6.RELEASE" 
    id("io.spring.dependency-management") version "1.0.7.RELEASE"
    kotlin("jvm") version "1.2.71"
    kotlin("plugin.spring") version "1.2.71"

    war


}

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

the<DependencyManagementExtension>().apply {
    imports {
        mavenBom(org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES)
    }
}



. . . .

dependencies {
    providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
    implementation("org.springframework.boot:spring-boot-starter-data-jpa")
    implementation("org.springframework.boot:spring-boot-starter-mustache")
    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")
    developmentOnly("org.springframework.boot:spring-boot-devtools")
    runtimeOnly("com.h2database:h2")
    testImplementation("org.springframework.boot:spring-boot-starter-test")
}

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

tasks.getByName<War>("war") {
    enabled = true
}

tasks.getByName<BootJar>("bootJar") {
    classifier = "boot"
    mainClassName = "com.daniel.we_adventurers"
    launchScript()

    manifest {
        attributes("Start-Class" to "com.daniel.we_adventurersn")
    }
}



tasks.getByName<BootWar>("bootWar") {
    classifier = "boot2"
    mainClassName = "com.daniel.we_adventurers"
    launchScript()

    manifest {
        attributes("Start-Class" to "com.daniel.we_adventurers")
        attributes("Main-Class" to "org.springframework.boot.loader.PropertiesLauncher")
    }

}


}

Assuming you've created your project using https://start.spring.io/ , then simply follow the docs for creating a war archive:

https://docs.spring.io/spring-boot/docs/current/gradle-plugin/reference/htmlsingle/#packaging-executable-wars

If you did not create it with https://start.spring.io/ , then I suggest creating one from there and use it as a reference to update your current project.

I know it's been a while since you asked, but also, since I came across your question, i would say your issue is most likely related to this line:

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

Please see my answer to this other question here Spring Boot Application Deployment Issue .

It should lead you in the right direction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM