• SpringBoot分离打Jar包的两种方式


    SpringBoot分离打Jar包的两种方式

    方式一:基于maven-jar-plugin

    此方式基于这个小伙伴的配置改的:https://blog.xlsea.cn/archives/710/

    注意

    • 配置中的插件 spring-boot-maven-plugin 用不到的,仅做说明放这里
    • 放了几个 systemScopedependency 作参考
    • 所有依赖包(包括systemScope),会通过插件 maven-dependency-plugin 自动复制到 lib 目录
    • 所有资源文件,会通过插件 maven-resources-plugin 自动复制到 config 目录
    • 此方式打包后,不需要再指定参数启动 -Dloader.path=lib,config
    • 打包完后部署需要的文件清单:(在 target/ 目录下都可以看到)
      • config/**:所有resources下的资源文件
      • lib/**:所有lib包,包括本地依赖
      • xxx.jar:应用Jar
    • 运行:java -jar xxx.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>liblib-path>
    		
    		<config-path>configconfig-path>
    		
    		<final-name>xxxfinal-name>
    		
    		<main-class>org.jeecg.JeecgSystemApplicationmain-class>
    	properties>
    
    	<build>
    		
    		<finalName>${final-name}finalName>
    		<plugins>
    			
    			<plugin>
    				<groupId>org.apache.maven.pluginsgroupId>
    				<artifactId>maven-compiler-pluginartifactId>
    				<configuration>
    					<source>1.8source>
    					<target>1.8target>
    					<encoding>UTF-8encoding>
    				configuration>
    			plugin>
    			
    			<plugin>
    				<groupId>org.apache.maven.pluginsgroupId>
    				<artifactId>maven-surefire-pluginartifactId>
    				<configuration>
    					<skip>trueskip>
    				configuration>
    			plugin>
    			
    			<plugin>
    				<groupId>org.springframework.bootgroupId>
    				<artifactId>spring-boot-maven-pluginartifactId>
    				<configuration>
    					<skip>trueskip>
    				configuration>
    			plugin>
    			
    			<plugin>
    				<groupId>org.apache.maven.pluginsgroupId>
    				<artifactId>maven-jar-pluginartifactId>
    				<configuration>
    					<archive>
    						<manifest>
    							<addClasspath>trueaddClasspath>
    							
    							<classpathPrefix>${lib-path}/classpathPrefix>
    							
    							<useUniqueVersions>falseuseUniqueVersions>
    							
    							<mainClass>${main-class}mainClass>
    						manifest>
    						<manifestEntries>
    							
    							
    							<Class-Path>./${config-path}/ lib/zwdd-1.2.0.jar lib/spire-10.jarClass-Path>
    						manifestEntries>
    					archive>
    					<outputDirectory>${project.build.directory}outputDirectory>
    				configuration>
    			plugin>
    			
    			<plugin>
    				<groupId>org.apache.maven.pluginsgroupId>
    				<artifactId>maven-dependency-pluginartifactId>
    				<executions>
    					<execution>
    						<id>copy-dependenciesid>
    						<phase>packagephase>
    						<goals>
    							<goal>copy-dependenciesgoal>
    						goals>
    						<configuration>
    							<outputDirectory>${project.build.directory}/${lib-path}/outputDirectory>
    						configuration>
    					execution>
    				executions>
    			plugin>
    			
    			<plugin>
    				<groupId>org.apache.maven.pluginsgroupId>
    				<artifactId>maven-resources-pluginartifactId>
    				<executions>
    					
    					<execution>
    						<id>copy-resourcesid>
    						<phase>packagephase>
    						<goals>
    							<goal>copy-resourcesgoal>
    						goals>
    						<configuration>
    							<resources>
    								
    								<resource>
    									<filtering>falsefiltering>
    									<directory>src/main/resourcesdirectory>
    									<includes>
    										<include>**/*include>
    									includes>
    								resource>
    								
    								<resource>
    									<filtering>truefiltering>
    									<directory>src/main/resourcesdirectory>
    									<includes>
    										<include>*.ymlinclude>
    									includes>
    								resource>
    							resources>
    							<outputDirectory>${project.build.directory}/${config-path}outputDirectory>
    						configuration>
    					execution>
    				executions>
    			plugin>
    		plugins>
    		<resources>
    			
    			<resource>
    				<directory>src/main/javadirectory>
    				<filtering>falsefiltering>
    				<includes>
    					<include>**/*.xmlinclude>
    					<include>**/*.jsoninclude>
    					<include>**/*.ftlinclude>
    				includes>
    			resource>
    			
    			<resource>
    				<directory>src/main/resourcesdirectory>
    				<filtering>falsefiltering>
    				<excludes>
    					<exclude>**/*exclude>
    				excludes>
    			resource>
    			
    			<resource>
    				<directory>src/main/resourcesdirectory>
    				<filtering>truefiltering>
    				<includes>
    					<include>*.ymlinclude>
    				includes>
    			resource>
    		resources>
    	build>
    
    project>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154

    完整配置(带部分注释)

    <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.bootgroupId>
    		<artifactId>jeecg-boot-parentartifactId>
    		<version>2.4.0version>
    	parent>
    	<modelVersion>4.0.0modelVersion>
    	<artifactId>jeecg-boot-module-systemartifactId>
    	<repositories>
    		<repository>
    			<id>aliyunid>
    			<name>aliyun Repositoryname>
    			<url>http://maven.aliyun.com/nexus/content/groups/publicurl>
    			<snapshots>
    				<enabled>falseenabled>
    			snapshots>
    		repository>
    	repositories>
    	<dependencies>
    		<dependency>
    			<groupId>com.spiregroupId>
    			<artifactId>spireartifactId>
    			<version>10version>
    			<scope>systemscope>
    			<systemPath>${project.basedir}/../lib/Spire.Doc.jarsystemPath>
    		dependency>
    		<dependency>
    			<groupId>com.zwdd.apigroupId>
    			<artifactId>zwddartifactId>
    			<version>1.2.0version>
    			<scope>systemscope>
    			<systemPath>${project.basedir}/../lib/zwdd-sdk-java-1.2.0.jarsystemPath>
    		dependency>
    	dependencies>
    
    	<properties>
    		
    		<lib-path>liblib-path>
    		
    		<jar-path>jarjar-path>
    		
    		<config-path>configconfig-path>
    		
    		<final-name>xxxfinal-name>
    		
    		<main-class>org.jeecg.JeecgSystemApplicationmain-class>
    	properties>
    
    	<build>
    		
    		<finalName>${final-name}finalName>
    		<plugins>
    			
    			<plugin>
    				<groupId>org.apache.maven.pluginsgroupId>
    				<artifactId>maven-compiler-pluginartifactId>
    				<configuration>
    					<source>1.8source>
    					<target>1.8target>
    					<encoding>UTF-8encoding>
    				configuration>
    			plugin>
    			
    			<plugin>
    				<groupId>org.apache.maven.pluginsgroupId>
    				<artifactId>maven-surefire-pluginartifactId>
    				<configuration>
    					<skip>trueskip>
    				configuration>
    			plugin>
    			
    			<plugin>
    				<groupId>org.springframework.bootgroupId>
    				<artifactId>spring-boot-maven-pluginartifactId>
    				<executions>
    					<execution>
    						<goals>
    							<goal>repackagegoal>
    						goals>
    					execution>
    				executions>
    				<configuration>
    					
    					<skip>trueskip>
    					<mainClass>${main-class}mainClass>
    					<fork>truefork>
    					<addResources>trueaddResources>
    					
    					<profiles>${profile.name}profiles>
    					<outputDirectory>${project.build.directory}/${jar-path}outputDirectory>
    				configuration>
    			plugin>
    			
    			<plugin>
    				<groupId>org.apache.maven.pluginsgroupId>
    				<artifactId>maven-jar-pluginartifactId>
    				<configuration>
    					
    					<excludes>
    						
    						
    						
    						
    						
    					excludes>
    					<archive>
    						<manifest>
    							<addClasspath>trueaddClasspath>
    							
    							<classpathPrefix>${lib-path}/classpathPrefix>
    							
    							<useUniqueVersions>falseuseUniqueVersions>
    							
    							<mainClass>${main-class}mainClass>
    						manifest>
    						<manifestEntries>
    							
    							
    							<Class-Path>./${config-path}/ lib/zwdd-1.2.0.jar lib/spire-10.jarClass-Path>
    						manifestEntries>
    					archive>
    					<outputDirectory>${project.build.directory}outputDirectory>
    				configuration>
    			plugin>
    			
    			<plugin>
    				<groupId>org.apache.maven.pluginsgroupId>
    				<artifactId>maven-dependency-pluginartifactId>
    				<executions>
    					<execution>
    						<id>copy-dependenciesid>
    						<phase>packagephase>
    						<goals>
    							<goal>copy-dependenciesgoal>
    						goals>
    						<configuration>
    							
    							
    							
    							
    							<outputDirectory>${project.build.directory}/${lib-path}/outputDirectory>
    						configuration>
    					execution>
    				executions>
    			plugin>
    			
    			<plugin>
    				<groupId>org.apache.maven.pluginsgroupId>
    				<artifactId>maven-resources-pluginartifactId>
    				<executions>
    					
    					<execution>
    						<id>copy-resourcesid>
    						<phase>packagephase>
    						<goals>
    							<goal>copy-resourcesgoal>
    						goals>
    						<configuration>
    							<resources>
    								
    								<resource>
    									<filtering>falsefiltering>
    									<directory>src/main/resourcesdirectory>
    									<includes>
    										
    										
    										<include>**/*include>
    									includes>
    								resource>
    								
    								<resource>
    									<filtering>truefiltering>
    									<directory>src/main/resourcesdirectory>
    									<includes>
    										<include>*.ymlinclude>
    									includes>
    								resource>
    							resources>
    							<outputDirectory>${project.build.directory}/${config-path}outputDirectory>
    						configuration>
    					execution>
    				executions>
    			plugin>
    		plugins>
    		
    		
    		<resources>
    			
    			<resource>
    				<directory>src/main/javadirectory>
    				<filtering>falsefiltering>
    				<includes>
    					<include>**/*.xmlinclude>
    					<include>**/*.jsoninclude>
    					<include>**/*.ftlinclude>
    				includes>
    			resource>
    			
    			<resource>
    				<directory>src/main/resourcesdirectory>
    				
    				<filtering>falsefiltering>
    				<excludes>
    					<exclude>**/*exclude>
    				excludes>
    			resource>
    			
    			
    			
    			
    			<resource>
    				<directory>src/main/resourcesdirectory>
    				<filtering>truefiltering>
    				<includes>
    					<include>*.ymlinclude>
    				includes>
    			resource>
    			
    			
    			
    			
    			
    			
    			
    			
    			
    			
    			
    		resources>
    	build>
    
    project>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235

    方式二:基于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>mainfinalName>
    		<plugins>
    			
    			<plugin>
    				<groupId>org.apache.maven.pluginsgroupId>
    				<artifactId>maven-compiler-pluginartifactId>
    				<configuration>
    					<source>1.8source>
    					<target>1.8target>
    					<encoding>UTF-8encoding>
    					<annotationProcessorPaths>
    						<path>
    							<groupId>org.mapstructgroupId>
    							<artifactId>mapstruct-processorartifactId>
    							<version>1.4.1.Finalversion>
    						path>
    						<path>
    							<groupId>org.projectlombokgroupId>
    							<artifactId>lombokartifactId>
    							<version>1.18.12version>
    						path>
    					annotationProcessorPaths>
    				configuration>
    			plugin>
    			
    			<plugin>
    				<groupId>org.springframework.bootgroupId>
    				<artifactId>spring-boot-maven-pluginartifactId>
    				<configuration>
    					
    					<mainClass>org.jeecg.JeecgSystemApplicationmainClass>
    					
    					<includeSystemScope>falseincludeSystemScope>
    					<skip>falseskip>
    					
    					<layout>ZIPlayout>
    					<includes>
    						<include>
    							<groupId>nothinggroupId>
    							<artifactId>nothingartifactId>
    						include>
    					includes>
    				configuration>
    				<executions>
    					<execution>
    						<goals>
    							<goal>repackagegoal>
    						goals>
    					execution>
    				executions>
    			plugin>
    			
    			<plugin>
    				<groupId>org.apache.maven.pluginsgroupId>
    				<artifactId>maven-dependency-pluginartifactId>
    				<executions>
    					<execution>
    						<id>copy-dependenciesid>
    						<phase>packagephase>
    						<goals>
    							<goal>copy-dependenciesgoal>
    						goals>
    						<configuration>
    							<outputDirectory>${project.build.directory}/lib/outputDirectory>
    						configuration>
    					execution>
    				executions>
    			plugin>
    			
    			<plugin>
    				<groupId>org.apache.maven.pluginsgroupId>
    				<artifactId>maven-resources-pluginartifactId>
    				<executions>
    					
    					<execution>
    						<id>copy-resourcesid>
    						<phase>packagephase>
    						<goals>
    							<goal>copy-resourcesgoal>
    						goals>
    						<configuration>
    							<resources>
    								
    								<resource>
    									<filtering>falsefiltering>
    									<directory>src/main/resourcesdirectory>
    									<includes>
    										<include>**/*include>
    									includes>
    								resource>
    								
    								<resource>
    									<directory>src/main/resourcesdirectory>
    									<filtering>truefiltering>
    									<includes>
    										<include>*.xmlinclude>
    										<include>*.ymlinclude>
    										<include>*.propertiesinclude>
    									includes>
    								resource>
    							resources>
    							<outputDirectory>${project.build.directory}/configoutputDirectory>
    						configuration>
    					execution>
    				executions>
    			plugin>
    		plugins>
    		<resources>
    			
    			<resource>
    				<directory>src/main/javadirectory>
    				<filtering>falsefiltering>
    				<includes>
    					<include>**/*.xmlinclude>
    					<include>**/*.jsoninclude>
    					<include>**/*.ftlinclude>
    				includes>
    			resource>
    			
    			<resource>
    				<directory>src/main/resourcesdirectory>
    				<filtering>truefiltering>
    				<includes>
    					<include>*.ymlinclude>
              <include>*.txtinclude>
    				includes>
    			resource>
    		resources>
    	build>
    project>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136

    附录:参考链接

  • 相关阅读:
    LeetCode·641.设计循环双端队列·循环双链表
    2017亚信java面试题目整理
    数据结构和算法——基于Java——3.1链表(单链表)
    湖南省2022年成人高考招生全国统一考试考生须知
    2004-2020年中小企业板上市公司财务报表股票交易董事高管1200+变量数据及说明
    vue的computed如果没有出现在模板里面,当它依赖的响应式属性发生变化,getter会触发吗?
    Chromium 历史版本离线安装包 - 下载方法
    HTML旅游景点网页作业制作——旅游中国11个页面(HTML+CSS+JavaScript)
    易点易动设备管理系统:提升生产企业设备保养效率的利器
    linux rsyslog日志采集格式设定五
  • 原文地址:https://blog.csdn.net/SerikaOnoe/article/details/128002977