git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@344334 13f79535-47bb-0310-9956-ffa450edef68master
@@ -10,9 +10,9 @@ | |||
<h2><a name="pack">GZip/BZip2</a></h2> | |||
<h3>Description</h3> | |||
<p>Packs a file using the GZip or BZip2 algorithm. | |||
<p>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.</p> | |||
resource is newer.</p> | |||
<h3>Parameters</h3> | |||
<table border="1" cellpadding="2" cellspacing="0"> | |||
<tr> | |||
@@ -23,7 +23,7 @@ file is newer.</p> | |||
<tr> | |||
<td valign="top">src</td> | |||
<td valign="top">the file to gzip/bzip.</td> | |||
<td align="center" valign="top">Yes</td> | |||
<td align="center" valign="top">Yes, or a nested resource collection.</td> | |||
</tr> | |||
<tr> | |||
<td valign="top">destfile</td> | |||
@@ -35,6 +35,11 @@ file is newer.</p> | |||
<td valign="top">the <i>deprecated</i> old name of destfile.</td> | |||
</tr> | |||
</table> | |||
<h4>any <a href="../CoreTypes/resources.html">resource</a> or single element | |||
resource collection</h4> | |||
<p>The specified resource will be used as src.</p> | |||
<h3>Examples</h3> | |||
<blockquote><pre> | |||
<gzip src="test.tar" destfile="test.tar.gz"/> | |||
@@ -42,6 +47,13 @@ file is newer.</p> | |||
<blockquote><pre> | |||
<bzip2 src="test.tar" destfile="test.tar.bz2"/> | |||
</pre></blockquote> | |||
<blockquote><pre> | |||
<gzip destfile="archive.tar.gz"> | |||
<url url="http://example.org/archive.tar"/> | |||
</gzip> | |||
</pre></blockquote> | |||
<p>downloads <i>http://example.org/archive.tar</i> and compresses it | |||
to <i>archive.tar.gz</i> in the project's basedir on the fly.</p> | |||
<hr> | |||
<p align="center">Copyright © 2000-2005 The Apache Software Foundation. All rights | |||
Reserved.</p> | |||
@@ -6,6 +6,12 @@ | |||
<bzip2 src="expected/asf-logo-huge.tar" zipfile="asf-logo-huge.tar.bz2" /> | |||
</target> | |||
<target name="realTestWithResource"> | |||
<bzip2 zipfile="asf-logo-huge.tar.bz2"> | |||
<file file="expected/asf-logo-huge.tar"/> | |||
</bzip2> | |||
</target> | |||
<target name="testDateCheck"> | |||
<touch file="asf-logo.gif.bz2"/> | |||
<bzip2 src="../asf-logo.gif" zipfile="asf-logo.gif.bz2" /> | |||
@@ -22,6 +22,12 @@ | |||
<gzip src="../asf-logo.gif" zipfile="asf-logo.gif.gz" /> | |||
</target> | |||
<target name="realTestWithResource"> | |||
<gzip zipfile="asf-logo.gif.gz"> | |||
<file file="../asf-logo.gif"/> | |||
</gzip> | |||
</target> | |||
<target name="testDateCheck"> | |||
<touch file="asf-logo.gif.gz"/> | |||
<gzip src="../asf-logo.gif" zipfile="asf-logo.gif.gz" /> | |||
@@ -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. | |||
* | |||
* <p>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.</p> | |||
* | |||
* @since Ant 1.7 | |||
*/ | |||
protected boolean supportsNonFileResources() { | |||
return getClass().equals(BZip2.class); | |||
} | |||
} |
@@ -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. | |||
* | |||
* <p>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.</p> | |||
* | |||
* @since Ant 1.7 | |||
*/ | |||
protected boolean supportsNonFileResources() { | |||
return getClass().equals(GZip.class); | |||
} | |||
} |
@@ -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. | |||
* | |||
* <p>This implementation returns false.</p> | |||
* | |||
* @since Ant 1.7 | |||
*/ | |||
protected boolean supportsNonFileResources() { | |||
return false; | |||
} | |||
} |
@@ -95,6 +95,10 @@ public class BZip2Test extends BuildFileTest { | |||
actualIn.close(); | |||
} | |||
public void testResource(){ | |||
executeTarget("realTestWithResource"); | |||
} | |||
public void testDateCheck(){ | |||
executeTarget("testDateCheck"); | |||
String log = getLog(); | |||
@@ -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(); | |||