SpringBoot中打war包需要注意事項(xiàng)
最近在做一個(gè)項(xiàng)目,遇到了項(xiàng)目打成 war 包的一個(gè)問(wèn)題,項(xiàng)目創(chuàng)建時(shí)選擇的時(shí) jar 包方式,后因項(xiàng)目部署要求,需要打成 war 包部署,遇到很多坑,在此做一下記錄
一、修改打包方式
原:
<version>0.0.1-SNAPSHOT</version><packaging>jar</packaging>
改后:
<version>0.0.1-SNAPSHOT</version><packaging>war</packaging>
二、排除內(nèi)置 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>
使用 排除內(nèi)置服務(wù)器
三、添加 Tomcat 依賴
用于編譯和測(cè)試開(kāi)發(fā),兩種方式
1、
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <!-- 該包只在編譯和測(cè)試的時(shí)候使用 --> <scope>provided</scope></dependency>
2、
<dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-servlet-api</artifactId> <version>8.5.34</version> <!-- 該包只在編譯和測(cè)試的時(shí)候使用 --> <scope>provided</scope></dependency>
四、改變項(xiàng)目的構(gòu)造方式
原:
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins></build>
改后:
<build><!-- 一般為你的項(xiàng)目名,與配置文件中的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>
五、修改啟動(dòng)類
啟動(dòng)類繼承 SpringBootServletInitializer,并實(shí)現(xiàn) configure() 方法原:
import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic 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@EnableSchedulingpublic 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 文件,標(biāo)明項(xiàng)目項(xiàng)目上下文路徑 context-path
server: servlet: context-path: /demo
七、修改靜態(tài)資源引入方式
我們使用 thymeleaf 模板引擎,引入 css、js 文件時(shí),需要加上項(xiàng)目上下文路徑原:
<link rel='stylesheet' href='http://www.gepszalag.com/bcjs/layui/css/layui.css' rel='external nofollow' media='all'>
改后:
<link rel='stylesheet' th:href='http://www.gepszalag.com/bcjs/@{/layui/css/layui.css}' rel='external nofollow' media='all'>
我們需要使用 th:href='http://www.gepszalag.com/bcjs/@{}' rel='external nofollow' 的方式,去引入靜態(tài)資源文件
八、測(cè)試
我們可以不使用項(xiàng)目的啟動(dòng)類啟動(dòng)項(xiàng)目,我們自己添加一個(gè)服務(wù)器來(lái)啟動(dòng)項(xiàng)目
就想普通的 SSM 項(xiàng)目,添加一個(gè) Tomcat 啟動(dòng)項(xiàng)目,如果能夠成功啟動(dòng)項(xiàng)目,并能正常訪問(wèn),那么打成 war 包也能夠正常運(yùn)行
到此這篇關(guān)于SpringBoot中打war包需要注意事項(xiàng)的文章就介紹到這了,更多相關(guān)SpringBoot打war包內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章: