springboot项目打包部署(jar包,war包)到linux云服务器上

本文详细介绍了如何将SpringBoot项目打包为WAR和JAR包,并进行部署。在WAR包部署中,需修改pom.xml排除内嵌Tomcat依赖,指定启动类,并在外部Tomcat中部署。对于JAR包部署,无需额外配置,直接启动即可。对于包含JSP的项目,需使用特定版本的spring-boot-maven-plugin并将JSP文件拷贝到指定目录。
摘要由CSDN通过智能技术生成

部署

war包部署

修改项目pom.xml

<packaging>war</packaging>

在这里插入图片描述

排除springboot内嵌tomcat依赖

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-tomcat</artifactId>
 <scope>provided</scope> <!--去掉内嵌tomcat-->
</dependency>

<dependency>
 <groupId>org.apache.tomcat.embed</groupId>
 <artifactId>tomcat-embed-jasper</artifactId>
 <scope>provided</scope> <!--去掉使⽤内嵌tomcat解析jsp-->
</dependency>

在这里插入图片描述

在插件中指定⼊⼝类

 <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                    <!--增加jvm参数-->
                    <jvmArguments>-Dfile.encoding=UTF-8</jvmArguments>
                    <!--指定启动⼊⼝类-->
                    <mainClass>com.blb.emp.EmpApplication</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>

在这里插入图片描述

配置启动类

继承SpringBootServletInitializer覆盖configure⽅法参数的当前启动类的class

package com.blb.emp;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
@MapperScan("com.blb.emp.mapper")
//1.继承SpringBootServletInitializer
//2.覆盖configure⽅法

public class EmpApplication extends SpringBootServletInitializer {

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

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(EmpApplication.class);
    }
}

在这里插入图片描述
在这里插入图片描述
我这里把项目名改成了emp,可以通过finalshell或者xshell,xftp上传到云服务器

打包测试

把war包放到tomcat的webapps
目录下即可
在这里插入图片描述
在这里插入图片描述

总结

在这里插入图片描述
因为这里配置的内置的tomcat其实我们在前面已经排除了所有就不起作用了

# ⼀旦使⽤war包部署注意:
- 1. application.yml 中配置port context-path 失效
- 2. 访问时使⽤打成war包的名字和外部tomcat端⼝号进⾏访问项⽬

jar包部署

默认就是jar,所有不需要改,但是如果试了war包部署的一定别忘了

<packaging>jar</packaging>

打包

在这里插入图片描述

启动jar包

java -jar 对应jar文件名字 nohup &

如果是thymeleaf或者前后端的springboot项目这样已经部署成功了并且是后台运行

jsp的springboot项目部署

因为springboot默认不支持jsp如果用上面直接部署会访问不到404

修改插件版本
<plugins>
 <!--版本必须为1.4.2版本-->
 <plugin>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-maven-plugin</artifactId>
 <version>1.4.2.RELEASE</version>
 </plugin>
</plugins>

注意:springboot :springboot部署jsp时,插件版本必须指定为1.4.2版本,并进⾏jsp
打包配置才可以,其他版本均不⽀持!!!

指定jsp打包配置

<resources>
 <!-- 打包时将jsp⽂件拷⻉到META-INF⽬录下-->
 <resource>
 <!-- 指定resources插件处理哪个⽬录下的资源⽂件 -->
 <directory>src/main/webapp</directory>
 <!--指定必须要放在此⽬录下才能被访问到-->
 <targetPath>META-INF/resources</targetPath>
 <includes>
 <include>**/**</include>
 </includes>
 </resource>
 <resource>
 <directory>src/main/resources</directory>
 <includes>
 <include>**/**</include>
 </includes>
 <filtering>false</filtering>
 </resource>
</resources>

完整版

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>1.4.2.RELEASE</version>
                <configuration>

                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
        <resources>
            <!-- 打包时将jsp⽂件拷⻉到META-INF⽬录下-->
            <resource>
                <!-- 指定resources插件处理哪个⽬录下的资源⽂件 -->
                <directory>src/main/webapp/WEB-INF</directory>
                <!--指定必须要放在此⽬录下才能被访问到-->
                <targetPath>META-INF/resources</targetPath>
                <includes>
                    <include>**/**</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/**</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值