1、pom文件中将项目改为 war
2、添加servlet 依赖
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
3、去除内置tomcat(经实验,不去除依赖并不会影响,项目的启动,只不过多余无法使用)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
4、去除log冲突
如果你使用日志插件为log4j 而不是logbcak(springboot内置默认使用logback,以jar跑时启动不会出错,打包war使用外置tomcat时会导致栈溢出,无法启动)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
5、重写SpringBootServletInitializer ,使用servlet初始化
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; public class SpringBootStartApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
// 注意这里要指向原先用main方法执行的Application启动类
return builder.sources(DemoApplication.class);
}
}
6、执行maven 打包 clean package -Dmaven.test.skip=true,若命令行或Terminal执行错误,可以选择idea添加 run configuration 执行命令
Terminal
run configuration