一、spring-boot-maven-plugin插件
SpringBoot项目生成可执行jar包是在maven项目的pom文件中加入了一个插件
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
该插件打出来的jar包是个fat jar,也就是包含了所有第三方依赖的jar包,该fat jar包中有除了java虚拟机以外的所有依赖,是个all-in-one jar包。
去target目录下就能看到
这个read-spring-0.0.1-SNAPSHOT.jar就是个fat jar,用解压工具能看到它的目录结构如下
fat jar 目录结构
├─BOOT-INF
│ ├─classes 这里面是我们项目自己写的代码
│ └─lib 这里面是maven pom.xml引用的所有的jar包
├─META-INF
│ ├─maven
│ ├─com.sid
│ ├─ read-spring
│ ├─ pom.properties
│ ├─ pom.xml
│ ├─MANIFEST.MF 下面会重点讲到
└─org
└─springframework
└─boot
└─loader spring boot loader相关的代码
├─archive
├─data
├─jar
└─util
└─JarLauncher.class 下面会重点讲到
二、MANIFEST.MF文件
看一看该文件的内容:
Manifest-Version: 1.0
Implementation-Title: read-spring
Implementation-Version: 0.0.1-SNAPSHOT
Archiver-Version: Plexus Archiver
Built-By: 37024
Implementation-Vendor-Id: com.sid
Spring-Boot-Version: 1.5.8.RELEASE
Implementation-Vendor: Pivotal Software, Inc.
Main-Class: org.springframework.boot.loader.JarLauncher
Start-Class: com.sid.ReadSpringApplication
Spring-Boot-Classes: BOOT-INF/classes/
Spring-Boot-Lib: BOOT-INF/lib/
Created-By: Apache Maven 3.6.3
Build-Jdk: 1.8.0_221
Implementation-URL: http://projects.spring.io/spring-boot/read-spring/
里面有两个重要的属性
1. Main-Class指向了JarLauncher,它是程序的启动入口,即JarLauncher类中的main方法才是程序的入口,在JarLauncher类中的main方法中利用反射调用定义好的Start-Class的main方法
2.Start-Class指向了SpringBoot项目中的启动类,也就是有注解@SpringBootApplication修饰的那个类
对spring boot loader相关的代码感兴趣可以看看这两篇介绍
SpringBoot源码分析之SpringBoot可执行文件解析
彻底透析SpringBoot jar可执行原理