天天看點

Springboot使用Maven将本地依賴打包到war包背景方案

背景

1、環境Springboot項目,打包工具使用Maven

2、需要WAR包部署到國産中間件

3、項目依賴了一些JAr包,非Maven庫中依賴

方案

1、首先修改pom檔案

<dependency>
			<groupId>com.xxxx</groupId>
			<artifactId>commons-io</artifactId>
			<version>2.0</version>
			<scope>system</scope>
			<systemPath>${pom.basedir}/lib/commons-io-2.0.jar</systemPath>
		</dependency>
           

scope屬性指定為system,會讀取本地三方依賴,systemPath為JAr包路徑。

2、添加Maven plugin插件,修改POM檔案

<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<excludes>
						<exclude>
							<groupId>org.projectlombok</groupId>
							<artifactId>lombok</artifactId>
						</exclude>
					</excludes>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-war-plugin</artifactId>
				<configuration>
					<webResources>
						<resource>
							<directory>lib</directory>
							<targetPath>WEB-INF/lib/</targetPath>
						</resource>
					</webResources>
				</configuration>
			</plugin>
		</plugins>
	</build>
           

重點是第二個plugin,這個插件會将依賴的JAr包打到WAR包中的WEB-INF/lib檔案夾,這樣才能被中間件加載。

<directory>lib</directory>

是指定本地的依賴JAr包位置,配置錯誤找不到對應的包。

3、按照Springboot打WAR包的方式修改項目

Springboot使用Maven将本地依賴打包到war包背景方案
Springboot使用Maven将本地依賴打包到war包背景方案
Springboot使用Maven将本地依賴打包到war包背景方案

4、打包

Springboot使用Maven将本地依賴打包到war包背景方案