Attached is a patch that re-implements whenempty functionality for a jar when 1) no files are found and 2) no manifests are found.
 
Note: the patch is based on the Jar.java file from the 01/06/02 Nightly snapshot.  Last I checked the CVS repository, this build included the most recent revision (1.31) of Jar.java.
 
Kyle
--- Jar_old.java        Mon Jan  7 05:15:46 2002
+++ Jar.java    Mon Jan  7 15:35:51 2002
@@ -1,7 +1,7 @@
 /*
  * The Apache Software License, Version 1.1
  *
- * Copyright (c) 1999 The Apache Software Foundation.  All rights 
+ * Copyright (c) 1999 The Apache Software Foundation.  All rights
  * reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -9,7 +9,7 @@
  * are met:
  *
  * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer. 
+ *    notice, this list of conditions and the following disclaimer.
  *
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in
@@ -17,15 +17,15 @@
  *    distribution.
  *
  * 3. The end-user documentation included with the redistribution, if
- *    any, must include the following acknowlegement:  
- *       "This product includes software developed by the 
+ *    any, must include the following acknowlegement:
+ *       "This product includes software developed by the
  *        Apache Software Foundation (http://www.apache.org/)."
  *    Alternately, this acknowlegement may appear in the software itself,
  *    if and wherever such third-party acknowlegements normally appear.
  *
  * 4. The names "The Jakarta Project", "Ant", and "Apache Software
  *    Foundation" must not be used to endorse or promote products derived
- *    from this software without prior written permission. For written 
+ *    from this software without prior written permission. For written
  *    permission, please contact [EMAIL PROTECTED]
  *
  * 5. Products derived from this software may not be called "Apache"
@@ -66,7 +66,7 @@
 
 /**
  * Creates a JAR archive.
- * 
+ *
  * @author James Davidson <a href="mailto:[EMAIL PROTECTED]">[EMAIL 
PROTECTED]</a>
  */
 public class Jar extends Zip {
@@ -75,8 +75,8 @@
 
     private File manifestFile;
     private Manifest manifest;
-    private Manifest execManifest;  
-    
+    private Manifest execManifest;
+
     /** true if a manifest has been specified in the task */
     private boolean buildFileManifest = false;
 
@@ -119,15 +119,15 @@
         manifest.merge(newManifest);
         buildFileManifest = true;
     }
