软件编程
位置:首页>> 软件编程>> java编程>> SpringBoot中打war包需要注意事项

SpringBoot中打war包需要注意事项

作者:Asurplus、  发布时间:2023-11-05 19:44:42 

标签:SpringBoot,打war包

最近在做一个项目,遇到了项目打成 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" 的方式,去引入静态资源文件

八、测试

我们可以不使用项目的启动类启动项目,我们自己添加一个服务器来启动项目

SpringBoot中打war包需要注意事项

就想普通的 SSM 项目,添加一个 Tomcat 启动项目,如果能够成功启动项目,并能正常访问,那么打成 war 包也能够正常运行

来源:https://blog.csdn.net/qq_40065776/article/details/106752325

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com