MANIFEST.MF是個什麼?
MANIFEST.MF是個什麼?
寫這篇文件主要記錄JRA文件裏面到底是什麼?然後MANIFEST.MF又是什麼?Springboot 如何只有Main方法就可以運行的?
Springboot項目打包
Java開發中JRA包中經常會看到這個文件中。Springboot打包也會生成對應的JRA,下圖我們用maven命令直接編譯打包
執行mvn clean package -DskipTests=true -P test
,生成的文件如下
- 這個JAR我們分兩部分來講解請看下圖
BOOT-INF
注意了這個是我們自己寫的代碼生成的class和配置文件
META-INF
包含了MANIFEST.MF
和 maven
文件夾
maven文件夾下面包含pom.xml
和pom.properites
文件
pom.xml
是代表的整個項目引用的第三方jar的maven坐標,如Spring 等
pom.properites
是當前執行 package 命令後打包當前項目的版本信息,
就如下面,是不是簡單明了呀。
#Generated by Apache Maven
#Fri May 29 16:56:23 CST 2020
version=1.0-SNAPSHOT
groupId=com.xxx
artifactId=xxxxxService
MANIFEST.MF 來源
接下來看打包文件中的MANIFEST.MF
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: //projects.spring.io/spring-boot/xxxAdminService/
直接看上面的內容,遇到問題我們先挑選容易的來看。
一般屬性
1、 Manifest-Version
用來定義manifest文件的版本,例如:Manifest-Version: 1.0
2、Built-By
3、Spring-Boot-Version
等等這些都是很簡單的熟悉
包擴展屬性
1、Implementation-Title 定義了擴展實現的標題
2、 Implementation-Version 定義擴展實現的版本
3、 Implementation-Vendor 定義擴展實現的組織
4、 Implementation-Vendor-Id 定義擴展實現的組織的標識
5、 Implementation-URL : 定義該擴展包的下載地址(URL)
項目加載文件相關屬性
1、Spring-Boot-Classes: BOOT-INF/classes/
2、Spring-Boot-Lib: BOOT-INF/lib/
應用程序相關屬性
1、Main-Class
org.springframework.boot.loader.JarLauncher
這個很重要,很重要,是當前JRA的啟動類, 定義jar文件的入口類,該類必須是一個可執行的類,一旦定義了該屬性即可通過 java -jar x.jar來運行該jar文件。
2、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