SpringBoot 项目移除内嵌Tomcat并将项目打成war包

这一篇记录一下Springboot项目移除内嵌Tomcat并将项目打包成war包的简单记录,各位看到此博客的小伙伴,如有不对的地方请及时通过私信我或者评论此博客的方式指出,以免误人子弟。多谢!

移除内嵌Tomcat

加入依赖


    org.springframework.boot
    spring-boot-starter-tomcat
    provided

启动类修改

继承 SpringBootServletInitializer 重写 SpringApplicationBuilder 方法,如下:

@SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application){
        return application.sources(DemoApplication.class);
    }

}

打包成war

修改pom

只要在pom文件中定义一下packaging即可,如下:

war

完整pom文件



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.3.3.RELEASE
         
    
    com.example
    demo
    0.0.1-SNAPSHOT
    demo
    Demo project for Spring Boot
    
        1.8
    

    war

    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
        
            org.springframework.boot
            spring-boot-starter-tomcat
            provided
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


你可能感兴趣的:(springboot,spring,boot)