MANIFEST.MF文件全面解析

来源:互联网 发布:中国的穆斯林问题 知乎 编辑:程序博客网 时间:2024/06/06 02:24

当我们用Jar命令打完包后,会在根目录下面创建META-INF目录,该目录下面会有一些对该Jar包信息的描述,其中肯定会有一个MANIFEST.MF文件,该文件包含了该Jar包的版本、创建人和类搜索路径等信息,当然如果是可执行Jar包,会包含Main-Class属性,表明Main方法入口。下面是httpclient.jar中的MANIFEST.MF内容:

Manifest-Version: 1.0Implementation-Title: HttpComponents Apache HttpClientImplementation-Version: 4.3.1Built-By: olegSpecification-Vendor: The Apache Software FoundationCreated-By: Apache Maven 3.0.5url:X-Compile-Source-JDK: 1.5Implementation-Vendor: The Apache Software FoundationImplementation-Vendor-Id: org.apacheBuild-Jdk: 1.7.0_21Specification-Title: HttpComponents Apache HttpClientSpecification-Version: 4.3.1Implementation-Build: tags/4.3.1-RC2/httpclient@r1528975; 2013-10-03 21:09:33+0200X-Compile-Target-JDK: 1.5Archiver-Version: Plexus Archiver

(二)格式规则

基本格式 属性名称:(空格)属性值 ;

每行最多72个字符,换行继续必须以空格开头 ;

文件最后一定是空行 ;

Class-Path 当前路径是jar包所在目录,如果要引用当前目录下一个子目录中的jar包,使用以下格式 子目录/jar包名称 子目录/jar名称,注意多个jar包之间用空格分隔, 在任何平台上路径分割符都是 /;

(三)内容分类

如果我们把MANIFEST中的配置信息进行分类,可以归纳出下面几个大类:

(1)一般属性

Manifest-Version 用来定义manifest文件的版本,例如:Manifest-Version: 1.0

Created-By 声明该文件的生成者,一般该属性是由jar命令行工具生成的,例如:Created-By: Apache Ant 1.5.3

Signature-Version 定义jar文件的签名版本

Class-Path 应用程序或者类装载器使用该值来构建内部的类搜索路径

(2)应用程序相关属性

Main-Class 定义jar文件的入口类,该类必须是一个可执行的类,一旦定义了该属性即可通过 java -jar xxx.jar来运行该jar文件。

(3)包扩展属性

Implementation-Title 定义了扩展实现的标题

Implementation-Version 定义扩展实现的版本

Implementation-Vendor 定义扩展实现的组织

Implementation-Vendor-Id 定义扩展实现的组织的标识

Implementation-URL 定义该扩展包的下载地址(URL)

Specification-Title 定义扩展规范的标题

Specification-Version 定义扩展规范的版本

Specification-Vendor 声明了维护该规范的组织

Sealed 定义jar文件是否封存,值可以是true或者false (这点我还不是很理解)

(4)小程序(Applet)相关属性

Extendsion-List该属性指定了小程序需要的扩展信息列表,列表中的每个名字对应以下的属性

<extension>-Extension-Name 定义了Jar文件的唯一标识

<extension>-Specification-Version 定义扩展规范的版本

<extension>-Implementation-Version 定义了扩展实现的版本

<extension>-Implementation-Vendor-Id 定义了扩展实现的供应商版本编号

<extension>-Implementation-URL 该jar文件最新版本的下载地址

(5)扩展标识属性

Extension-Name该属性定义了jar文件的唯一标识符

(6)签名相关属性

签名方面的属性我们可以来参照JavaMail所提供的mail.jar中的一段

Name: javax/mail/Address.class

Digest-Algorithms: SHA MD5

SHA-Digest: AjR7RqnN//cdYGouxbd06mSVfI4=

MD5-Digest: ZnTIQ2aQAtSNIOWXI1pQpw==

这段内容定义类签名的类名、计算摘要的算法名以及对应的摘要内容(使用BASE方法进行编码)

(7)自定义属性

除了前面提到的一些属性外,你也可以在MANIFEST.MF中增加自己的属性以及响应的值,例如J2ME程序jar包中就可能包含着如下信息

MicroEdition-Configuration: CLDC-1.0

MIDlet-Name: J2ME_MOBBER Midlet Suite

MIDlet-Info-URL:

MIDlet-Icon: /icon.png

MIDlet-Vendor: Midlet Suite Vendor

MIDlet-1: mobber,/icon.png,mobber

MIDlet-Version: 1.0.0

MicroEdition-Profile: MIDP-1.0

MIDlet-Description: Communicator

(四)MANIFEST.MF信息的获取

现在我们要获取ant.jar中的MANIFEST.MF文件的信息,那么我们可以通过java.util.jar这个类库来获取,代码如下:

package me.simplecd;import java.io.File;import java.util.Map;import java.util.jar.Attributes;import java.util.jar.JarFile;import java.util.jar.Manifest;publicclass ManifestUtil {publicstaticvoid main(String[] args) throws Exception {JarFile jar=new JarFile(new File("F:\\workspace\\simplecd\\WebContent\\WEB-INF\\lib\\ant.jar"));Manifest manifest = jar.getManifest();Attributes mainAttributes = manifest.getMainAttributes();for(Map.Entry<Object, Object> attrEntry : mainAttributes.entrySet()){ System.out.println("main\t"+attrEntry.getKey()+"-->"+attrEntry.getValue());}Map<String, Attributes> entries = manifest.getEntries();for(Map.Entry<String, Attributes> entry : entries.entrySet()){Attributes values = entry.getValue();for(Map.Entry<Object, Object> attrEntry : values.entrySet()){System.out.println(attrEntry.getKey()+"-->"+attrEntry.getValue());}}}}

Ant.jar下面的MANIFEST.MF文件内容如下:

Manifest-Version: 1.0

Ant-Version: Apache Ant 1.9.2

Created-By: 1.6.0_27-b27 (Sun Microsystems Inc.)

Main-Class: org.apache.tools.ant.Main

Name: org/apache/tools/ant/

Extension-name: org.apache.tools.ant

Specification-Title: Apache Ant

Specification-Version: 1.9.2

Specification-Vendor: Apache Software Foundation

Implementation-Title: org.apache.tools.ant

Implementation-Version: 1.9.2

Implementation-Vendor: Apache Software Foundation

我们运行上面代码所得结果如下:

main Ant-Version-->Apache Ant 1.9.2

main Manifest-Version-->1.0

main Created-By-->1.6.0_27-b27 (Sun Microsystems Inc.)

main Main-Class-->org.apache.tools.ant.Main

Implementation-Vendor-->Apache Software Foundation

Implementation-Title-->org.apache.tools.ant

Specification-Title-->Apache Ant

Implementation-Version-->1.9.2

Specification-Version-->1.9.2

Extension-name-->org.apache.tools.ant

Specification-Vendor-->Apache Software Foundation