首頁 > 軟體

SpringBoot分離打Jar包的兩種設定方式

2022-11-27 14:01:36

SpringBoot分離打Jar包的兩種方式

方式一:基於maven-jar-plugin

此方式基於這個小夥伴的設定改的:https://www.jb51.net/article/188606.htm

注意

  • 這種方式打包出來的Jar基於外掛提供的類載入器啟動:org.springframework.boot.loader.PropertiesLauncher
  • 所有依賴包(包括systemScope),會通過外掛 maven-dependency-plugin 自動複製到 lib 目錄
  • 所有資原始檔,會通過外掛 maven-resources-plugin 自動複製到 config 目錄
  • 此方式打包後,需要指定引數啟動 -Dloader.path=lib路徑,config路徑
  • 打包完後部署需要的檔案清單:(在 target/ 目錄下都可以看到)
    • config/**:所有resources下的資原始檔
    • lib/**:所有lib包,包括本地依賴
    • xxx.jar:應用Jar
  • 執行:java -Dloader.path=lib,config -Dspring.profiles.active=dev -jar main.jar

簡略版設定

<project xmlns="http://maven.apache.org/POM/4.0.0"
		 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<properties>
		<!--依賴輸出目錄-->
		<lib-path>lib</lib-path>
		<!--組態檔輸出目錄-->
		<config-path>config</config-path>
		<!--jar包名稱-->
		<final-name>xxx</final-name>
		<!--指定啟動類-->
		<main-class>org.jeecg.JeecgSystemApplication</main-class>
	</properties>

	<build>
		<!--專案名稱-->
		<finalName>${final-name}</finalName>
		<plugins>
			<!--定義專案的編譯環境-->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>
			<!--maven的測試用例外掛,建議跳過。-->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<configuration>
					<skip>true</skip>
				</configuration>
			</plugin>
			<!--這個是springboot的預設編譯外掛,他預設會把所有的檔案打包成一個jar-->
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<skip>true</skip>
				</configuration>
			</plugin>
			<!-- 打自定義的JAR包 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-jar-plugin</artifactId>
				<configuration>
					<archive>
						<manifest>
							<addClasspath>true</addClasspath>
							<!-- MANIFEST.MF 中 Class-Path 加入字首 -->
							<classpathPrefix>${lib-path}/</classpathPrefix>
							<!-- jar包不包含唯一版本標識 -->
							<useUniqueVersions>false</useUniqueVersions>
							<!--指定入口類 -->
							<mainClass>${main-class}</mainClass>
						</manifest>
						<manifestEntries>
							<!--MANIFEST.MF 中 Class-Path 加入資原始檔目錄 -->
							<!--本地依賴,多個需要使用空格隔開-->
							<Class-Path>./${config-path}/ lib/zwdd-1.2.0.jar lib/spire-10.jar</Class-Path>
						</manifestEntries>
					</archive>
					<outputDirectory>${project.build.directory}</outputDirectory>
				</configuration>
			</plugin>
			<!-- 該外掛的作用是用於複製依賴的jar包到指定的資料夾裡 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-dependency-plugin</artifactId>
				<executions>
					<execution>
						<id>copy-dependencies</id>
						<phase>package</phase>
						<goals>
							<goal>copy-dependencies</goal>
						</goals>
						<configuration>
							<outputDirectory>${project.build.directory}/${lib-path}/</outputDirectory>
						</configuration>
					</execution>
				</executions>
			</plugin>
			<!-- 該外掛的作用是用於複製指定的檔案 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-resources-plugin</artifactId>
				<executions>
					<!-- 複製組態檔 -->
					<execution>
						<id>copy-resources</id>
						<phase>package</phase>
						<goals>
							<goal>copy-resources</goal>
						</goals>
						<configuration>
							<resources>
								<!--複製資原始檔到外部,注意這裡先不做filtering處理,防止某些靜態檔案損壞-->
								<resource>
									<filtering>false</filtering>
									<directory>src/main/resources</directory>
									<includes>
										<include>**/*</include>
									</includes>
								</resource>
								<!--僅針對yml組態檔filtering處理(預留位置@@等)-->
								<resource>
									<filtering>true</filtering>
									<directory>src/main/resources</directory>
									<includes>
										<include>*.yml</include>
									</includes>
								</resource>
							</resources>
							<outputDirectory>${project.build.directory}/${config-path}</outputDirectory>
						</configuration>
					</execution>
				</executions>
			</plugin>
		</plugins>
		<resources>
			<!--包含java類路徑下的資原始檔(mybatis的xml等)-->
			<resource>
				<directory>src/main/java</directory>
				<filtering>false</filtering>
				<includes>
					<include>**/*.xml</include>
					<include>**/*.json</include>
					<include>**/*.ftl</include>
				</includes>
			</resource>
			<!--排除jar包內的所有resources組態檔-->
			<resource>
				<directory>src/main/resources</directory>
				<filtering>false</filtering>
				<excludes>
					<exclude>**/*</exclude>
				</excludes>
			</resource>
			<!--注: 為了能在IDEA中跑起來,需要將所有yml組態檔打進jar包,filtering必須開啟(處理預留位置等操作)-->
			<resource>
				<directory>src/main/resources</directory>
				<filtering>true</filtering>
				<includes>
					<include>*.yml</include>
				</includes>
			</resource>
		</resources>
	</build>
