SpringBoot分離打Jar包的兩種配置方式

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.jar\BOOT-INF\lib,和外部依賴產生沖突 -->
					<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項目分離打包
  • maven-jar-plugin插件對scope="system"依賴的處理
  • https://docs.spring.io/spring-boot/docs/current/maven-plugin/reference/htmlsingle/
  • https://www.kancloud.cn/zhangdaiscott/jeecg-boot/3043463
  • Springboot jar包外指定配置文件及原理
  • Spring Boot之application.properites的failed to convert java.lang.String to java.lang.Integer問題解決
  • Maven maven-dependency-plugin包含本地依賴包
  • Spring Boot 分離資源文件打包
  • https://blog.csdn.net/weixin_40461281/article/details/115905734
  • https://www.jianshu.com/p/0277b6a17892

到此這篇關於SpringBoot分離打Jar包的兩種方式的文章就介紹到這瞭,更多相關SpringBoot打Jar包內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: