SpringBoot项目默认是打包成jar,如果我们想把SpringBoot项目打包成war,可以按照以下步骤就行操作:

  1. 在pom文件中修改打包方式为war

    <packaging>war</packaging>
  2. 移除springboot web模块中自带的tomcat

    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>            
            <exclusions>
                <exclusion>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                    <groupId>org.springframework.boot</groupId>
                </exclusion>
            </exclusions>
    </dependency>
  3. 在pom文件中添加servlet的依赖

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
    </dependency>
  4. 在Application启动类同一包下面新建WarStarterApplication类,继承SpringBootServletInitializer类,并重写configure方法

    public class WarStarterApplication extends SpringBootServletInitializer {
        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
            //指向Application启动类
            return builder.sources(Application.class);
        }
    }
  5. maven install即可打包成war

强力小磊哥
79 声望6 粉丝

翻过一座山,就高过一座山