【问题标题】:Generating war for spring boot application为 Spring Boot 应用程序生成战争
【发布时间】:2017-12-21 09:33:07
【问题描述】:

我一直在关注 spring 提供的教程,似乎无法理解为什么我的 .war 文件是空的。点击here查看/下载代码。最终目标是下载此文件,构建一个 .war 文件,然后将其部署到我的 tomcat 服务器。我已将以下内容添加到 build.gradle

应用插件:“战争”

但这只会产生一个包含零类文件的战争。

build.gradle

buildscript {
repositories {
    mavenCentral()
}
dependencies {
    classpath("org.springframework.boot:spring-boot-gradle 
plugin:1.5.3.RELEASE")
}
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'war'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'

jar {
    baseName = 'gs-spring-boot'
     version =  '0.1.0'
}

repositories {
    mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    // tag::actuator[]
    compile("org.springframework.boot:spring-boot-starter-actuator")
    // end::actuator[]
    // tag::tests[]
    testCompile("org.springframework.boot:spring-boot-starter-test")
    // end::tests[]
 }

【问题讨论】:

  • 您是否也尝试过将 jar 块(带有 baseName 和版本)重命名为 war?
  • 是的,但没有区别

标签: spring tomcat spring-boot gradle war


【解决方案1】:

对于教程代码,你需要做两件事:

  1. 用于打包的war插件spring-boot ref:

    apply plugin: 'war'
    
    war {
        baseName = 'gs-spring-boot'
        version =  '0.1.0'
    }    
    
  2. 创建一个可部署的war文件spring-boot ref

    @SpringBootApplication
    public class Application extends SpringBootServletInitializer {
    
        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
            return application.sources(Application.class);
        }
    

并且还指定提供的tomcat依赖:

dependencies {
    // …
    providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
    // …
}

这是我的commit reference 和结果:

【讨论】:

  • 谢谢陈睿!我曾尝试为其提供运行时,但我不知道扩展 SpringBootServletInitializer。
猜你喜欢
  • 2017-12-03
  • 2017-03-11
  • 1970-01-01
  • 1970-01-01
  • 2014-12-31
  • 2018-06-07
  • 2022-11-10
  • 1970-01-01
  • 2020-02-26
相关资源
最近更新 更多