代码改变世界

MANIFEST.MF是个什么?

2020-05-29 18:00  tony4geek  阅读(3688)  评论(0编辑  收藏  举报
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.MFmaven文件夹

maven文件夹下面包含pom.xmlpom.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: http://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