在pom.xml文件中,将打包方式改为war:

    1. <packaging>war</packaging>

    然后添加如下的Tomcat依赖配置,覆盖Spring Boot自带的Tomcat依赖:

    1. <dependency>
    2. <groupId>org.springframework.boot</groupId>
    3. <artifactId>spring-boot-starter-tomcat</artifactId>
    4. <scope>provided</scope>
    5. </dependency>

    <build></build>标签内配置项目名(该配置类似于server.context-path=mrbird):

    1. ...
    2. <build>
    3. ...
    4. <finalName>mySpring</finalName>
    5. </build>
    6. ...

    添加启动类ServletInitializer:

    1. import org.springframework.boot.builder.SpringApplicationBuilder;
    2. import org.springframework.boot.web.support.SpringBootServletInitializer;
    3. public class ServletInitializer extends SpringBootServletInitializer {
    4. @Override
    5. protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    6. return application.sources(Application.class);
    7. }
    8. }

    其中Application为Spring Boot的启动类。

    准备完毕后,运行mvn clean package命令即可在target目录下生产war包:

    Screen Shot 2021-10-16 at 5.40.06 PM.png