You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by co...@apache.org on 2001/08/01 14:56:23 UTC

cvs commit: jakarta-ant/src/main/org/apache/tools/ant/taskdefs Jar.java Manifest.java Zip.java

conor       01/08/01 05:56:23

  Modified:    src/main/org/apache/tools/ant/taskdefs Jar.java
                        Manifest.java Zip.java
  Log:
  Allow manifests of Jar files to be specified in-line. This allows the
  use of Ant properties in the manifest. The inline manifest will be
  merged with any external file manifests. Example usage
  
      <jar jarfile="taskjar2.jar" basedir="." manifest="manifest.mf">
        <include name="src/**/*"/>
        <manifest>
          <attribute name="Built-By" value="${user.name}"/>
        </manifest>
      </jar>
  
  Revision  Changes    Path
  1.21      +39 -2     jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Jar.java
  
  Index: Jar.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Jar.java,v
  retrieving revision 1.20
  retrieving revision 1.21
  diff -u -r1.20 -r1.21
  --- Jar.java	2001/07/30 11:15:17	1.20
  +++ Jar.java	2001/08/01 12:56:23	1.21
  @@ -71,7 +71,8 @@
       private File manifestFile;
       private Manifest manifest;
       private Manifest execManifest;    
  -
  +    private boolean buildFileManifest = false;
  +    
       public Jar() {
           super();
           archiveType = "jar";
  @@ -83,6 +84,14 @@
           super.setZipfile(jarFile);
       }
   
  +    public void addConfiguredManifest(Manifest newManifest) throws ManifestException {
  +        if (manifest == null) {
  +            manifest = getDefaultManifest();
  +        }
  +        manifest.merge(newManifest);
  +        buildFileManifest = true;
  +    }
  +    
       public void setManifest(File manifestFile) {
           if (!manifestFile.exists()) {
               throw new BuildException("Manifest file: " + manifestFile + " does not exist.", 
  @@ -264,7 +273,35 @@
        */
       protected boolean isUpToDate(FileScanner[] scanners, File zipFile) throws BuildException {
           // need to handle manifest as a special check
  -        if (manifestFile != null && manifestFile.lastModified() > zipFile.lastModified()) {
  +        if (buildFileManifest || manifestFile == null) {
  +            java.util.zip.ZipFile theZipFile = null;
  +            try {
  +                theZipFile = new java.util.zip.ZipFile(zipFile);
  +                java.util.zip.ZipEntry entry = theZipFile.getEntry("META-INF/MANIFEST.MF");
  +                if (entry == null) {
  +                    return false;
  +                }
  +                Manifest currentManifest = new Manifest(theZipFile.getInputStream(entry));
  +                if (!currentManifest.equals(manifest)) {
  +                    return false;
  +                }
  +            }
  +            catch (Exception e) {
  +                // any problems and we will rebuild
  +                return false;
  +            }
  +            finally {
  +                if (theZipFile != null) {
  +                    try {
  +                        theZipFile.close();
  +                    }
  +                    catch (IOException e) {
  +                        //ignore
  +                    }
  +                }
  +            }
  +        }
  +        else if (manifestFile.lastModified() > zipFile.lastModified()) {
               return false;
           }
           return super.isUpToDate(scanners, zipFile);
  
  
  
  1.4       +118 -15   jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Manifest.java
  
  Index: Manifest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Manifest.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- Manifest.java	2001/07/30 11:15:17	1.3
  +++ Manifest.java	2001/08/01 12:56:23	1.4
  @@ -57,6 +57,8 @@
   import java.util.*;
   import java.io.*;
   
  +import org.apache.tools.ant.BuildException;
  +
   /**
    * Class to manage Manifest information
    * 
  @@ -84,7 +86,7 @@
       /**
        * Class to hold manifest attributes
        */
  -    private class Attribute {
  +    static public class Attribute {
           /** The attribute's name */
           private String name = null;
           
  @@ -118,6 +120,17 @@
               this.value = value;
           }
           
  +        public boolean equals(Object rhs) {
  +            if (!(rhs instanceof Attribute)) {
  +                return false;
  +            }
  +            
  +            Attribute rhsAttribute = (Attribute)rhs;
  +            return (name != null && rhsAttribute.name != null &&
  +                    name.toLowerCase().equals(rhsAttribute.name.toLowerCase()) &&
  +                    value != null && value.equals(rhsAttribute.value));
  +        }
  +                    
           /**
            * Parse a line into name and value pairs
            *
  @@ -214,7 +227,9 @@
        * Manifest. A section consists of a set of attribute values,
        * separated from other sections by a blank line.
        */
  -    private class Section {
  +    static public class Section {
  +        private Vector warnings = new Vector();
  +        
           /** The section's name if any. The main section in a manifest is unnamed.*/
           private String name = null;
           
  @@ -266,7 +281,7 @@
                   }
                   else {
                       attribute = new Attribute(line);
  -                    String nameReadAhead = addAttribute(attribute);
  +                    String nameReadAhead = addAttributeAndCheck(attribute);
                       if (nameReadAhead != null) {
                           return nameReadAhead;
                       }
  @@ -292,6 +307,11 @@
                   // the merge file always wins
                   attributes.put(attributeName, section.attributes.get(attributeName));
               }
  +            
  +            // add in the warnings
  +            for (Enumeration e = section.warnings.elements(); e.hasMoreElements();) {
  +                warnings.addElement(e.nextElement());
  +            }
           }
           
           /**
  @@ -337,6 +357,14 @@
           public void removeAttribute(String attributeName) {
               attributes.remove(attributeName.toLowerCase());
           }
  +
  +        public void addConfiguredAttribute(Attribute attribute) throws ManifestException {
  +            String check = addAttributeAndCheck(attribute);
  +            if (check != null) {
  +                throw new BuildException("Use the \"name\" attribute of the <section> element rather than using " +
  +                                         "the \"Name\" attribute");
  +            }
  +        }
           
           /**
            * Add an attribute to the section
  @@ -347,7 +375,10 @@
            *
            * @throws ManifestException if the attribute already exists in this section.
            */
  -        public String addAttribute(Attribute attribute) throws ManifestException {
  +        public String addAttributeAndCheck(Attribute attribute) throws ManifestException {
  +            if (attribute.getName() == null || attribute.getValue() == null) {
  +                throw new BuildException("Attributes must have name and value");
  +            }
               if (attribute.getName().equalsIgnoreCase(ATTRIBUTE_NAME)) {
                   warnings.addElement("\"" + ATTRIBUTE_NAME + "\" attributes should not occur in the " +
                                       "main section and must be the first element in all " + 
  @@ -368,8 +399,34 @@
               }
               return null;
           }
  -    }
   
  +        public Enumeration getWarnings() {
  +            return warnings.elements();
  +        }
  +        
  +        public boolean equals(Object rhs) {
  +            if (!(rhs instanceof Section)) {
  +                return false;
  +            }
  +            
  +            Section rhsSection = (Section)rhs;
  +            if (attributes.size() != rhsSection.attributes.size()) {
  +                return false;
  +            }
  +        
  +            for (Enumeration e = attributes.elements(); e.hasMoreElements();) {
  +                Attribute attribute  = (Attribute)e.nextElement();
  +                Attribute rshAttribute = (Attribute)rhsSection.attributes.get(attribute.getName().toLowerCase());
  +                if (!attribute.equals(rshAttribute)) {
  +                    return false;
  +                }
  +            }
  +            
  +            return true;
  +        }
  +    }        
  +
  +
       /** The version of this manifest */
       private String manifestVersion = DEFAULT_MANIFEST_VERSION;
       
  @@ -379,9 +436,6 @@
       /** The named sections of this manifest */
       private Hashtable sections = new Hashtable();
   
  -    /** Warnings for this manifest file */
  -    private Vector warnings = new Vector();
  -    
       /** Construct an empty manifest */
       public Manifest() {
       }
  @@ -428,15 +482,26 @@
                   // this line is the first attribute. set it and then let the normal
                   // read handle the rest
                   Attribute firstAttribute = new Attribute(line);
  -                section.addAttribute(firstAttribute);
  +                section.addAttributeAndCheck(firstAttribute);
               }
                       
               section.setName(nextSectionName);
               nextSectionName = section.read(reader);
  -            sections.put(section.getName().toLowerCase(), section);
  +            addConfiguredSection(section);
           }
       }
       
  +    public void addConfiguredSection(Section section) throws ManifestException {
  +        if (section.getName() == null) {
  +            throw new BuildException("Sections must have a name");
  +        }
  +        sections.put(section.getName().toLowerCase(), section);
  +    }
  +    
  +    public void addConfiguredAttribute(Attribute attribute) throws ManifestException {
  +        mainSection.addConfiguredAttribute(attribute);
  +    }
  +    
       /**
        * Merge the contents of the given manifest into this manifest
        *
  @@ -460,10 +525,6 @@
               }
           }
           
  -        // add in the warnings
  -        for (Enumeration e = other.warnings.elements(); e.hasMoreElements();) {
  -            warnings.addElement(e.nextElement());
  -        }
       }
       
       /**
  @@ -483,7 +544,7 @@
           mainSection.write(writer);
           if (signatureVersion != null) {
               try {
  -                mainSection.addAttribute(new Attribute(ATTRIBUTE_SIGNATURE_VERSION, signatureVersion));
  +                mainSection.addConfiguredAttribute(new Attribute(ATTRIBUTE_SIGNATURE_VERSION, signatureVersion));
               }
               catch (ManifestException e) {
                   // shouldn't happen - ignore
  @@ -518,6 +579,48 @@
        * @return an enumeration of warning strings
        */
       public Enumeration getWarnings() {
  +        Vector warnings = new Vector();
  +        
  +        for (Enumeration e2 = mainSection.getWarnings(); e2.hasMoreElements();) {
  +            warnings.addElement(e2.nextElement());
  +        }
  +        
  +        // create a vector and add in the warnings for all the sections
  +        for (Enumeration e = sections.elements(); e.hasMoreElements();) {
  +            Section section = (Section)e.nextElement();
  +            for (Enumeration e2 = section.getWarnings(); e2.hasMoreElements();) {
  +                warnings.addElement(e2.nextElement());
  +            }
  +        }
  +        
           return warnings.elements();
  +    }
  +    
  +    public boolean equals(Object rhs) {
  +        if (!(rhs instanceof Manifest)) {
  +            return false;
  +        }
  +        
  +        Manifest rhsManifest = (Manifest)rhs;
  +        if (!manifestVersion.equals(rhsManifest.manifestVersion)) {
  +            return false;
  +        }
  +        if (sections.size() != rhsManifest.sections.size()) {
  +            return false;
  +        }
  +        
  +        if (!mainSection.equals(rhsManifest.mainSection)) {
  +            return false;
  +        }
  +        
  +        for (Enumeration e = sections.elements(); e.hasMoreElements();) {
  +            Section section = (Section)e.nextElement();
  +            Section rhsSection = (Section)rhsManifest.sections.get(section.getName().toLowerCase());
  +            if (!section.equals(rhsSection)) {
  +                return false;
  +            }
  +        }
  +        
  +        return true;
       }
   }
  
  
  
  1.45      +10 -6     jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Zip.java
  
  Index: Zip.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Zip.java,v
  retrieving revision 1.44
  retrieving revision 1.45
  diff -u -r1.44 -r1.45
  --- Zip.java	2001/07/30 11:15:17	1.44
  +++ Zip.java	2001/08/01 12:56:23	1.45
  @@ -295,8 +295,9 @@
               } finally {
                   // Close the output stream.
                   try {
  -                    if (zOut != null)
  -                        zOut.close ();
  +                    if (zOut != null) {
  +                        zOut.close();
  +                    }
                   } catch(IOException ex) {
                       // If we're in this finally clause because of an exception, we don't 
                       // really care if there's an exception when closing the stream. E.g. if it
  @@ -317,7 +318,7 @@
               }
   
               if (reallyDoUpdate) {
  -                if (!renamedFile.renameTo (zipFile)) {
  +                if (!renamedFile.renameTo(zipFile)) {
                       msg+=" (and I couldn't rename the temporary file "+
                           renamedFile.getName()+" back)";
                   }
  @@ -327,11 +328,14 @@
           } finally {
               cleanUp();
           }
  +        
           // If we've been successful on an update, delete the temporary file
  -        if (success && reallyDoUpdate)
  -            if (!renamedFile.delete())
  -                log ("Warning: unable to delete temporary file "+
  +        if (success && reallyDoUpdate) {
  +            if (!renamedFile.delete()) {
  +                log ("Warning: unable to delete temporary file " +
                        renamedFile.getName(), Project.MSG_WARN);
  +            }
  +        }
       }
   
       /**