-    
+
     public void setManifest(File manifestFile) {
         if (!manifestFile.exists()) {
-            throw new BuildException("Manifest file: " + manifestFile + " does 
not exist.", 
+            throw new BuildException("Manifest file: " + manifestFile + " does 
not exist.",
                                      getLocation());
         }
 
         this.manifestFile = manifestFile;
-        
+
         Reader r = null;
         try {
             r = new FileReader(manifestFile);
@@ -174,14 +174,14 @@
             for (Enumeration e = execManifest.getWarnings(); 
e.hasMoreElements(); ) {
                 log("Manifest warning: " + (String)e.nextElement(), 
Project.MSG_WARN);
             }
-        
+
             zipDir(null, zOut, "META-INF/");
             // time to write the manifest
             ByteArrayOutputStream baos = new ByteArrayOutputStream();
             PrintWriter writer = new PrintWriter(baos);
             execManifest.write(writer);
             writer.flush();
-        
+
             ByteArrayInputStream bais = new 
ByteArrayInputStream(baos.toByteArray());
             super.zipFile(bais, zOut, "META-INF/MANIFEST.MF", 
System.currentTimeMillis());
             super.initZipOutputStream(zOut);
@@ -274,12 +274,12 @@
             throw new BuildException("Invalid Manifest", e, getLocation());
         }
     }
-    
+
     protected void zipFile(File file, ZipOutputStream zOut, String vPath)
         throws IOException
     {
         // If the file being added is META-INF/MANIFEST.MF, we warn if it's 
not the
-        // one specified in the "manifest" attribute - or if it's being added 
twice, 
+        // one specified in the "manifest" attribute - or if it's being added 
twice,
         // meaning the same file is specified by the "manifeset" attribute and 
in
         // a <fileset> element.
         if (vPath.equalsIgnoreCase("META-INF/MANIFEST.MF"))  {
@@ -295,7 +295,7 @@
         throws IOException
     {
         // If the file being added is META-INF/MANIFEST.MF, we merge it with 
the
-        // current manifest 
+        // current manifest
         if (vPath.equalsIgnoreCase("META-INF/MANIFEST.MF"))  {
             try {
                 zipManifestEntry(is);
@@ -309,7 +309,7 @@
     }
 
     /**
-     * Check whether the archive is up-to-date; 
+     * Check whether the archive is up-to-date;
      * @param scanners list of prepared scanners containing files to archive
      * @param zipFile intended archive file (may or may not exist)
      * @return true if nothing need be done (may have done something already); 
false if
@@ -317,53 +317,77 @@
      * @exception BuildException if it likes
      */
     protected boolean isUpToDate(FileScanner[] scanners, File zipFile) throws 
BuildException {
+        String[][] fileNames = super.grabFileNames(scanners);
+        File[] files = super.grabFiles(scanners, fileNames);
+
         // need to handle manifest as a special check
-        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) {
-                    log("Updating jar since the current jar has no manifest", 
Project.MSG_VERBOSE);
-                    return false;
-                }
-                Manifest currentManifest = new Manifest(new 
InputStreamReader(theZipFile.getInputStream(entry)));
-                if (manifest == null) {
-                    manifest = Manifest.getDefaultManifest();
-                }
-                if (!currentManifest.equals(manifest)) {
-                    log("Updating jar since jar manifest has changed", 
Project.MSG_VERBOSE);
-                    return false;
+        if (manifestFile == null) {
+            if (!buildFileManifest) {
+                if (files.length == 0) {
+                    // If the jar file is truly empty (no manifest, no files),
+                    // utilize whenempty attribute
+                    if (emptyBehavior.equals("skip")) {
+                        // Skip with warning.
+                        log("Warning: skipping " + archiveType + " archive " + 
zipFile +
+                            " because no files were included and no manifest 
file was" +
+                            " specified.", Project.MSG_WARN);
+                        return true;
+                    } else if (emptyBehavior.equals("fail")) {
+                        // Fail.
+                        throw new BuildException("Cannot create " + 
archiveType + " archive " + zipFile +
+                                                 ": no files were included and 
no manifest file was specified.",
+                                                 location);
+                    } else {
+                        // Create.
+                        return createEmptyZip(zipFile);
+                    }
                 }
-            }
-            catch (Exception e) {
-                // any problems and we will rebuild
-                log("Updating jar since cannot read current jar manifest: " + 
e.getClass().getName() + e.getMessage(), 
-                    Project.MSG_VERBOSE);
-                return false;
-            }
-            finally {
-                if (theZipFile != null) {
-                    try {
-                        theZipFile.close();
+            } else {
+                // Otherwise,  check the jar, generate a manifest if none is 
present
+                // or use the user-specified manifest.
+                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) {
+                        log("Updating jar since the current jar has no 
manifest", Project.MSG_VERBOSE);
+                        return false;
+                    }
+                    Manifest currentManifest = new 
Manifest(theZipFile.getInputStream(entry));
+                    if (manifest == null) {
+                        manifest = getDefaultManifest();
                     }
-                    catch (IOException e) {
-                        //ignore
+                    if (!currentManifest.equals(manifest)) {
+                        log("Updating jar since jar manifest has changed", 
Project.MSG_VERBOSE);
+                        return false;
+                    }
+                } catch (Exception e) {
+                    // any problems and we will rebuild
+                    log("Updating jar since cannot read current jar manifest: 
" + e.getClass().getName() + e.getMessage(),
+                        Project.MSG_VERBOSE);
+                    return false;
+                } finally {
+                    if (theZipFile != null) {
+                        try {
+                            theZipFile.close();
+                        }
+                        catch (IOException e) {
+                            //ignore
+                        }
                     }
                 }
             }
-        }
-        else if (manifestFile.lastModified() > zipFile.lastModified()) {
+        } else if (manifestFile.lastModified() > zipFile.lastModified()) {
             return false;
         }
         return super.isUpToDate(scanners, zipFile);
     }
-        
+
     protected boolean createEmptyZip(File zipFile) {
-        // Jar files always contain a manifest and can never be empty        
+        // Jar files always contain a manifest and can never be empty
         return false;
     }
-    
+
     /**
      * Make sure we don't think we already have a MANIFEST next time this task
      * gets executed.

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to