文章目录1, 项目结构2, 默认打包可执行jar启动主类JarLauncher3, 打包可执行jar启动主类PropertiesLauncher1, 项目结构springboot-tar-demo/ ├── src/ │ ├── main/ │ │ ├── assembly/ │ │ │ └── assembly.xml # 上述tar结构定义文件 │ │ ├── java/ │ │ │ └── com/my/test/ │ │ │ └── Application.java # 业务主类含main方法 │ │ └── resources/ │ │ └── application.yml # 配置文件 │ └── test/ └── pom.xml2, 默认打包可执行jar启动主类JarLauncher# 1maven打包配置buildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactIdversion2.7.14/version!-- 适配你的Spring Boot版本 --configurationarchivemanifest!-- 可选默认就是JarLauncher无需手动配 --mainClassorg.springframework.boot.loader.JarLauncher/mainClass!-- 关键添加Class-Path条目 --addClasspathtrue/addClasspath!-- 指定Class-Path中依赖的前缀 --classpathPrefixlib//classpathPrefix/manifestmanifestEntries!-- 指定业务主类 --Start-Classcom.my.test.Application/Start-Class!-- Created-By会由Maven Archiver自动生成无需手动配 --/manifestEntries/archive/configurationexecutionsexecutiongoalsgoalrepackage/goal!-- 关键打包为Spring Boot可执行JAR --/goals/execution/executions/plugin/plugins/build# 2打包后的目录结构[roothost-t1 xx]# ls -Fconfig/ lib/ logs/ xx.jar start.sh*# 3查看可执行jar包[roothost-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启动方式-不灵活[roothost-t1 xx2]# java -jar xx.jar3, 打包可执行jar启动主类PropertiesLauncher# 1maven打包配置buildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactIdversion2.7.14/versionconfigurationarchivemanifest!-- 指定启动器为PropertiesLauncher --mainClassorg.springframework.boot.loader.PropertiesLauncher/mainClass/manifest!-- 可选配置默认的外部classpath和业务主类 --manifestEntriesStart-Classcom.my.test.Application/Start-ClassSpring-Boot-Classpathlib//Spring-Boot-Classpath/manifestEntries/archive/configurationexecutionsexecutiongoalsgoalrepackage/goal/goals/execution/executions/plugin/plugins/build# 2打包后的目录结构[roothost-t1 xx2]# ls -Fconfig/ lib/ start.sh* xx2.jar# 3查看可执行jar包[roothost-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启动方式-灵活[roothost-t1 xx2]#java -Dloader.pathlib/,ext-lib/ -jar xx2.jar java -Dloader.maincom.example.AnotherApp -jar xx2.jar