Spring Boot將項(xiàng)目打包成war包的操作方法
1 修改項(xiàng)目打包類(lèi)型
在pom.xml里,項(xiàng)目打包類(lèi)型將jar設(shè)置成war:
<packaging>war</packaging>
2 移除內(nèi)置tomcat容器
在pom.xml里設(shè)置:
<dependencies>
<!--web啟動(dòng)器依賴-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!--移除默認(rèn)啟動(dòng)容器-->
<exclusions>
<exclusion>
<artifactId>spring-boot-starter-tomcat</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>
3 添加servlet-api依賴
若項(xiàng)目的某些工具類(lèi)會(huì)用到該依賴,如果缺失,會(huì)報(bào)錯(cuò):
/tool/WebUtil.java:[6,26] 程序包javax.servlet.http不存在
需要在pom.xml里添加如下依賴:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
或者下面依賴(任選其一):
<dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-servlet-api</artifactId> <version>8.0.36</version> <scope>provided</scope> </dependency>
4 修改項(xiàng)目啟動(dòng)類(lèi)
Spring Boot入口類(lèi)必須實(shí)現(xiàn)SpringBootServletInitializer接口的configure方法才能讓外部容器運(yùn)行Spring Boot項(xiàng)目。
原入口類(lèi)MainApplication.java內(nèi)容如下:
package com.maxbill;
import com.maxbill.core.desktop.DesktopApp;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
@MapperScan("com.maxbill.base.dao")
public class MainApplication extends DesktopApp {
public static ConfigurableApplicationContext context;
public static void main(String[] args) {
//啟動(dòng)后臺(tái)服務(wù)
context = SpringApplication.run(MainApplication.class, args);
//啟動(dòng)桌面服務(wù)
launch(args);
}
}
修改后內(nèi)容如下:
package com.maxbill;
import com.maxbill.core.desktop.DesktopApp;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.boot.builder.SpringApplicationBuilder;
@SpringBootApplication
@MapperScan("com.maxbill.base.dao")
public class MainApplication extends SpringBootServletInitializer{
@Override
//修改啟動(dòng)類(lèi),繼承 SpringBootServletInitializer并重寫(xiě) configure方法
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder){
//注意這里要指向原先用main方法執(zhí)行的Application啟動(dòng)類(lèi)
return builder.sources(MainApplication.class);
}
public static ConfigurableApplicationContext context;
public static void main(String[] args) {
//啟動(dòng)后臺(tái)服務(wù)
context = SpringApplication.run(MainApplication.class, args);
//啟動(dòng)桌面服務(wù)
launch(args);
}
}
5 打包部署項(xiàng)目
maven執(zhí)行命令跳過(guò)測(cè)試打包
mvn clean package -DskipTests
build信息如下
[INFO] Building war: D:\Workspace\MaxBill-RedisPlus-master\RedisPlus\target\RedisPlus-0.0.1-SNAPSHOT.war
[INFO]
[INFO] --- spring-boot-maven-plugin:2.0.4.RELEASE:repackage (default) @ RedisPlus ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
build成功后,在項(xiàng)目target目錄下把war包部署到tomcat的webapps目錄下,例如:
D:\Program Files\Apache Software Foundation\Tomcat 8.0\webapps
啟動(dòng)tomcat服務(wù),在瀏覽器訪問(wèn)
http://localhost:[端口號(hào)]/[打包項(xiàng)目名]/
總結(jié)
以上所述是小編給大家介紹的Spring Boot將項(xiàng)目打包成war包的操作方法,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- 基于Java的打包jar、war、ear包的作用與區(qū)別詳解
- Maven項(xiàng)目打包成war包部署到Tomcat的方法
- Maven引入本地Jar包并打包進(jìn)War包中的方法
- 淺談maven的jar包和war包區(qū)別 以及打包方法
- SpringBoot項(xiàng)目打包成war包并部署在tomcat上運(yùn)行的操作步驟
- spring boot項(xiàng)目打包成war在tomcat運(yùn)行的全步驟
- SpringBoot項(xiàng)目打包war包時(shí)無(wú)法運(yùn)行問(wèn)題的解決方式
- Spring Boot打包war jar 部署tomcat
- SpringBoot項(xiàng)目如何打包成war包
- springboot文件打包成jar或war的方法
- 前端vue項(xiàng)目打包為war包的實(shí)現(xiàn)示例
相關(guān)文章
Springboot創(chuàng)建子父工程過(guò)程圖解
這篇文章主要介紹了Springboot創(chuàng)建子父工程過(guò)程圖解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02
Java實(shí)現(xiàn)Fibonacci(斐波那契)取余的示例代碼
這篇文章主要介紹了Java實(shí)現(xiàn)Fibonacci取余的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
Java使用Instant時(shí)輸出的時(shí)間比預(yù)期少了八個(gè)小時(shí)
在Java中,LocalDateTime表示沒(méi)有時(shí)區(qū)信息的日期和時(shí)間,而Instant表示基于UTC的時(shí)間點(diǎn),本文主要介紹了Java使用Instant時(shí)輸出的時(shí)間比預(yù)期少了八個(gè)小時(shí)的問(wèn)題解決,感興趣的可以了解一下2024-09-09
基于SpringBoot+Redis實(shí)現(xiàn)分布式鎖
本文主要介紹了基于SpringBoot+Redis實(shí)現(xiàn)分布式鎖,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05
Spring中BeanFactory與FactoryBean接口的區(qū)別詳解
這篇文章主要給大家介紹了關(guān)于Spring中BeanFactory與FactoryBean接口的區(qū)別的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用Spring具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
Java Class.forName()用法和newInstance()方法原理解析
這篇文章主要介紹了Java Class.forName()用法和newInstance()方法原理解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08

