Browse Source

New task Touch. Expand now also retains the time from the Zipfile.

Touch works somewhat like the Unix touch(1) command. It changes the
modification time of files and maybe creates a new file if the
requested one doesn't exist.

In JDK 1.1 only the creation of new files will work, all other cases
generate warning messages.

Expand and Untar reuse Touch to set the modification times of the
expanded files from the information inside the archive.

I had to make Task.setProject public to ease reuse.


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@267708 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 25 years ago
parent
commit
1f4fe213e2
6 changed files with 271 additions and 33 deletions
  1. +46
    -1
      docs/index.html
  2. +1
    -1
      src/main/org/apache/tools/ant/Task.java
  3. +12
    -0
      src/main/org/apache/tools/ant/taskdefs/Expand.java
  4. +198
    -0
      src/main/org/apache/tools/ant/taskdefs/Touch.java
  5. +13
    -31
      src/main/org/apache/tools/ant/taskdefs/Untar.java
  6. +1
    -0
      src/main/org/apache/tools/ant/taskdefs/defaults.properties

+ 46
- 1
docs/index.html View File

@@ -22,7 +22,7 @@
<li>Sam Ruby (<a href="mailto:rubys@us.ibm.com">rubys@us.ibm.com</a>)</li> <li>Sam Ruby (<a href="mailto:rubys@us.ibm.com">rubys@us.ibm.com</a>)</li>
</ul> </ul>


<p>Version 1.0.8.1 - 2000/06/27</p>
<p>Version 1.0.8.1 - 2000/06/28</p>


<hr> <hr>
<h2>Table of Contents</h2> <h2>Table of Contents</h2>
@@ -525,6 +525,7 @@ If you do not want these default excludes applied, you may disable them with the
<li><a href="#rmic">Rmic</a></li> <li><a href="#rmic">Rmic</a></li>
<li><a href="#tar">Tar</a></li> <li><a href="#tar">Tar</a></li>
<li><a href="#taskdef">Taskdef</a></li> <li><a href="#taskdef">Taskdef</a></li>
<li><a href="#touch">Touch</a></li>
<li><a href="#tstamp">Tstamp</a></li> <li><a href="#tstamp">Tstamp</a></li>
<li><a href="#style">Style</a></li> <li><a href="#style">Style</a></li>
<li><a href="#untar">Untar</a></li> <li><a href="#untar">Untar</a></li>
@@ -1017,6 +1018,8 @@ systems.</p>
<h2><a name="expand">Expand</a></h2> <h2><a name="expand">Expand</a></h2>
<h3>Description</h3> <h3>Description</h3>
<p>Unzips a zipfile.</p> <p>Unzips a zipfile.</p>
<p>For JDK 1.1 "last modified time" field is set to current time instead of being
carried from zipfile.</p>
<h3>Parameters</h3> <h3>Parameters</h3>
<table border="1" cellpadding="2" cellspacing="0"> <table border="1" cellpadding="2" cellspacing="0">
<tr> <tr>
@@ -2426,6 +2429,48 @@ href="#writingowntask">Writing your own task</a>&quot;.</p>
<p>makes a task called <code>myjavadoc</code> available to Ant. The class <code>com.mydomain.JavadocTask</code> <p>makes a task called <code>myjavadoc</code> available to Ant. The class <code>com.mydomain.JavadocTask</code>
implements the task.</p> implements the task.</p>
<hr> <hr>
<h2><a name="touch">Touch</a></h2>
<h3>Description</h3>
<p>Changes the modification time of a file and possibly creates it at
the same time.</p>
<p>For JDK 1.1 only the creation of new files with a modification time
of now works, all other cases will emit a warning.</p>
<h3>Parameters</h3>
<table border="1" cellpadding="2" cellspacing="0">
<tr>
<td valign="top"><b>Attribute</b></td>
<td valign="top"><b>Description</b></td>
<td align="center" valign="top"><b>Required</b></td>
</tr>
<tr>
<td valign="top">file</td>
<td valign="top">the name of the file</td>
<td valign="top" align="center">Yes</td>
</tr>
<tr>
<td valign="top">millis</td>
<td valign="top">specifies the new modification time of the file
in milliseconds since midnight Jan 1 1970</td>
<td valign="top" align="center">No</td>
</tr>
<tr>
<td valign="top">datetime</td>
<td valign="top">specifies the new modification time of the file
in the format MM/DD/YYYY HH:MM AM_or_PM.</td>
<td valign="top" align="center">No</td>
</tr>
</table>
<p>If both <code>millis</code> and <code>datetime</code> are ommited
the current time is assumed.</p>
<h3>Examples</h3>
<pre> &lt;touch file=&quot;myfile&quot; /&gt;</pre>
<p>creates <code>myfile</code> if it doesn't exist and changes the
modification time to the current time.</p>
<pre> &lt;touch file=&quot;myfile&quot; datetime=&quot;06/28/2000 2:02 pm&quot; /&gt;</pre>
<p>creates <code>myfile</code> if it doesn't exist and changes the
modification time to Jun, 28 2000 2:02 pm (14:02 for those used to 24
hour times).</p>
<hr>
<h2><a name="tstamp">Tstamp</a></h2> <h2><a name="tstamp">Tstamp</a></h2>
<h3>Description</h3> <h3>Description</h3>
<p>Sets the DSTAMP, TSTAMP and TODAY properties in the current project. The <p>Sets the DSTAMP, TSTAMP and TODAY properties in the current project. The


