1. 程式人生 > 程式設計 >SpringBoot中打war包需要注意事項

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 包也能夠正常執行

到此這篇關於SpringBoot中打war包需要注意事項的文章就介紹到這了,更多相關SpringBoot打war包內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!