寫這篇文件主要記錄JRA文件裏面究竟是什麼?而後MANIFEST.MF又是什麼?Springboot 如何只有Main方法就能夠運行的?java
Java開發中JRA包中常常會看到這個文件中。Springboot打包也會生成對應的JRA,下圖咱們用maven命令直接編譯打包spring
執行mvn clean package -DskipTests=true -P test
,生成的文件以下
springboot
BOOT-INF
注意了這個是咱們本身寫的代碼生成的class和配置文件
META-INF
包含了MANIFEST.MF
和 maven
文件夾mybatis
maven文件夾下面包含pom.xml
和pom.properites
文件
pom.xml
是表明的整個項目引用的第三方jar的maven座標,如Spring 等
pom.properites
是當前執行 package 命令後打包當前項目的版本信息,
就以下面,是否是簡單明瞭呀。app
#Generated by Apache Maven #Fri May 29 16:56:23 CST 2020 version=1.0-SNAPSHOT groupId=com.xxx artifactId=xxxxxService
接下來看打包文件中的MANIFEST.MF
maven
Manifest-Version: 1.0 Implementation-Title: xxxxService Implementation-Version: 1.0-SNAPSHOT Archiver-Version: Plexus Archiver Built-By: tony Implementation-Vendor-Id: com.xx Spring-Boot-Version: 1.5.10.RELEASE Implementation-Vendor: Pivotal Software, Inc. Main-Class: org.springframework.boot.loader.JarLauncher Start-Class: com.xxx.xxxxApplication Spring-Boot-Classes: BOOT-INF/classes/ Spring-Boot-Lib: BOOT-INF/lib/ Created-By: Apache Maven 3.5.3 Build-Jdk: 1.8.0_144 Implementation-URL: http://projects.spring.io/spring-boot/xxxAdminService/
直接看上面的內容,遇到問題咱們先挑選容易的來看。spring-boot
一、 Manifest-Version
用來定義manifest文件的版本,例如:Manifest-Version: 1.0ui
二、Built-Bycode
三、Spring-Boot-Version
等等這些都是很簡單的熟悉xml
一、Implementation-Title 定義了擴展實現的標題
二、 Implementation-Version 定義擴展實現的版本
三、 Implementation-Vendor 定義擴展實現的組織
四、 Implementation-Vendor-Id 定義擴展實現的組織的標識
五、 Implementation-URL : 定義該擴展包的下載地址(URL)
一、Spring-Boot-Classes: BOOT-INF/classes/
二、Spring-Boot-Lib: BOOT-INF/lib/
一、Main-Class
org.springframework.boot.loader.JarLauncher
這個很重要,很重要,是當前JRA的啓動類, 定義jar文件的入口類,該類必須是一個可執行的類,一旦定義了該屬性便可經過 java -jar x.jar來運行該jar文件。
二、Start-Class
com.jc.xxxApplication
這個是你本身項目的啓動執行類的開始,我這裏是Springboot的main方法的開始
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.transaction.annotation.EnableTransactionManagement; @EnableScheduling @MapperScan(basePackages = "com.xxxxx.mapper") @SpringBootApplication(exclude = DruidDataSourceAutoConfigure.class) public class xxxxApplication { public static void main(String[] args) { SpringApplication.run(xxxxAdminApplication.class, args); } }
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler) // package org.springframework.boot.loader; import org.springframework.boot.loader.archive.Archive; import org.springframework.boot.loader.archive.Archive.Entry; public class JarLauncher extends ExecutableArchiveLauncher { static final String BOOT_INF_CLASSES = "BOOT-INF/classes/"; static final String BOOT_INF_LIB = "BOOT-INF/lib/"; public JarLauncher() { } protected JarLauncher(Archive archive) { super(archive); } protected boolean isNestedArchive(Entry entry) { return entry.isDirectory() ? entry.getName().equals("BOOT-INF/classes/") : entry.getName().startsWith("BOOT-INF/lib/"); } public static void main(String[] args) throws Exception { (new JarLauncher()).launch(args); } }
看到上面的代碼沒有JarLauncher
,上面的第一張截圖中紅框標出來的,
這個就是 執行java -jar
的入口。這個類裏面會加載咱們寫代碼編譯出來的文件。
我這個JAR是Springboot項目打包生成的,JarLauncher
會加載上面第二張截圖中的class 和配置文件。
你們有興趣能夠看看org/springframework/boot/loader
下面的類,這個包下面着重講解了Springboot 如何只有Main方法就能夠運行加載咱們編譯的class和配置文件。
以上就是Springboot如何優雅運行java -jar xxx.jar