SpringBoot中打war包需要注意事项
作者:Asurplus、 时间:2023-11-05 19:44:42
最近在做一个项目,遇到了项目打成 war 包的一个问题,项目创建时选择的时 jar 包方式,后因项目部署要求,需要打成 war 包部署,遇到很多坑,在此做一下记录
一、修改打包方式
原:
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
改后:
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
二、排除内置 Tomcat
原:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
改后:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
使用 排除内置服务器
三、添加 Tomcat 依赖
用于编译和测试开发,两种方式
1、
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<!-- 该包只在编译和测试的时候使用 -->
<scope>provided</scope>
</dependency>
2、
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-servlet-api</artifactId>
<version>8.5.34</version>
<!-- 该包只在编译和测试的时候使用 -->
<scope>provided</scope>
</dependency>
四、改变项目的构造方式
原:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
改后:
<build>
<!-- 一般为你的项目名,与配置文件中的context-path保持一致 -->
<finalName>demo</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webResources>
<resource>
<directory>src/main/resources/lib</directory>
<targetPath>WEB-INF/lib/</targetPath>
<includes>
<include>**/*.jar</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>
五、修改启动类
启动类继承 SpringBootServletInitializer,并实现 configure() 方法
原:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
改后:
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;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
public class DemoApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(DemoApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
六、修改配置文件
修改 application.yml 文件,标明项目项目上下文路径 context-path
server:
servlet:
context-path: /demo
七、修改静态资源引入方式
我们使用 thymeleaf 模板引擎,引入 css、js 文件时,需要加上项目上下文路径
原:
<link rel="stylesheet" href="layui/css/layui.css" rel="external nofollow" media="all">
改后:
<link rel="stylesheet" th:href="@{/layui/css/layui.css}" rel="external nofollow" media="all">
我们需要使用 th:href="@{}" rel="external nofollow" 的方式,去引入静态资源文件
八、测试
我们可以不使用项目的启动类启动项目,我们自己添加一个服务器来启动项目
就想普通的 SSM 项目,添加一个 Tomcat 启动项目,如果能够成功启动项目,并能正常访问,那么打成 war 包也能够正常运行
来源:https://blog.csdn.net/qq_40065776/article/details/106752325
标签:SpringBoot,打war包
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java读取properties配置文件的8种方式汇总
2022-05-03 17:11:34
![](https://img.aspxhome.com/file/2023/9/96079_0s.png)
Java 日志打印的15个好建议
2021-09-08 17:57:42
![](https://img.aspxhome.com/file/2023/3/91473_0s.jpg)
C#二维码图片识别代码
2022-01-15 13:03:51
android中图形图像处理之drawable用法分析
2023-01-13 07:45:03
SpringBoot浅析安全管理之OAuth2框架
2022-10-12 17:17:04
![](https://img.aspxhome.com/file/2023/7/61847_0s.png)
C# 两种方式反编译修改源码(dnspy,ildasm & ilasm)
2022-04-29 18:48:56
![](https://img.aspxhome.com/file/2023/2/104312_0s.png)
c# 生成二维码的示例
2021-09-17 14:03:26
![](https://img.aspxhome.com/file/2023/2/79082_0s.png)
Java日常练习题,每天进步一点点(17)
2021-10-15 10:24:46
![](https://img.aspxhome.com/file/2023/0/132200_0s.png)
Android 广播大全 Intent Action 事件详解
2021-09-10 12:40:15
C# 多线程学习之基础入门
2022-11-08 05:38:13
![](https://img.aspxhome.com/file/2023/1/86961_0s.png)
如何让C#、VB.NET实现复杂的二进制操作
2023-10-14 19:22:52
![](https://img.aspxhome.com/file/2023/6/128166_0s.jpg)
Java异常处理try catch的基本使用
2023-11-24 05:04:38
基于C#对用户密码使用MD5加密与解密
2022-11-24 23:05:23
Spark JDBC操作MySQL方式详细讲解
2021-05-24 12:41:51
MyBatis SpringMVC整合实现步骤详解
2023-02-02 20:33:07
java 使用Scanner类接收从控制台输入的数据方式
2023-10-23 08:54:57
java 中http请求为了防止乱码解决方案
2023-08-09 07:59:33
c#创建圆形类Circle、矩形类实现代码
2023-08-18 19:59:12
![](https://img.aspxhome.com/file/2023/3/80123_0s.png)
C#调用Windows的API实现窗体动画
2022-09-09 13:05:22
![](https://img.aspxhome.com/file/2023/4/113764_0s.png)
Android输入框实时模糊搜索效果的示例代码
2022-09-17 02:12:10
![](https://img.aspxhome.com/file/2023/0/90120_0s.gif)