文章目录
- 1, 项目结构
- 2, 默认打包可执行jar启动主类:JarLauncher
- 3, 打包可执行jar启动主类:PropertiesLauncher
1, 项目结构
springboot-tar-demo/ ├── src/ │ ├── main/ │ │ ├── assembly/ │ │ │ └── assembly.xml # 上述tar结构定义文件 │ │ ├── java/ │ │ │ └── com/my/test/ │ │ │ └── Application.java # 业务主类(含main方法) │ │ └── resources/ │ │ └── application.yml # 配置文件 │ └── test/ └── pom.xml2, 默认打包可执行jar启动主类:JarLauncher
# 1,maven打包配置:<build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>2.7.14</version><!-- 适配你的Spring Boot版本 --><configuration><archive><manifest><!-- 可选:默认就是JarLauncher,无需手动配 --><mainClass>org.springframework.boot.loader.JarLauncher</mainClass><!-- 关键:添加Class-Path条目 --><addClasspath>true</addClasspath><!-- 指定Class-Path中依赖的前缀 --><classpathPrefix>lib/</classpathPrefix></manifest><manifestEntries><!-- 指定业务主类 --><Start-Class>com.my.test.Application</Start-Class><!-- Created-By会由Maven Archiver自动生成,无需手动配 --></manifestEntries></archive></configuration><executions><execution><goals><goal>repackage</goal><!-- 关键:打包为Spring Boot可执行JAR --></goals></execution></executions></plugin></plugins></build># 2,打包后的目录结构[root@host-t1 xx]# ls -Fconfig/ lib/ logs/ xx.jar start.sh*# 3,查看可执行jar包[root@host-t1 xx]# vim xx.jarMETA-INF/MANIFEST.MF:Manifest-Version:1.0Implementation-Title: test-Application Implementation-Version:1.2.3 Class-Path: lib/config-generator-5.6.8.jar lib/jline-3.3.0.jar lib/jac kson-databind-2.10.5.1.jar lib/jackson-annotations-2.10.5.jar lib/ja... r lib/gsjdbc4.jar Build-Jdk-Spec:1.8Created-By: Maven Archiver3.4.0 Main-Class: com.my.test.Application# 4,启动方式-不灵活:[root@host-t1 xx2]# java -jar xx.jar3, 打包可执行jar启动主类:PropertiesLauncher
# 1,maven打包配置:<build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>2.7.14</version><configuration><archive><manifest><!-- 指定启动器为PropertiesLauncher --><mainClass>org.springframework.boot.loader.PropertiesLauncher</mainClass></manifest><!-- 可选:配置默认的外部classpath和业务主类 --><manifestEntries><Start-Class>com.my.test.Application</Start-Class><Spring-Boot-Classpath>lib/</Spring-Boot-Classpath></manifestEntries></archive></configuration><executions><execution><goals><goal>repackage</goal></goals></execution></executions></plugin></plugins></build># 2,打包后的目录结构[root@host-t1 xx2]# ls -Fconfig/ lib/ start.sh* xx2.jar# 3,查看可执行jar包[root@host-t1 xx2]# vim xx2.jarMETA-INF/MANIFEST.MF: Manifest-Version:1.0# 可选:指定默认加载的外部classpath(同级lib目录,支持通配符)#Spring-Boot-Classpath: lib/# “类路径索引文件”,提前记录所有依赖的位置(可以记录一个或多个jar路径),启动时直接读取索引,无需遍历#Spring-Boot-Classpath-Index: BOOT-INF/classpath.idxImplementation-Title: boot Implementation-Version:1.2#内部的业务类(编译后的 .class 文件)、配置文件、静态资源存放路径,默认BOOT-INF/classes/Spring-Boot-Classes: BOOT-INF/classes/#内部 依赖库的存放路径(默认是 BOOT-INF/lib/)Spring-Boot-Lib: BOOT-INF/lib/ Build-Jdk-Spec:1.8Spring-Boot-Version:2.7.14 Created-By: Maven JAR Plugin3.2.2#PropertiesLauncher是 Spring Boot 内置的三种启动器之一(另外两种是 JarLauncher(默认)、WarLauncher)#核心优势是支持高度灵活的外部化配置:#无需自定义类加载器,就能动态加载外部目录(如同级 lib)的所有 JAR 包;#支持运行时指定 classpath、配置文件路径、主类等Main-Class: org.springframework.boot.loader.PropertiesLauncher#指定你的业务主类(也可运行时指定)Start-Class: com.my.test.Application# 4,启动方式-灵活:[root@host-t1 xx2]#java -Dloader.path=lib/,ext-lib/ -jar xx2.jar java -Dloader.main=com.example.AnotherApp -jar xx2.jar