• SpringBoot War打包部署


    修改打包方式

    war
    
    • 1

    修改 Servlet 容器的 scope

    
                org.springframework.boot
                spring-boot-starter-tomcat
                provided
            
    
    • 1
    • 2
    • 3
    • 4
    • 5

    由于我们会使用外部的 Tomcat,所以需要主动把嵌入式容器 spring-boot-starter-tomcat 依赖的 scope 声明为 provided,表示该依赖只用于编译、测试。

    新增一个继承类

    package org.example.springbootwar;
    
    import org.springframework.boot.builder.SpringApplicationBuilder;
    import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
    
    public class ServletInitializer extends SpringBootServletInitializer {
    
        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
            return application.sources(SpringBootWarApplication.class);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    我们需要继承 SpringBootServletInitializer 类并覆写其 configure(SpringApplicationBuilder application) 方法,通过该方法指定 @SpringBootApplication 所在类。

    修改打包名称

     <build>      
           <finalName>spring-boot-warfinalName>
     build>
    
    • 1
    • 2
    • 3

    使用 mvn 打包

    # 先执行 clean,再执行 package,并且跳过测试用例的执行
    mvn clean package -Dmaven.test.skip=true
    
    • 1
    • 2

    部署到 tomcat

    将打包好的 target/spring-boot-war.war包部署到 tomcat 的 webapp目录下。

    访问对应springboot项目

    // spring-boot-war 是 webapp目录下的包名,作为项目的根目录
    curl 'http://120.0.0.1/spring-boot-war/test'
    
    • 1
    • 2

    注意:

    如果部署成功,但Spring Boot 没有启动日志,并且访问 404,则可能有 tomcat 的版本和 spring-boot的版本不一致。

    需要查看 spring-boot-starter-web 对应的 内置tomcat 的版本。可以通过一下几个方法查看:

    • 查看依赖

      在 idea 中查看对应的 版本

      在这里插入图片描述

    • 到 mvnrepository 仓库中查看

      https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web/3.2.5
      
      • 1

      在这里插入图片描述

    Github

    完整的代码:https://github.com/chenguowei/project_java/blob/main/spring-boot-examples-demo/spring-boot-war/README.md

  • 相关阅读:
    Qt插件之Qt Designer插件实现
    各位师傅求解答,什么原因
    如何进行编译和链接操作?
    核心转储core dump
    入门四认识HTML
    android脱壳:一种使用native进行抽取壳脱壳的方法,native版本的frida-fart
    企业运维实践-Nginx使用geoip2模块并利用MaxMind的GeoIP2数据库实现处理不同国家或城市的访问最佳实践指南
    C++多线程学习(一):C++11 多线程快速入门
    JVM-GC-基础知识
    JS选择框全选效果(从入门到优化)
  • 原文地址:https://blog.csdn.net/gochenguowei/article/details/138190809