diff --git a/docs/manual/CoreTasks/pack.html b/docs/manual/CoreTasks/pack.html index e1f75a3c6..944dadc5b 100644 --- a/docs/manual/CoreTasks/pack.html +++ b/docs/manual/CoreTasks/pack.html @@ -10,9 +10,9 @@
Packs a file using the GZip or BZip2 algorithm. +
Packs a resource using the GZip or BZip2 algorithm. The output file is only generated if it doesn't exist or the source -file is newer.
+resource is newer.src | the file to gzip/bzip. | -Yes | +Yes, or a nested resource collection. |
destfile | @@ -35,6 +35,11 @@ file is newer.the deprecated old name of destfile. |
The specified resource will be used as src.
+<gzip src="test.tar" destfile="test.tar.gz"/> @@ -42,6 +47,13 @@ file is newer.+<bzip2 src="test.tar" destfile="test.tar.bz2"/>++<gzip destfile="archive.tar.gz"> + <url url="http://example.org/archive.tar"/> +</gzip> +downloads http://example.org/archive.tar and compresses it +to archive.tar.gz in the project's basedir on the fly.
Copyright © 2000-2005 The Apache Software Foundation. All rights Reserved.
diff --git a/src/etc/testcases/taskdefs/bzip2.xml b/src/etc/testcases/taskdefs/bzip2.xml index a037c3b49..48920f42a 100644 --- a/src/etc/testcases/taskdefs/bzip2.xml +++ b/src/etc/testcases/taskdefs/bzip2.xml @@ -6,6 +6,12 @@+ + ++ ++ + diff --git a/src/etc/testcases/taskdefs/gzip.xml b/src/etc/testcases/taskdefs/gzip.xml index f4b5e0e8d..2a1f6ccc7 100644 --- a/src/etc/testcases/taskdefs/gzip.xml +++ b/src/etc/testcases/taskdefs/gzip.xml @@ -22,6 +22,12 @@ + ++ ++ diff --git a/src/main/org/apache/tools/ant/taskdefs/BZip2.java b/src/main/org/apache/tools/ant/taskdefs/BZip2.java index e81b88f4f..ff531110e 100644 --- a/src/main/org/apache/tools/ant/taskdefs/BZip2.java +++ b/src/main/org/apache/tools/ant/taskdefs/BZip2.java @@ -22,6 +22,7 @@ import java.io.BufferedOutputStream; import java.io.FileOutputStream; import java.io.IOException; import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.util.FileUtils; import org.apache.tools.bzip2.CBZip2OutputStream; /** @@ -42,19 +43,27 @@ public class BZip2 extends Pack { bos.write('B'); bos.write('Z'); zOut = new CBZip2OutputStream(bos); - zipFile(source, zOut); + zipResource(getSrcResource(), zOut); } catch (IOException ioe) { String msg = "Problem creating bzip2 " + ioe.getMessage(); throw new BuildException(msg, ioe, getLocation()); } finally { - if (zOut != null) { - try { - // close up - zOut.close(); - } catch (IOException e) { - //ignore - } - } + FileUtils.close(zOut); } } + + /** + * Whether this task can deal with non-file resources. + * + * This implementation returns true only if this task is + * <bzip2>. Any subclass of this class that also wants to + * support non-file resources needs to override this method. We + * need to do so for backwards compatibility reasons since we + * can't expect subclasses to support resources.
+ * + * @since Ant 1.7 + */ + protected boolean supportsNonFileResources() { + return getClass().equals(BZip2.class); + } } diff --git a/src/main/org/apache/tools/ant/taskdefs/GZip.java b/src/main/org/apache/tools/ant/taskdefs/GZip.java index 7076d12d2..e728bd826 100644 --- a/src/main/org/apache/tools/ant/taskdefs/GZip.java +++ b/src/main/org/apache/tools/ant/taskdefs/GZip.java @@ -21,6 +21,7 @@ import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.GZIPOutputStream; import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.util.FileUtils; /** * Compresses a file with the GZIP algorithm. Normally used to compress @@ -39,19 +40,27 @@ public class GZip extends Pack { GZIPOutputStream zOut = null; try { zOut = new GZIPOutputStream(new FileOutputStream(zipFile)); - zipFile(source, zOut); + zipResource(getSrcResource(), zOut); } catch (IOException ioe) { String msg = "Problem creating gzip " + ioe.getMessage(); throw new BuildException(msg, ioe, getLocation()); } finally { - if (zOut != null) { - try { - // close up - zOut.close(); - } catch (IOException e) { - // do nothing - } - } + FileUtils.close(zOut); } } + + /** + * Whether this task can deal with non-file resources. + * + *This implementation returns true only if this task is + * <gzip>. Any subclass of this class that also wants to + * support non-file resources needs to override this method. We + * need to do so for backwards compatibility reasons since we + * can't expect subclasses to support resources.
+ * + * @since Ant 1.7 + */ + protected boolean supportsNonFileResources() { + return getClass().equals(GZip.class); + } } diff --git a/src/main/org/apache/tools/ant/taskdefs/Pack.java b/src/main/org/apache/tools/ant/taskdefs/Pack.java index 995d9d0c2..af466110b 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Pack.java +++ b/src/main/org/apache/tools/ant/taskdefs/Pack.java @@ -17,14 +17,15 @@ package org.apache.tools.ant.taskdefs; - import java.io.File; -import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Task; +import org.apache.tools.ant.types.Resource; +import org.apache.tools.ant.types.ResourceCollection; +import org.apache.tools.ant.types.resources.FileResource; /** * Abstract Base class for pack tasks. @@ -36,6 +37,7 @@ public abstract class Pack extends Task { protected File zipFile; protected File source; + private Resource src; /** * the required destination file. @@ -58,9 +60,37 @@ public abstract class Pack extends Task { * @param src the source file */ public void setSrc(File src) { - source = src; + setSrcResource(new FileResource(src)); } + /** + * The resource to pack; required. + * @param src resource to expand + */ + public void setSrcResource(Resource src) { + if (src.isDirectory()) { + throw new BuildException("the source can't be a directory"); + } + if (src instanceof FileResource) { + source = ((FileResource) src).getFile(); + } else if (!supportsNonFileResources()) { + throw new BuildException("Only FileSystem resources are" + + " supported."); + } + this.src = src; + } + + /** + * Set the source resource. + * @param a the resource to pack as a single element Resource collection. + */ + public void addConfigured(ResourceCollection a) { + if (a.size() != 1) { + throw new BuildException("only single argument resource collections" + + " are supported as archives"); + } + setSrcResource((Resource) a.iterator().next()); + } /** * validation routine @@ -76,13 +106,9 @@ public abstract class Pack extends Task { + "represent a directory!", getLocation()); } - if (source == null) { - throw new BuildException("src attribute is required", getLocation()); - } - - if (source.isDirectory()) { - throw new BuildException("Src attribute must not " - + "represent a directory!", getLocation()); + if (getSrcResource() == null) { + throw new BuildException("src attribute or nested resource is" + + " required", getLocation()); } } @@ -93,10 +119,11 @@ public abstract class Pack extends Task { public void execute() throws BuildException { validate(); - if (!source.exists()) { - log("Nothing to do: " + source.getAbsolutePath() + Resource s = getSrcResource(); + if (!s.isExists()) { + log("Nothing to do: " + s.toString() + " doesn't exist."); - } else if (zipFile.lastModified() < source.lastModified()) { + } else if (zipFile.lastModified() < s.getLastModified()) { log("Building: " + zipFile.getAbsolutePath()); pack(); } else { @@ -129,11 +156,22 @@ public abstract class Pack extends Task { */ protected void zipFile(File file, OutputStream zOut) throws IOException { - FileInputStream fIn = new FileInputStream(file); + zipResource(new FileResource(file), zOut); + } + + /** + * zip a resource to an output stream + * @param resource the resource to zip + * @param zOut the output stream + * @throws IOException on error + */ + protected void zipResource(Resource resource, OutputStream zOut) + throws IOException { + InputStream rIn = resource.getInputStream(); try { - zipFile(fIn, zOut); + zipFile(rIn, zOut); } finally { - fIn.close(); + rIn.close(); } } @@ -141,4 +179,24 @@ public abstract class Pack extends Task { * subclasses must implement this method to do their compression */ protected abstract void pack(); + + /** + * The source resource. + * + * @since Ant 1.7 + */ + public Resource getSrcResource() { + return src; + } + + /** + * Whether this task can deal with non-file resources. + * + *This implementation returns false.
+ * + * @since Ant 1.7 + */ + protected boolean supportsNonFileResources() { + return false; + } } diff --git a/src/testcases/org/apache/tools/ant/taskdefs/BZip2Test.java b/src/testcases/org/apache/tools/ant/taskdefs/BZip2Test.java index 51bc76efa..a86972eaa 100644 --- a/src/testcases/org/apache/tools/ant/taskdefs/BZip2Test.java +++ b/src/testcases/org/apache/tools/ant/taskdefs/BZip2Test.java @@ -95,6 +95,10 @@ public class BZip2Test extends BuildFileTest { actualIn.close(); } + public void testResource(){ + executeTarget("realTestWithResource"); + } + public void testDateCheck(){ executeTarget("testDateCheck"); String log = getLog(); diff --git a/src/testcases/org/apache/tools/ant/taskdefs/GzipTest.java b/src/testcases/org/apache/tools/ant/taskdefs/GzipTest.java index 8d88e86b3..da1df5839 100644 --- a/src/testcases/org/apache/tools/ant/taskdefs/GzipTest.java +++ b/src/testcases/org/apache/tools/ant/taskdefs/GzipTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2002,2004 The Apache Software Foundation + * Copyright 2000-2002,2004-2005 The Apache Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -57,6 +57,10 @@ public class GzipTest extends BuildFileTest { + log + "'", log.endsWith("asf-logo.gif.gz")); } + public void testResource(){ + executeTarget("realTestWithResource"); + } + public void testDateCheck(){ executeTarget("testDateCheck"); String log = getLog();