SpringBoot打包war
一、修改pom配置
1.将打包方式修改为war
<packaging>war</packaging>
2.排除springboot内置tomcat
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 排除内置容器,排除内置容器导出成war包可以让外部容器运行spring-boot项目-->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
3.添加tomcat API依赖
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
二、修改启动类
在@SpringBootApplication启动类的同级创建一个类ServletInitializer
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
//Application.class 是项目的启动类(@SpringBootApplication)
return application.sources(Application.class);
}
}
三、打包
采用maven打包就可以了