• 将SpringBOOT项目 打成 war 包 并 部署到 Tomcat


    当前环境:Windows

    Tomcat版本:tomcat8.5

    SpringBoot版本: 2.2.3

    1. pom.xml 修改打包方式

        <packaging>warpackaging>

    2.加入SpringBoot打包插件(pom.xml)

    1. <build>
    2. <plugins>
    3. <plugin>
    4. <groupId>org.springframework.bootgroupId>
    5. <artifactId>spring-boot-maven-pluginartifactId>
    6. plugin>
    7. plugins>
    8. build>

    3. 在打包插件中加入配置SpringBoot的入口类的标签名

    1. <build>
    2. <plugins>
    3. <plugin>
    4. <groupId>org.springframework.bootgroupId>
    5. <artifactId>spring-boot-maven-pluginartifactId>
    6. <configuration>
    7. <fork>truefork>
    8. <jvmArguments>Dfile.encoding=UTF-8jvmArguments>
    9. <mainClass>com.ApplicationmainClass>
    10. configuration>
    11. plugin>
    12. plugins>
    13. build>

    4.依赖的修改(pom.xml)

    因为打war在tomcat部署,我们需要将内嵌的tomcat去掉,加入你的springboot有jsp文件的话还要将tomcat解析jsp的依赖去掉。

    1. <dependency>
    2. <groupId>org.springframework.bootgroupId>
    3. <artifactId>spring-boot-starter-webartifactId>
    4. dependency>
    5. <dependency>
    6. <groupId>org.apache.tomcat.embedgroupId>
    7. <artifactId>tomcat-embed-jasperartifactId>
    8. <scope>providedscope>
    9. dependency>
    10. <dependency>
    11. <groupId>org.springframework.bootgroupId>
    12. <artifactId>spring-boot-starter-tomcatartifactId>
    13. <scope>providedscope>
    14. dependency>

    provided : 这个scope的意思在当前环境可以使用,但是不参与打包!!!

    5. 修改主配置类(用于依赖外部tomcat)

    1. @SpringBootApplication
    2. public class Application extends SpringBootServletInitializer {
    3. @Override //这个表示使用外部的tomcat容器
    4. protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
    5. // 注意这里要指向原先用main方法执行的启动类
    6. return builder.sources(Application.class);
    7. }
    8. public static void main(String[] args) {
    9. SpringApplication.run(Application.class, args);
    10. }
    11. }

    6. 测试war包

    将war包放入tomcat下的webapps下面,我们启动tomcat:

    7. 启动tomcat

     注意:war包部署的时候,tomcat默认将你的路径变成你的war的路径。

    访问我们的测试接口

     成功

    注意:war部署的时候 tomcat默认将你的根路径变成你的war包的名称

    例如 你的war是 test.war
    那么部署的时候访问接口必须是
    http://localhost:8080/test/

    8.tomcat访问配置

    直接打包,上传到服务器的tomcat的webapps下,启动后自动会解压,这里需要注意的一点就是需要修改tomcat配置文件server.xml,添加如下内容:

    <Context path="/" docBase="redis_tools-1.0-SNAPSHOT" debug="0" privileged="true"/>

    注:redis_tools-1.0-SNAPSHOT设置为包名即可,其他的地方都无需修改,启动后访问:
    http://127.0.0.1:8080/redis_tools-1.0-SNAPSHOT

  • 相关阅读:
    《程序员面试金典(第6版)》面试题 02.05. 链表求和(构建一个新链表)
    Unity基础课程之物理引擎8-扔保龄球游戏案例(完)
    对时序数据进行分类与聚类
    做自媒体如何写好一个标题获得更多的流量
    使用java实现快速排序算法的性能测试
    【功能安全】基础了解
    uni-app系列:uni.navigateTo传值跳转
    Hadoop2.x-基础[HDFS篇](介绍、常用API、I/O操作、工作机制)
    2022 全球 AI 模型周报
    vue:基础:原理知识点
  • 原文地址:https://blog.csdn.net/weixin_42405670/article/details/127960876