MANIFEST.MF文件全面解析

(一)簡介

        

        當我們用Jar命令打完包後,會在根目錄下面創建META-INF目錄,該目錄下面會有一些對該Jar包信息的描述,其中肯定會有一個MANIFEST.MF文件,該文件包含了該Jar包的版本、創建人和類搜索路徑等信息,當然如果是可執行Jar包,會包含Main-Class屬性,表明Main方法入口。下面是httpclient.jar中的MANIFEST.MF內容:

Manifest-Version: 1.0

Implementation-Title: HttpComponents Apache HttpClient

Implementation-Version: 4.3.1

Built-By: oleg

Specification-Vendor: The Apache Software Foundation

Created-By: Apache Maven 3.0.5

url: http://hc.apache.org/httpcomponents-client

X-Compile-Source-JDK: 1.5

Implementation-Vendor: The Apache Software Foundation

Implementation-Vendor-Id: org.apache

Build-Jdk: 1.7.0_21

Specification-Title: HttpComponents Apache HttpClient

Specification-Version: 4.3.1

Implementation-Build: tags/4.3.1-RC2/httpclient@r1528975; 2013-10-03 2

1:09:33+0200

X-Compile-Target-JDK: 1.5

Archiver-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: http://www.javayou.com

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這個類庫來獲取,代碼如下:

       
Java代碼  收藏代碼
  1. package me.simplecd;  
  2.   
  3. import java.io.File;  
  4. import java.util.Map;  
  5. import java.util.jar.Attributes;  
  6. import java.util.jar.JarFile;  
  7. import java.util.jar.Manifest;  
  8. publicclass ManifestUtil {  
  9.     publicstaticvoid main(String[] args) throws Exception {  
  10.        JarFile jar=new JarFile(new File("F:\\workspace\\simplecd\\WebContent\\WEB-INF\\lib\\ant.jar"));  
  11.        Manifest manifest = jar.getManifest();  
  12.        Attributes mainAttributes = manifest.getMainAttributes();  
  13.        for(Map.Entry<Object, Object> attrEntry : mainAttributes.entrySet()){         System.out.println("main\t"+attrEntry.getKey()+"-->"+attrEntry.getValue());  
  14.        }  
  15.        Map<String, Attributes> entries = manifest.getEntries();  
  16.        for(Map.Entry<String, Attributes> entry : entries.entrySet()){  
  17.            Attributes values = entry.getValue();  
  18.            for(Map.Entry<Object, Object> attrEntry : values.entrySet()){  
  19.             System.out.println(attrEntry.getKey()+"-->"+attrEntry.getValue());  
  20.            }  
  21.        }  
  22.     }  
  23. }  
 

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

很明顯空行上面的屬性是通過Manifest類中的getMainAttributes方法獲取的(運行結果紅色的部分),它是該文件中的主要信息,另外的是通過getEntries獲取的(運行結果中藍色的部分)。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章