Spring Boot 技巧:使用 War 包发布

Spring Boot 技巧
  1. 修改 Spring Boot 的 Application 类,使其继承 SpringBootServletInitializer 类,并重写 configure 方法:
@SpringBootApplication
public class CmsApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(CmsApplication.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(CmsApplication.class, args);
    }
}
  1. 修改项目构建配置文件:

Maven(pom.xml):

<packaging>war</packaging>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
</dependencies>

Gradle(build.gradle):

apply plugin: 'war'

dependencies {
    providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
}
  1. 执行打包命令:

Maven:

mvn clean package

Gradle:

gradle clean
gradle bootWar

推荐阅读更多精彩内容