FileDocCategorySizeDatePackage
ManifestTask.javaAPI DocApache Ant 1.706717Wed Dec 13 06:16:24 GMT 2006org.apache.tools.ant.taskdefs

ManifestTask

public class ManifestTask extends org.apache.tools.ant.Task
Creates a manifest file for inclusion in a JAR, Ant task wrapper around {@link Manifest Manifest}. This task can be used to write a Manifest file, optionally replacing or updating an existing file.
since
Ant 1.5
ant.task
category="java"

Fields Summary
private Manifest
nestedManifest
Holds the real data.
private File
manifestFile
The file to which the manifest should be written when used as a task
private Mode
mode
The mode with which the manifest file is written
private String
encoding
The encoding of the manifest file
Constructors Summary
public ManifestTask()
Default constructor

        mode = new Mode();
        mode.setValue("replace");
    
Methods Summary
public voidaddConfiguredAttribute(Manifest.Attribute attribute)
Add an attribute to the manifest - it is added to the main section.

param
attribute the attribute to be added.
exception
ManifestException if the attribute is not valid.

        nestedManifest.addConfiguredAttribute(attribute);
    
public voidaddConfiguredSection(Manifest.Section section)
Add a section to the manifest

param
section the manifest section to be added
exception
ManifestException if the section is not valid.

        nestedManifest.addConfiguredSection(section);
    
public voidexecute()
Create or update the Manifest when used as a task.

throws
BuildException if the manifest cannot be written.

        if (manifestFile == null) {
            throw new BuildException("the file attribute is required");
        }

        Manifest toWrite = Manifest.getDefaultManifest();
        Manifest current = null;
        BuildException error = null;

        if (manifestFile.exists()) {
            FileInputStream fis = null;
            InputStreamReader isr = null;
            try {
                fis = new FileInputStream(manifestFile);
                if (encoding == null) {
                    isr = new InputStreamReader(fis, "UTF-8");
                } else {
                    isr = new InputStreamReader(fis, encoding);
                }
                current = new Manifest(isr);
            } catch (ManifestException m) {
                error = new BuildException("Existing manifest " + manifestFile
                                           + " is invalid", m, getLocation());
            } catch (IOException e) {
                error = new BuildException("Failed to read " + manifestFile,
                                           e, getLocation());
            } finally {
                FileUtils.close(isr);
            }
        }

        //look for and print warnings
        for (Enumeration e = nestedManifest.getWarnings();
                e.hasMoreElements();) {
            log("Manifest warning: " + (String) e.nextElement(),
                    Project.MSG_WARN);
        }
        try {
            if (mode.getValue().equals("update") && manifestFile.exists()) {
                if (current != null) {
                    toWrite.merge(current);
                } else if (error != null) {
                    throw error;
                }
            }

            toWrite.merge(nestedManifest);
        } catch (ManifestException m) {
            throw new BuildException("Manifest is invalid", m, getLocation());
        }

        if (toWrite.equals(current)) {
            log("Manifest has not changed, do not recreate",
                Project.MSG_VERBOSE);
            return;
        }

        PrintWriter w = null;
        try {
            FileOutputStream fos = new FileOutputStream(manifestFile);
            OutputStreamWriter osw = new OutputStreamWriter(fos, Manifest.JAR_ENCODING);
            w = new PrintWriter(osw);
            toWrite.write(w);
        } catch (IOException e) {
            throw new BuildException("Failed to write " + manifestFile,
                                     e, getLocation());
        } finally {
            if (w != null) {
                w.close();
            }
        }
    
public voidsetEncoding(java.lang.String encoding)
The encoding to use for reading in an existing manifest file

param
encoding the manifest file encoding.

        this.encoding = encoding;
    
public voidsetFile(java.io.File f)
The name of the manifest file to create/update. Required if used as a task.

param
f the Manifest file to be written

        manifestFile = f;
    
public voidsetMode(org.apache.tools.ant.taskdefs.ManifestTask$Mode m)
Update policy: either "update" or "replace"; default is "replace".

param
m the mode value - update or replace.

        mode = m;