+ 1
- 1
src/main/org/apache/tools/ant/Task.java View File

@@ -73,7 +73,7 @@ public abstract class Task {
* *
* @param project Project in whose scope this task belongs. * @param project Project in whose scope this task belongs.
*/ */
void setProject(Project project) {
public void setProject(Project project) {
this.project = project; this.project = project;
} }




+ 12
- 0
src/main/org/apache/tools/ant/taskdefs/Expand.java View File

@@ -61,6 +61,7 @@ import java.util.zip.*;
* Unzip a file. * Unzip a file.
* *
* @author costin@dnt.ro * @author costin@dnt.ro
* @author Stefan Bodewig <a href="mailto:stefan.bodewig@megabit.net">stefan.bodewig@megabit.net</a>
*/ */
public class Expand extends Task { public class Expand extends Task {
private String dest; // req private String dest; // req
@@ -73,6 +74,10 @@ public class Expand extends Task {
*/ */
// XXX move it to util or tools // XXX move it to util or tools
public void execute() throws BuildException { public void execute() throws BuildException {
Touch touch = new Touch();
touch.setProject(project);
touch.setTarget(target);
try { try {
File srcF=project.resolveFile(source); File srcF=project.resolveFile(source);
File dir=project.resolveFile(dest); File dir=project.resolveFile(dest);
@@ -103,6 +108,13 @@ public class Expand extends Task {
fos.close(); fos.close();
} }

if (project.getJavaVersion() != Project.JAVA_1_1) {
touch.setFile(f.getAbsolutePath());
touch.setMillis(ze.getTime());
touch.touch();
}

} catch( FileNotFoundException ex ) { } catch( FileNotFoundException ex ) {
System.out.println("FileNotFoundException: " + ze.getName() ); System.out.println("FileNotFoundException: " + ze.getName() );
} }


+ 198
- 0
src/main/org/apache/tools/ant/taskdefs/Touch.java View File

@@ -0,0 +1,198 @@
/*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* 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
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* 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", "Tomcat", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/

package org.apache.tools.ant.taskdefs;

import org.apache.tools.ant.*;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.Locale;

/**
* Touch a file - corresponds to the Unix touch command.
*
* <p>If the file to touch doesn't exist, an empty one is
* created. Setting the modification time of files is not supported in
* JDK 1.1.
*
* @author Stefan Bodewig <a href="mailto:stefan.bodewig@megabit.net">stefan.bodewig@megabit.net</a>
*/
public class Touch extends Task {

private File file; // required
private long millis = -1;
private String dateTime;

private static Method setLastModified = null;
private static Object lockReflection = new Object();

/**
* The name of the file to touch.
*/
public void setFile(String name) {
file = project.resolveFile(name);
}

/**
* Milliseconds since 01/01/1970 00:00 am.
*/
public void setMillis(long millis) {
this.millis = millis;
}

/**
* Date in the format DD/MM/YYYY HH:MM AM_PM.
*/
public void setDatetime(String dateTime) {
this.dateTime = dateTime;
}

/**
* Do the work.
*
* @exception BuildException Thrown in unrecoverable error.
*/
public void execute() throws BuildException {
if (file.exists() && project.getJavaVersion() == Project.JAVA_1_1) {
project.log("Cannot change the modification time of "
+ file + " in JDK 1.1",
Project.MSG_WARN);
return;
}
if (dateTime != null) {
DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT,
DateFormat.SHORT,
Locale.US);
try {
setMillis(df.parse(dateTime).getTime());
} catch (ParseException pe) {
throw new BuildException(pe.getMessage(), pe, location);
}
}

if (millis >= 0 && project.getJavaVersion() == Project.JAVA_1_1) {
project.log(file +
" will be created but its modification time cannot be set in JDK 1.1",
Project.MSG_WARN);
}

touch();
}

/**
* Does the actual work. Entry point for Untar and Expand as well.
*/
void touch() throws BuildException {
if (!file.exists()) {
project.log("Creating "+file, Project.MSG_INFO);
try {
FileOutputStream fos = new FileOutputStream(file);
fos.write(new byte[0]);
fos.close();
} catch (IOException ioe) {
throw new BuildException("Could not create "+file, ioe,
location);
}
}

if (project.getJavaVersion() == Project.JAVA_1_1) {
return;
}

if (setLastModified == null) {
synchronized (lockReflection) {
if (setLastModified == null) {
try {
setLastModified =
java.io.File.class.getMethod("setLastModified",
new Class[] {Long.TYPE});
} catch (NoSuchMethodException nse) {
throw new BuildException("File.setlastModified not in JDK > 1.1?",
nse, location);
}
}
}
}
Long[] times = new Long[1];
if (millis < 0) {
times[0] = new Long(System.currentTimeMillis());
} else {
times[0] = new Long(millis);
}

try {
project.log("Setting modification time for "+file,
Project.MSG_VERBOSE);

setLastModified.invoke(file, times);
} catch (InvocationTargetException ite) {
Throwable nested = ite.getTargetException();
throw new BuildException("Exception setting the modification time of "
+ file, nested, location);
} catch (Throwable other) {
throw new BuildException("Exception setting the modification time of "
+ file, other, location);
}
}

}

+ 13
- 31
src/main/org/apache/tools/ant/taskdefs/Untar.java View File

@@ -57,8 +57,6 @@ package org.apache.tools.ant.taskdefs;
import org.apache.tools.ant.*; import org.apache.tools.ant.*;
import org.apache.tools.tar.*; import org.apache.tools.tar.*;
import java.io.*; import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;


/** /**
* Untar a file. * Untar a file.
@@ -78,35 +76,23 @@ public class Untar extends Task {
*/ */
public void execute() throws BuildException { public void execute() throws BuildException {


Method setLastModified = null;
Long[] times = null;
// 1.0 is ruled out anyway, so this ensures 1.2 or above
if (project.getJavaVersion() != Project.JAVA_1_1) {
try {
setLastModified =
java.io.File.class.getMethod("setLastModified",
new Class[] {Long.TYPE});

times = new Long[1];
} catch (Exception e) {
project.log("File.setLastModified(long) not found",
Project.MSG_VERBOSE);
}
}
Touch touch = new Touch();
touch.setProject(project);
touch.setTarget(target);
try { try {
if (source == null) { if (source == null) {
throw new BuildException("No source specified");
throw new BuildException("No source specified", location);
} }
File srcF=project.resolveFile(source); File srcF=project.resolveFile(source);
if (!srcF.exists()) { if (!srcF.exists()) {
throw new BuildException("source doesn't exist");
throw new BuildException("source "+srcF+" doesn't exist",
location);
} }


if (dest == null) { if (dest == null) {
throw new BuildException("No destination specified");
throw new BuildException("No destination specified", location);
} }

File dir=project.resolveFile(dest); File dir=project.resolveFile(dest);


project.log("Expanding: " + srcF + " into " + dir, Project.MSG_INFO); project.log("Expanding: " + srcF + " into " + dir, Project.MSG_INFO);
@@ -117,7 +103,8 @@ public class Untar extends Task {
while ((te = tis.getNextEntry()) != null) { while ((te = tis.getNextEntry()) != null) {
try { try {
File f = new File(dir, project.translatePath(te.getName())); File f = new File(dir, project.translatePath(te.getName()));
project.log("expand-file " + te.getName() , "untar", Project.MSG_VERBOSE );
project.log("expand-file " + te.getName() , "untar",
Project.MSG_VERBOSE );
// create intermediary directories - sometimes tar don't add them // create intermediary directories - sometimes tar don't add them
File dirF=new File(f.getParent()); File dirF=new File(f.getParent());
dirF.mkdirs(); dirF.mkdirs();
@@ -136,15 +123,10 @@ public class Untar extends Task {
fos.close(); fos.close();
} }


if (setLastModified != null) {
times[0] = new Long(te.getModTime().getTime());
try {
setLastModified.invoke(f, times);
} catch (Exception e) {
project.log("cannot invoke File.setLastModified(long)",
Project.MSG_VERBOSE);
setLastModified = null;
}
if (project.getJavaVersion() != Project.JAVA_1_1) {
touch.setFile(f.getAbsolutePath());
touch.setMillis(te.getModTime().getTime());
touch.touch();
} }


} catch(FileNotFoundException ex) { } catch(FileNotFoundException ex) {


+ 1
- 0
src/main/org/apache/tools/ant/taskdefs/defaults.properties View File

@@ -32,6 +32,7 @@ rename=org.apache.tools.ant.taskdefs.Rename
patch=org.apache.tools.ant.taskdefs.Patch patch=org.apache.tools.ant.taskdefs.Patch
compileTask=org.apache.tools.ant.taskdefs.CompileTask compileTask=org.apache.tools.ant.taskdefs.CompileTask
style=org.apache.tools.ant.taskdefs.XSLTProcess style=org.apache.tools.ant.taskdefs.XSLTProcess
touch=org.apache.tools.ant.taskdefs.Touch


# optional tasks # optional tasks
script=org.apache.tools.ant.taskdefs.optional.Script script=org.apache.tools.ant.taskdefs.optional.Script


Loading…
Cancel
Save