</project>

完整設定(帶部分註釋)

<project xmlns="http://maven.apache.org/POM/4.0.0"
		 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<parent>
		<groupId>org.jeecgframework.boot</groupId>
		<artifactId>jeecg-boot-parent</artifactId>
		<version>2.4.0</version>
	</parent>
	<modelVersion>4.0.0</modelVersion>
	<artifactId>jeecg-boot-module-system</artifactId>
	<repositories>
		<repository>
			<id>aliyun</id>
			<name>aliyun Repository</name>
			<url>http://maven.aliyun.com/nexus/content/groups/public</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
	</repositories>
	<dependencies>
		<dependency>
			<groupId>com.spire</groupId>
			<artifactId>spire</artifactId>
			<version>10</version>
			<scope>system</scope>
			<systemPath>${project.basedir}/../lib/Spire.Doc.jar</systemPath>
		</dependency>
		<dependency>
			<groupId>com.zwdd.api</groupId>
			<artifactId>zwdd</artifactId>
			<version>1.2.0</version>
			<scope>system</scope>
			<systemPath>${project.basedir}/../lib/zwdd-sdk-java-1.2.0.jar</systemPath>
		</dependency>
	</dependencies>

	<properties>
		<!--依賴輸出目錄-->
		<lib-path>lib</lib-path>
		<!--springboot預設打包輸出目錄-->
		<jar-path>jar</jar-path>
		<!--組態檔輸出目錄-->
		<config-path>config</config-path>
		<!--jar包名稱-->
		<final-name>xxx</final-name>
		<!--指定啟動類-->
		<main-class>org.jeecg.JeecgSystemApplication</main-class>
	</properties>

	<build>
		<!--專案名稱-->
		<finalName>${final-name}</finalName>
		<plugins>
			<!--定義專案的編譯環境-->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>
			<!--maven的測試用例外掛,建議跳過。-->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<configuration>
					<skip>true</skip>
				</configuration>
			</plugin>
			<!--這個是springboot的預設編譯外掛,他預設會把所有的檔案打包成一個jar,注意這裡打包出來不會包含systemScope的jar包,有需要的話得在最後的resources裡設定-->
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<executions>
					<execution>
						<goals>
							<goal>repackage</goal>
						</goals>
					</execution>
				</executions>
				<configuration>
					<!--這裡僅展示外掛作用,直接跳過此外掛-->
					<skip>true</skip>
					<mainClass>${main-class}</mainClass>
					<fork>true</fork>
					<addResources>true</addResources>
					<!--指定啟用的組態檔application-xxx.yml-->
					<profiles>${profile.name}</profiles>
					<outputDirectory>${project.build.directory}/${jar-path}</outputDirectory>
				</configuration>
			</plugin>
			<!-- 打自定義的JAR包 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-jar-plugin</artifactId>
				<configuration>
					<!-- 不打包資原始檔(組態檔和依賴包分開),這裡設定的資源排除,僅在*.xml這類檔案萬用字元篩選生效,因此不在這裡處理 -->
					<excludes>
						<!--這種檔案方式匹配可以生效-->
						<!--<exclude>*.yml</exclude>-->
						<!--下面這種方式設定是無效的,見:https://stackoverflow.com/questions/4113697/in-maven-how-to-exclude-resources-from-the-generated-jar-->
						<!--上述問題連結中有此描述:<exclude>src/test/resources/**</exclude> doesn't work. Exclude will be applied on jar final path and should be <exclude>*.properties</exclude>-->
						<!--<exclude>src/main/resources/**</exclude>-->
					</excludes>
					<archive>
						<manifest>
							<addClasspath>true</addClasspath>
							<!-- MANIFEST.MF 中 Class-Path 加入字首 -->
							<classpathPrefix>${lib-path}/</classpathPrefix>
							<!-- jar包不包含唯一版本標識 -->
							<useUniqueVersions>false</useUniqueVersions>
							<!--指定入口類 -->
							<mainClass>${main-class}</mainClass>
						</manifest>
						<manifestEntries>
							<!--MANIFEST.MF 中 Class-Path 加入資原始檔目錄 -->
							<!--本地依賴,多個需要使用空格隔開-->
							<Class-Path>./${config-path}/ lib/zwdd-1.2.0.jar lib/spire-10.jar</Class-Path>
						</manifestEntries>
					</archive>
					<outputDirectory>${project.build.directory}</outputDirectory>
				</configuration>
			</plugin>
			<!-- 該外掛的作用是用於複製依賴的jar包到指定的資料夾裡 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-dependency-plugin</artifactId>
				<executions>
					<execution>
						<id>copy-dependencies</id>
						<phase>package</phase>
						<goals>
							<goal>copy-dependencies</goal>
						</goals>
						<configuration>
							<!--這裡可以手動新增構建id,但預設是全打包就不需要了-->
							<!--<includeArtifactIds>xxxx</includeArtifactIds>-->
							<!--預設包含所有scope,因此原生的依賴也正常複製-->
							<!--<includeScope>system</includeScope>-->
							<outputDirectory>${project.build.directory}/${lib-path}/</outputDirectory>
						</configuration>
					</execution>
				</executions>
			</plugin>
			<!-- 該外掛的作用是用於複製指定的檔案 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-resources-plugin</artifactId>
				<executions>
					<!-- 複製組態檔 -->
					<execution>
						<id>copy-resources</id>
						<phase>package</phase>
						<goals>
							<goal>copy-resources</goal>
						</goals>
						<configuration>
							<resources>
								<!--複製資原始檔到外部,注意這裡先不做filtering處理,防止某些靜態檔案損壞-->
								<resource>
									<filtering>false</filtering>
									<directory>src/main/resources</directory>
									<includes>
										<!--<include>*.yml</include>-->
										<!--把所有resources目錄下的資原始檔複製出來-->
										<include>**/*</include>
									</includes>
								</resource>
								<!--僅針對yml組態檔filtering處理(預留位置@@等)-->
								<resource>
									<filtering>true</filtering>
									<directory>src/main/resources</directory>
									<includes>
										<include>*.yml</include>
									</includes>
								</resource>
							</resources>
							<outputDirectory>${project.build.directory}/${config-path}</outputDirectory>
						</configuration>
					</execution>
				</executions>
			</plugin>
		</plugins>
		<!--手動處理資原始檔,這裡的操作都是針對最終打出的jar包內部檔案的進行引入、篩選、過濾等等,預設檔案都打進jar包內部的根路徑下,因此前面的外掛[maven-jar-plugin]中需要設定相對路徑-->
		<!--具體路徑在這裡: /project/build/plugins/[maven-jar-plugin]/configuration/archive/manifestEntries/Class-Path 新增classpath:. (注意和其它設定以空格分開)-->
		<resources>
			<!--包含java類路徑下的資原始檔(mybatis的xml等)-->
			<resource>
				<directory>src/main/java</directory>
				<filtering>false</filtering>
				<includes>
					<include>**/*.xml</include>
					<include>**/*.json</include>
					<include>**/*.ftl</include>
				</includes>
			</resource>
			<!--排除jar包內的所有resources組態檔-->
			<resource>
				<directory>src/main/resources</directory>
				<!--filtering會做處理組態檔@@預留位置等操作,但是不排除某些檔案的話可能導致壓縮損壞-->
				<filtering>false</filtering>
				<excludes>
					<exclude>**/*</exclude>
				</excludes>
			</resource>
			<!--注: 上述設定已經能夠正常分離所有設定、外部依賴、工程程式碼,啟動命令:java -jar xxx.jar-->
			<!--注: 但是工程打包後,你會發現IDEA上跑不起來(target/classes目錄下沒有組態檔)-->
			<!--注: 這裡嘗試過在IDEA啟動app時指定JVM引數(但是沒有用,誰研究過可以說下): -Dloader.path=lib,config-->
			<!--注: 為了能在IDEA中跑起來,需要將所有yml組態檔打進jar包,filtering必須開啟(處理預留位置等操作)-->
			<resource>
				<directory>src/main/resources</directory>
				<filtering>true</filtering>
				<includes>
					<include>*.yml</include>
				</includes>
			</resource>
			<!--本地依賴打進jar包,這個設定是配合spring-boot-maven-plugin外掛使用的-->
			<!--<resource>-->
			<!--	<directory>../lib/crack</directory>-->
			<!--	<targetPath>BOOT-INF/lib/</targetPath>-->
			<!--	<includes><include>**/*.jar</include></includes>-->
			<!--</resource>-->
			<!--<resource>-->
			<!--	<directory>../lib</directory>-->
			<!--	<targetPath>BOOT-INF/lib/</targetPath>-->
			<!--	<includes><include>*.jar</include></includes>-->
			<!--</resource>-->
		</resources>
	</build>
</project>

方式二:基於spring-boot-maven-plugin

注意

  • 這種方式打包出來的Jar基於外掛提供的類載入器啟動:org.springframework.boot.loader.PropertiesLauncher
  • 所有依賴包(包括systemScope),會通過外掛 maven-dependency-plugin 自動複製到 lib 目錄
  • 所有資原始檔,會通過外掛 maven-resources-plugin 自動複製到 config 目錄
  • 此方式打包後,需要指定引數啟動 -Dloader.path=lib路徑,config路徑
  • 打包完後部署需要的檔案清單:(在 target/ 目錄下都可以看到)
    • config/**:所有resources下的資原始檔
    • lib/**:所有lib包,包括本地依賴
    • xxx.jar:應用Jar
  • 執行:java -Dloader.path=lib,config -Dspring.profiles.active=dev -jar main.jar

設定參考

<project xmlns="http://maven.apache.org/POM/4.0.0"
		 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

	<build>
		<finalName>main</finalName>
		<plugins>
			<!--該外掛的作用是指定編譯設定、做預處理,如Lombok、mapstruct等框架需要預處理程式碼-->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
					<encoding>UTF-8</encoding>
					<annotationProcessorPaths>
						<path>
							<groupId>org.mapstruct</groupId>
							<artifactId>mapstruct-processor</artifactId>
							<version>1.4.1.Final</version>
						</path>
						<path>
							<groupId>org.projectlombok</groupId>
							<artifactId>lombok</artifactId>
							<version>1.18.12</version>
						</path>
					</annotationProcessorPaths>
				</configuration>
			</plugin>
			<!--該外掛的作用是打包spring-boot的jar包-->
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<!--入口其實會自動設定-->
					<mainClass>org.jeecg.JeecgSystemApplication</mainClass>
					<!--不排除的話,systemScope的依賴包會自動被此外掛打包進xxx.jarBOOT-INFlib,和外部依賴產生衝突 -->
					<includeSystemScope>false</includeSystemScope>
					<skip>false</skip>
					<!--分離Jar包-->
					<layout>ZIP</layout>
					<includes>
						<include>
							<groupId>nothing</groupId>
							<artifactId>nothing</artifactId>
						</include>
					</includes>
				</configuration>
				<executions>
					<execution>
						<goals>
							<goal>repackage</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
			<!-- 該外掛的作用是複製依賴的jar包到指定的資料夾裡 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-dependency-plugin</artifactId>
				<executions>
					<execution>
						<id>copy-dependencies</id>
						<phase>package</phase>
						<goals>
							<goal>copy-dependencies</goal>
						</goals>
						<configuration>
							<outputDirectory>${project.build.directory}/lib/</outputDirectory>
						</configuration>
					</execution>
				</executions>
			</plugin>
			<!-- 該外掛的作用是複製指定的檔案 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-resources-plugin</artifactId>
				<executions>
					<!-- 複製組態檔 -->
					<execution>
						<id>copy-resources</id>
						<phase>package</phase>
						<goals>
							<goal>copy-resources</goal>
						</goals>
						<configuration>
							<resources>
								<!--複製資原始檔到外部,注意這裡先不做filtering處理,防止某些靜態檔案損壞-->
								<resource>
									<filtering>false</filtering>
									<directory>src/main/resources</directory>
									<includes>
										<include>**/*</include>
									</includes>
								</resource>
								<!--僅針對組態檔filtering處理(預留位置@@等)-->
								<resource>
									<directory>src/main/resources</directory>
									<filtering>true</filtering>
									<includes>
										<include>*.xml</include>
										<include>*.yml</include>
										<include>*.properties</include>
									</includes>
								</resource>
							</resources>
							<outputDirectory>${project.build.directory}/config</outputDirectory>
						</configuration>
					</execution>
				</executions>
			</plugin>
		</plugins>
		<resources>
			<!--打包java路徑下的靜態檔案-->
			<resource>
				<directory>src/main/java</directory>
				<filtering>false</filtering>
				<includes>
					<include>**/*.xml</include>
					<include>**/*.json</include>
					<include>**/*.ftl</include>
				</includes>
			</resource>
			<!--注: 為了能在IDEA中跑起來,需要將所有yml組態檔打進jar包,filtering必須開啟(處理預留位置等操作)-->
			<resource>
				<directory>src/main/resources</directory>
				<filtering>true</filtering>
				<includes>
					<include>*.yml</include>
          <include>*.txt</include>
				</includes>
			</resource>
		</resources>
	</build>
</project>

附錄:參考連結

到此這篇關於SpringBoot分離打Jar包的兩種方式的文章就介紹到這了,更多相關SpringBoot打Jar包內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com