Use ANT to Build a JAR MANIFEST.MF File

This tutorial look at how to use ant to create a Jar MANIFEST.MF file, as Prerequisites, please download Ant(http://ant.apache.org/bindownload.cgi) and install it.

JAR file is a file format based on the popular ZIP file format and is used to aggregate many files into one, a JAR file is a essential zip file that contains an optional META-INF directory, the MANIFEST file is one single component of Jar file, under META-INF directory and used to describe all associated metadata of Jar file.

The MANIFEST file is represented as so-called “name: value” pairs inspired by the RFC822 standard, we also call these pairs headers or attributes, binary data of any form is represented as base64. Continuations are required for binary data which causes line length to exceed 72 bytes. Here is an example content of MANIFEST file:

MANIFEST.MF
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.6.5
Created-By: 1.5.0_07-b03 (Sun Microsystems Inc.)
Implementation-Title: ASM
Implementation-Version: 2.2.3
Implementation-Vendor: France Telecom R&D
Class-Path: com.asjava.Main
Main-Class: x-com.jar;common-io.jar

ANT supports Jar manifest task to create a manifest file, optionally replacing or updating an existing file. And you can write some attributes (e.g.Main-Class and Class-Path) to manifest.mf.

Ant build.xml to create ant manifest.mf file

<property name="build.dir" value="build"/>
<property name="build.main.classes" value="${build.dir}/main_classes"/>
<property name="dist.dir" value="dist"/>
<property name="dist.plugin.name" value="iAntAndMainfestExample.jar"/>
<property name="3rdParty" value="3rdParty"/>

<target name="dist" depends="compile">
        <mkdir dir="${dist.dir}"/>
        <delete file="${dist.dir}/${dist.plugin.name}" failonerror="false"/>
        <jar destfile="${dist.dir}/${dist.plugin.name}" basedir="${build.main.classes}">
            <manifest>
                <attribute name="Built-By" value="${user.name}"/>
                <attribute name="Implementation-Vendor" value="Asjava.com, Inc"/>
                <attribute name="Implementation-Vendor-Id" value="com.Javarmi"/>
                <attribute name="Implementation-Title" value="Implementation"/>
                <attribute name="Class-Path"
                           value="${3rdParty}\geronimo-annotation_1.0_spec-1.1.1.jar
${3rdParty}\xml-resolver-1.2.jar ${3rdParty}\XmlSchema-1.4.5.jar"/>
                <section name="com.asjava.plugin">
                    <attribute name="Module-Class" value="Plugin"/>
                </section>
            </manifest>
        </jar>
</target>

You use ant command line to excute build.xml and finally get the following JAR MANIFEST.MF File, the attribute ‘classpath’ is set in manifest file.

MANIFEST.MF
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.7.1
Created-By: 1.5.0_22-b03 (Sun Microsystems Inc.)
Built-By: Administrator
Implementation-Vendor: Asjava.com, Inc
Implementation-Vendor-Id: com.Javarmi
Implementation-Title: Implementation
Class-Path: 3rdParty\geronimo-annotation_1.0_spec-1.1.1.jar 3rdParty\x
 ml-resolver-1.2.jar 3rdParty\XmlSchema-1.4.5.jar

Name: com.asjava.plugin
Module-Class: Plugin

The ant task manifestis is a nested attribute in Task JAR, by the mean, manifest.MF is generated in Jar package, these content of manifest.MF describes the metadata of Jar.

The parameters of Ant Jar manifest task:

Attribute Description
file the manifest-file to create/update, it’s optional, by default it will be set named manifest.MF
mode One of “update” or “replace”, default is “replace”.
encoding The encoding used to read the existing manifest when updating. The task will always use UTF-8 when writing the manifest.
mergeClassPathAttributes Whether to merge the Class-Path attributes found in different manifests (if updating). If false, only the attribute of the most recent manifest will be preserved.
flattenAttributes Whether to merge attributes occuring more than once in a section (this can only happen for the Class-Path attribute) into a single attribute. Since Ant 1.8.0.

I use ANT 1.7 to generate JAR Manifest.MF file, it works fine.

Оцените статью
ASJAVA.COM
Добавить комментарий

Your email address will not be published. Required fields are marked *

  1. Hellen Flores

    Appreciated the share!
    Hellen

    Ответить