From 1f4fe213e2f3268419fb74d79bfb3ff4a3de77fe Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Thu, 29 Jun 2000 10:23:41 +0000 Subject: [PATCH] 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 --- docs/index.html | 47 ++++- src/main/org/apache/tools/ant/Task.java | 2 +- .../org/apache/tools/ant/taskdefs/Expand.java | 12 ++ .../org/apache/tools/ant/taskdefs/Touch.java | 198 ++++++++++++++++++ .../org/apache/tools/ant/taskdefs/Untar.java | 44 ++-- .../tools/ant/taskdefs/defaults.properties | 1 + 6 files changed, 271 insertions(+), 33 deletions(-) create mode 100644 src/main/org/apache/tools/ant/taskdefs/Touch.java diff --git a/docs/index.html b/docs/index.html index 99fdd70c8..9fb392d67 100644 --- a/docs/index.html +++ b/docs/index.html @@ -22,7 +22,7 @@
  • Sam Ruby (rubys@us.ibm.com)
  • -

    Version 1.0.8.1 - 2000/06/27

    +

    Version 1.0.8.1 - 2000/06/28


    Table of Contents

    @@ -525,6 +525,7 @@ If you do not want these default excludes applied, you may disable them with the
  • Rmic
  • Tar
  • Taskdef
  • +
  • Touch
  • Tstamp
  • Style
  • Untar
  • @@ -1017,6 +1018,8 @@ systems.

    Expand

    Description

    Unzips a zipfile.

    +

    For JDK 1.1 "last modified time" field is set to current time instead of being +carried from zipfile.

    Parameters

    @@ -2426,6 +2429,48 @@ href="#writingowntask">Writing your own task".

    makes a task called myjavadoc available to Ant. The class com.mydomain.JavadocTask implements the task.


    +

    Touch

    +

    Description

    +

    Changes the modification time of a file and possibly creates it at +the same time.

    +

    For JDK 1.1 only the creation of new files with a modification time +of now works, all other cases will emit a warning.

    +

    Parameters

    +
    + + + + + + + + + + + + + + + + + + + + +
    AttributeDescriptionRequired
    filethe name of the fileYes
    millisspecifies the new modification time of the file + in milliseconds since midnight Jan 1 1970No
    datetimespecifies the new modification time of the file + in the format MM/DD/YYYY HH:MM AM_or_PM.No
    +

    If both millis and datetime are ommited +the current time is assumed.

    +

    Examples

    +
      <touch file="myfile" />
    +

    creates myfile if it doesn't exist and changes the +modification time to the current time.

    +
      <touch file="myfile" datetime="06/28/2000 2:02 pm" />
    +

    creates myfile 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).

    +

    Tstamp

    Description

    Sets the DSTAMP, TSTAMP and TODAY properties in the current project. The diff --git a/src/main/org/apache/tools/ant/Task.java b/src/main/org/apache/tools/ant/Task.java index daf7fc1d2..a5949bfda 100644 --- a/src/main/org/apache/tools/ant/Task.java +++ b/src/main/org/apache/tools/ant/Task.java @@ -73,7 +73,7 @@ public abstract class Task { * * @param project Project in whose scope this task belongs. */ - void setProject(Project project) { + public void setProject(Project project) { this.project = project; } diff --git a/src/main/org/apache/tools/ant/taskdefs/Expand.java b/src/main/org/apache/tools/ant/taskdefs/Expand.java index b354ce9aa..82b153204 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Expand.java +++ b/src/main/org/apache/tools/ant/taskdefs/Expand.java @@ -61,6 +61,7 @@ import java.util.zip.*; * Unzip a file. * * @author costin@dnt.ro + * @author Stefan Bodewig stefan.bodewig@megabit.net */ public class Expand extends Task { private String dest; // req @@ -73,6 +74,10 @@ public class Expand extends Task { */ // XXX move it to util or tools public void execute() throws BuildException { + Touch touch = new Touch(); + touch.setProject(project); + touch.setTarget(target); + try { File srcF=project.resolveFile(source); File dir=project.resolveFile(dest); @@ -103,6 +108,13 @@ public class Expand extends Task { fos.close(); } + + if (project.getJavaVersion() != Project.JAVA_1_1) { + touch.setFile(f.getAbsolutePath()); + touch.setMillis(ze.getTime()); + touch.touch(); + } + } catch( FileNotFoundException ex ) { System.out.println("FileNotFoundException: " + ze.getName() ); } diff --git a/src/main/org/apache/tools/ant/taskdefs/Touch.java b/src/main/org/apache/tools/ant/taskdefs/Touch.java new file mode 100644 index 000000000..8a2003988 --- /dev/null +++ b/src/main/org/apache/tools/ant/taskdefs/Touch.java @@ -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 + * . + */ + +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. + * + *

    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 stefan.bodewig@megabit.net + */ +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); + } + } + +} diff --git a/src/main/org/apache/tools/ant/taskdefs/Untar.java b/src/main/org/apache/tools/ant/taskdefs/Untar.java index ce197bdb3..d165436e4 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Untar.java +++ b/src/main/org/apache/tools/ant/taskdefs/Untar.java @@ -57,8 +57,6 @@ package org.apache.tools.ant.taskdefs; import org.apache.tools.ant.*; import org.apache.tools.tar.*; import java.io.*; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; /** * Untar a file. @@ -78,35 +76,23 @@ public class Untar extends Task { */ 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 { if (source == null) { - throw new BuildException("No source specified"); + throw new BuildException("No source specified", location); } File srcF=project.resolveFile(source); if (!srcF.exists()) { - throw new BuildException("source doesn't exist"); + throw new BuildException("source "+srcF+" doesn't exist", + location); } if (dest == null) { - throw new BuildException("No destination specified"); + throw new BuildException("No destination specified", location); } - File dir=project.resolveFile(dest); project.log("Expanding: " + srcF + " into " + dir, Project.MSG_INFO); @@ -117,7 +103,8 @@ public class Untar extends Task { while ((te = tis.getNextEntry()) != null) { try { 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 File dirF=new File(f.getParent()); dirF.mkdirs(); @@ -136,15 +123,10 @@ public class Untar extends Task { 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) { diff --git a/src/main/org/apache/tools/ant/taskdefs/defaults.properties b/src/main/org/apache/tools/ant/taskdefs/defaults.properties index 8093640c7..6fb1354f5 100644 --- a/src/main/org/apache/tools/ant/taskdefs/defaults.properties +++ b/src/main/org/apache/tools/ant/taskdefs/defaults.properties @@ -32,6 +32,7 @@ rename=org.apache.tools.ant.taskdefs.Rename patch=org.apache.tools.ant.taskdefs.Patch compileTask=org.apache.tools.ant.taskdefs.CompileTask style=org.apache.tools.ant.taskdefs.XSLTProcess +touch=org.apache.tools.ant.taskdefs.Touch # optional tasks script=org.apache.tools.ant.taskdefs.optional.Script