在pom.xml文件中,将打包方式改为war:
<packaging>war</packaging>
然后添加如下的Tomcat依赖配置,覆盖Spring Boot自带的Tomcat依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
在<build></build>标签内配置项目名(该配置类似于server.context-path=mrbird):
...
<build>
...
<finalName>mrbird</finalName>
</build>
...
添加启动类ServletInitializer:
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
}
其中Application为Spring Boot的启动类。
准备完毕后,运行mvn clean package命令即可在target目录下生产war包: