SoFunction
Updated on 2025-03-04

Springboot package JAR package slimming lib and configuration file separation method

Springboot package JAR package slimming lib and configuration file separation

Recently, during the deployment and use of the project, when transmitting JAR packets, I found that the JAR packets were a bit large and the transmission speed was a bit slow.

Based on this premise, the author explores the optimization of the packaging configuration of the project. Generally speaking, lib rarely modifys, and the configuration files will change according to the needs. If the configuration files are entered into the JAR production environment, it will be modified and other inconvenient operations.

The following configuration is in <build> <plugins>Add related configuration</plugins></build>

1. Specify the packaging environment and skip the compilation unit test

&lt;!--Define the project's compilation environment--&gt;
			&lt;plugin&gt;
				&lt;groupId&gt;&lt;/groupId&gt;
				&lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
				&lt;configuration&gt;
					&lt;source&gt;1.8&lt;/source&gt;
					&lt;target&gt;1.8&lt;/target&gt;
					&lt;encoding&gt;UTF-8&lt;/encoding&gt;
				&lt;/configuration&gt;
			&lt;/plugin&gt;
			&lt;!--Default executionsrc/test/javaTest cases under the path,Skip execution--&gt;
			&lt;plugin&gt;
				&lt;groupId&gt;&lt;/groupId&gt;
				&lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
				&lt;configuration&gt;
					&lt;skip&gt;true&lt;/skip&gt;
				&lt;/configuration&gt;
			&lt;/plugin&gt;

Packaging exclusion configuration files and libs

&lt;plugin&gt;
				&lt;groupId&gt;&lt;/groupId&gt;
				&lt;artifactId&gt;maven-jar-plugin&lt;/artifactId&gt;
				&lt;version&gt;3.2.0&lt;/version&gt;
				&lt;configuration&gt;
					&lt;!-- Don't package resource files,If the full amount is releasedJARWill not be able to enter, either--&gt;
&lt;!--					&lt;excludes&gt;--&gt;
&lt;!--						&lt;exclude&gt;*.yml&lt;/exclude&gt;--&gt;
&lt;!--						&lt;exclude&gt;*.properties&lt;/exclude&gt;--&gt;
&lt;!--					&lt;/excludes&gt;--&gt;
					&lt;archive&gt;
						&lt;manifest&gt;
							&lt;addClasspath&gt;true&lt;/addClasspath&gt;
							&lt;!--  middle Class-Path Add prefix --&gt;
							&lt;classpathPrefix&gt;lib/&lt;/classpathPrefix&gt;
							&lt;!-- jarPackage does not contain a unique version identifier --&gt;
							&lt;useUniqueVersions&gt;false&lt;/useUniqueVersions&gt;
							&lt;!--Specify the startup entry class --&gt;
							&lt;mainClass&gt;&lt;/mainClass&gt;
						&lt;/manifest&gt;
						&lt;manifestEntries&gt;
							&lt;!-- middle Class-Path Add to resource file directory --&gt;
							&lt;Class-Path&gt;./resources/&lt;/Class-Path&gt;
						&lt;/manifestEntries&gt;
					&lt;/archive&gt;
					&lt;outputDirectory&gt;${}&lt;/outputDirectory&gt;
				&lt;/configuration&gt;
			&lt;/plugin&gt;

3. If you need a full package, you can also add the following configurations

Easy to use in the development environment

			&lt;!--Full quantityJARBag,最初的打Bag方式,springbootThe default compilation plugin,默认会把所有的文件打Bag成一个jar,--&gt;
			&lt;plugin&gt;
				&lt;groupId&gt;&lt;/groupId&gt;
				&lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
				&lt;executions&gt;
					&lt;execution&gt;
						&lt;goals&gt;
							&lt;goal&gt;repackage&lt;/goal&gt;
						&lt;/goals&gt;
					&lt;/execution&gt;
				&lt;/executions&gt;
				&lt;configuration&gt;
                    &lt;!--set upspringbootStartup class orJARBag的启动类--&gt;
					&lt;mainClass&gt;&lt;/mainClass&gt;
					&lt;fork&gt;true&lt;/fork&gt;
					&lt;addResources&gt;true&lt;/addResources&gt;
					&lt;outputDirectory&gt;${}/jar&lt;/outputDirectory&gt;
				&lt;/configuration&gt;
			&lt;/plugin&gt;

4. Copy lib and configuration files separately

&lt;!--Copy the associationJARPack to the specifiedlibPackaging Directory ,targetUnder the folder under the directory,Modify as needed --&gt;
			&lt;plugin&gt;
				&lt;groupId&gt;&lt;/groupId&gt;
				&lt;artifactId&gt;maven-dependency-plugin&lt;/artifactId&gt;
				&lt;executions&gt;
					&lt;execution&gt;
						&lt;id&gt;copy-dependencies&lt;/id&gt;
						&lt;phase&gt;package&lt;/phase&gt;
						&lt;goals&gt;
							&lt;goal&gt;copy-dependencies&lt;/goal&gt;
						&lt;/goals&gt;
						&lt;configuration&gt;
							&lt;outputDirectory&gt;${}/lib/&lt;/outputDirectory&gt;
						&lt;/configuration&gt;
					&lt;/execution&gt;
				&lt;/executions&gt;
			&lt;/plugin&gt;

			&lt;!-- Copy the configuration file to the specifiedresourcesPackaging Directory ,targetUnder the folder under the directory,Modify as needed--&gt;
			&lt;plugin&gt;
				&lt;groupId&gt;&lt;/groupId&gt;
				&lt;artifactId&gt;maven-resources-plugin&lt;/artifactId&gt;
				&lt;version&gt;3.2.0&lt;/version&gt;
				&lt;executions&gt;
					&lt;execution&gt; &lt;!-- Copy the configuration file --&gt;
						&lt;id&gt;copy-resources&lt;/id&gt;
						&lt;phase&gt;package&lt;/phase&gt;
						&lt;goals&gt;
							&lt;goal&gt;copy-resources&lt;/goal&gt;
						&lt;/goals&gt;
						&lt;configuration&gt;
							&lt;resources&gt;
								&lt;resource&gt;
									&lt;directory&gt;src/main/resources&lt;/directory&gt;
									&lt;includes&gt;
										&lt;include&gt;*.yml&lt;/include&gt;
										&lt;include&gt;*.properties&lt;/include&gt;
									&lt;/includes&gt;
								&lt;/resource&gt;
							&lt;/resources&gt;
							&lt;outputDirectory&gt;${}/resources&lt;/outputDirectory&gt;
						&lt;/configuration&gt;
					&lt;/execution&gt;
				&lt;/executions&gt;
			&lt;/plugin&gt;

5. The full configuration is as follows, copy and use

Note the specification of the startup class:

The following configuration is re-entered <build>Add related configuration</build>

	&lt;!--Split configuration files andLIB,GiveJARSlimming--&gt;
		&lt;!--Start the reference command,AgainJARDirectory execution,You can also specify an absolute path:
${}Under the project pathtargetTable of contents
		:Load locallylib
		:Load locally配置
		:Specify the port
		/-/- This is escaped,The bet command needs to be removed/
		java -=lib/ -jar  /-/-=resources/ /-/-=8080
		--&gt;
		&lt;plugins&gt;
			&lt;!--Define the project's compilation environment--&gt;
			&lt;plugin&gt;
				&lt;groupId&gt;&lt;/groupId&gt;
				&lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
				&lt;configuration&gt;
					&lt;source&gt;1.8&lt;/source&gt;
					&lt;target&gt;1.8&lt;/target&gt;
					&lt;encoding&gt;UTF-8&lt;/encoding&gt;
				&lt;/configuration&gt;
			&lt;/plugin&gt;
			&lt;!--Default executionsrc/test/javaTest cases under the path,Skip execution--&gt;
			&lt;plugin&gt;
				&lt;groupId&gt;&lt;/groupId&gt;
				&lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
				&lt;configuration&gt;
					&lt;skip&gt;true&lt;/skip&gt;
				&lt;/configuration&gt;
			&lt;/plugin&gt;
			&lt;!--Full quantityJARBag,最初的beatBag方式,springbootThe default compilation plugin,默认会把所有的文件beatBag成一个jar--&gt;
			&lt;plugin&gt;
				&lt;groupId&gt;&lt;/groupId&gt;
				&lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
				&lt;executions&gt;
					&lt;execution&gt;
						&lt;goals&gt;
							&lt;goal&gt;repackage&lt;/goal&gt;
						&lt;/goals&gt;
					&lt;/execution&gt;
				&lt;/executions&gt;
				&lt;configuration&gt;
					&lt;mainClass&gt;&lt;/mainClass&gt;
					&lt;fork&gt;true&lt;/fork&gt;
					&lt;addResources&gt;true&lt;/addResources&gt;
					&lt;outputDirectory&gt;${}/jar&lt;/outputDirectory&gt;
				&lt;/configuration&gt;
			&lt;/plugin&gt;
			&lt;!-- beatJARBag --&gt;
			&lt;plugin&gt;
				&lt;groupId&gt;&lt;/groupId&gt;
				&lt;artifactId&gt;maven-jar-plugin&lt;/artifactId&gt;
				&lt;version&gt;3.2.0&lt;/version&gt;
				&lt;configuration&gt;
					&lt;!-- 不beatBag资源文件,如果放开Full quantityJAR也不会beat进去--&gt;
&lt;!--					&lt;excludes&gt;--&gt;
&lt;!--						&lt;exclude&gt;*.yml&lt;/exclude&gt;--&gt;
&lt;!--						&lt;exclude&gt;*.properties&lt;/exclude&gt;--&gt;
&lt;!--					&lt;/excludes&gt;--&gt;
					&lt;archive&gt;
						&lt;manifest&gt;
							&lt;addClasspath&gt;true&lt;/addClasspath&gt;
							&lt;!--  middle Class-Path Add prefix --&gt;
							&lt;classpathPrefix&gt;lib/&lt;/classpathPrefix&gt;
							&lt;!-- jarBag不Bag含唯一版本标识 --&gt;
							&lt;useUniqueVersions&gt;false&lt;/useUniqueVersions&gt;
							&lt;!--SpecifyspringbootStart the entry class --&gt;
							&lt;mainClass&gt;&lt;/mainClass&gt;
						&lt;/manifest&gt;
						&lt;manifestEntries&gt;
							&lt;!-- middle Class-Path 加入资源文件Table of contents --&gt;
							&lt;Class-Path&gt;./resources/&lt;/Class-Path&gt;
						&lt;/manifestEntries&gt;
					&lt;/archive&gt;
					&lt;outputDirectory&gt;${}&lt;/outputDirectory&gt;
				&lt;/configuration&gt;
			&lt;/plugin&gt;
			&lt;!--Copy the associationJARBag到SpecifylibbeatBagTable of contents ,targetTable of contents下的文件夹下,Modify as needed --&gt;
			&lt;plugin&gt;
				&lt;groupId&gt;&lt;/groupId&gt;
				&lt;artifactId&gt;maven-dependency-plugin&lt;/artifactId&gt;
				&lt;executions&gt;
					&lt;execution&gt;
						&lt;id&gt;copy-dependencies&lt;/id&gt;
						&lt;phase&gt;package&lt;/phase&gt;
						&lt;goals&gt;
							&lt;goal&gt;copy-dependencies&lt;/goal&gt;
						&lt;/goals&gt;
						&lt;configuration&gt;
							&lt;outputDirectory&gt;${}/lib/&lt;/outputDirectory&gt;
						&lt;/configuration&gt;
					&lt;/execution&gt;
				&lt;/executions&gt;
			&lt;/plugin&gt;

			&lt;!-- Copy the configuration file到SpecifyresourcesbeatBagTable of contents ,targetTable of contents下的文件夹下,Modify as needed--&gt;
			&lt;plugin&gt;
				&lt;groupId&gt;&lt;/groupId&gt;
				&lt;artifactId&gt;maven-resources-plugin&lt;/artifactId&gt;
				&lt;version&gt;3.2.0&lt;/version&gt;
				&lt;executions&gt;
					&lt;execution&gt; &lt;!-- Copy the configuration file --&gt;
						&lt;id&gt;copy-resources&lt;/id&gt;
						&lt;phase&gt;package&lt;/phase&gt;
						&lt;goals&gt;
							&lt;goal&gt;copy-resources&lt;/goal&gt;
						&lt;/goals&gt;
						&lt;configuration&gt;
							&lt;resources&gt;
								&lt;resource&gt;
									&lt;directory&gt;src/main/resources&lt;/directory&gt;
									&lt;includes&gt;
										&lt;include&gt;*.yml&lt;/include&gt;
										&lt;include&gt;*.properties&lt;/include&gt;
									&lt;/includes&gt;
								&lt;/resource&gt;
							&lt;/resources&gt;
							&lt;outputDirectory&gt;${}/resources&lt;/outputDirectory&gt;
						&lt;/configuration&gt;
					&lt;/execution&gt;
				&lt;/executions&gt;
			&lt;/plugin&gt;
		&lt;/plugins&gt;

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.