前言
我們都知道 SpringBoot 是内嵌 Tomcat 伺服器,是以每次重新啟動都是用的新的 Tomcat 伺服器。這樣就會出現一些問題,所有們才需要将 SpringBoot 項目打成 war 包,放到 Tomcat 中去運作。這裡記錄一下将 Springboot項目打成 war 包的具體的步驟,以便後續參考學習。
正文
首先看看我項目引用的外部 jar 包:
第一步:修改pom.xml檔案将預設的jar方式改為war:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.icbc</groupId>
<artifactId>icbc</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
第二步:排除SpringBoot内置的Tomcat容器并添加Tomcat依賴:
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
第三步:在pom.xml裡面把以上所有引入的外部jar包加入到本地依賴裡面,
<!--引入外部jar包-->
<dependency>
<groupId>com.1</groupId>
<artifactId>out-jar-1</artifactId>
<version>1.45</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/bcprov-jdk16-1.45.jar</systemPath>
</dependency>
<dependency>
<groupId>com.2</groupId>
<artifactId>out-jar-2</artifactId>
<version>1.6</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/commons-codec-1.6.jar</systemPath>
</dependency>
<dependency>
<groupId>com.3</groupId>
<artifactId>out-jar-3</artifactId>
<version>2.5</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/commons-lang-2.5.jar</systemPath>
</dependency>
<dependency>
<groupId>com.4</groupId>
<artifactId>out-jar-4</artifactId>
<version>1.2</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/commons-logging-1.2.jar</systemPath>
</dependency>
<dependency>
<groupId>com.5</groupId>
<artifactId>out-jar-5</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/icbc-api-sdk-cop.jar</systemPath>
</dependency>
<dependency>
<groupId>com.6</groupId>
<artifactId>out-jar-6</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/icbc-api-sdk-cop-io.jar</systemPath>
</dependency>
<dependency>
<groupId>com.7</groupId>
<artifactId>out-jar-7</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/icbc-ca.jar</systemPath>
</dependency>
<dependency>
<groupId>com.8</groupId>
<artifactId>out-jar-8</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/InfosecCrypto_Java1_02_JDK14+.jar</systemPath>
</dependency>
<dependency>
<groupId>com.9</groupId>
<artifactId>out-jar-9</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/jstl.jar</systemPath>
</dependency>
<dependency>
<groupId>com.10</groupId>
<artifactId>out-jar-10</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/log4j-1.2.17.jar</systemPath>
</dependency>
<dependency>
<groupId>com.11</groupId>
<artifactId>out-jar-11</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/ojdbc14-10.2.0.3.0.jar</systemPath>
</dependency>
<dependency>
<groupId>com.12</groupId>
<artifactId>out-jar-12</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/proguard.jar</systemPath>
</dependency>
<dependency>
<groupId>com.13</groupId>
<artifactId>out-jar-13</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/sqljdbc.jar</systemPath>
</dependency>
第四步:在 plugins 标簽下添加以下配置,否則外部依賴無法打進war包
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<!--<failOnMissingWebXml>false</failOnMissingWebXml>-->
<webResources>
<resource>
<directory>/src/main/resources/lib</directory>
<targetPath>WEB-INF/lib/</targetPath>
<includes>
<include>**/*.jar</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
第五步:修改SpringBoot啟動類,首先需要繼承org.springframework.boot.web.support.SpringBootServletInitializer類,然後重寫configure類
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.cache.annotation.EnableCaching;
@SpringBootApplication
@EnableCaching
public class IcbcApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(IcbcApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(IcbcApplication.class, args);
}
}
第六步:以上步驟完成之後,就可以打包了,打包的方式可以看我這篇文章:https://blog.csdn.net/weixin_42135693/article/details/106641803
war包在本地Tomcat運作的方法
war包放到tomcat的webapps目錄下,再啟動tomcat(bin下面的startup.bat),即可自動解壓部署。