Hi,
I've created a patch wich gives the copy task a preservelastmodified attribute.
This attribute will make the last modified time of the copied file the same as
the original file (defaults to "no"). Any comments on this patch?
Jaco
Index: Project.java
===================================================================
RCS file:
/home/cvspublic/jakarta-ant/src/main/org/apache/tools/ant/Project.java,v
retrieving revision 1.48
diff -u -r1.48 Project.java
--- Project.java 2000/11/28 02:51:45 1.48
+++ Project.java 2000/11/28 10:17:58
@@ -57,6 +57,7 @@
import java.io.*;
import java.util.*;
import java.text.*;
+import java.lang.reflect.*;
/**
* Central representation of an Ant project. This class defines a
@@ -107,6 +108,9 @@
private Vector listeners = new Vector();
+ private static Method setLastModified = null;
+ private static Object lockReflection = new Object();
+
static {
// Determine the Java version by looking at available classes
@@ -673,6 +677,22 @@
}
/**
+ * Convienence method to copy a file from a source to a
+ * destination specifying if token filtering must be used, if
+ * source files may overwrite newer destination files and the
+ * last modified time of <code>destFile</code> file should be made equal
+ * to the last modified time of <code>sourceFile</code>.
+ *
+ * @throws IOException
+ */
+ public void copyFile(String sourceFile, String destFile, boolean filtering,
+ boolean overwrite, boolean preserveLastModified)
+ throws IOException {
+ copyFile(new File(sourceFile), new File(destFile), filtering,
+ overwrite, preserveLastModified);
+ }
+
+ /**
* Convienence method to copy a file from a source to a destination.
* No filtering is performed.
*
@@ -689,8 +709,7 @@
* @throws IOException
*/
public void copyFile(File sourceFile, File destFile, boolean filtering)
- throws IOException
- {
+ throws IOException {
copyFile(sourceFile, destFile, filtering, false);
}
@@ -703,6 +722,21 @@
*/
public void copyFile(File sourceFile, File destFile, boolean filtering,
boolean overwrite) throws IOException {
+ copyFile(sourceFile, destFile, filtering, overwrite, false);
+ }
+
+ /**
+ * Convienence method to copy a file from a source to a
+ * destination specifying if token filtering must be used, if
+ * source files may overwrite newer destination files and the
+ * last modified time of <code>destFile</code> file should be made equal
+ * to the last modified time of <code>sourceFile</code>.
+ *
+ * @throws IOException
+ */
+ public void copyFile(File sourceFile, File destFile, boolean filtering,
+ boolean overwrite, boolean preserveLastModified)
+ throws IOException {
if (overwrite ||
destFile.lastModified() < sourceFile.lastModified()) {
@@ -750,6 +784,51 @@
in.close();
out.close();
}
+ if (preserveLastModified) {
+ setFileLastModified(destFile, sourceFile.lastModified());
+ }
+ }
+ }
+
+ /**
+ * Calls File.setLastModified(long time) in a Java 1.1 compatible way.
+ */
+ void setFileLastModified(File file, long time) throws BuildException {
+ if (getJavaVersion() == JAVA_1_1) {
+ log("Cannot change the modification time of " + file
+ + " in JDK 1.1",
+ Project.MSG_WARN);
+ 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);
+ }
+ }
+ }
+ }
+ Long[] times = new Long[1];
+ if (time < 0) {
+ times[0] = new Long(System.currentTimeMillis());
+ } else {
+ times[0] = new Long(time);
+ }
+ try {
+ log("Setting modification time for " + file, MSG_VERBOSE);
+ setLastModified.invoke(file, times);
+ } catch (InvocationTargetException ite) {
+ Throwable nested = ite.getTargetException();
+ throw new BuildException("Exception setting the modification time "
+ + "of " + file, nested);
+ } catch (Throwable other) {
+ throw new BuildException("Exception setting the modification time "
+ + "of " + file, other);
}
}
Index: taskdefs/Copy.java
===================================================================
RCS file:
/home/cvspublic/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Copy.java,v
retrieving revision 1.10
diff -u -r1.10 Copy.java
--- taskdefs/Copy.java 2000/11/23 14:43:24 1.10
+++ taskdefs/Copy.java 2000/11/28 10:17:59
@@ -83,6 +83,7 @@
protected boolean filtering = false;
protected boolean forceOverwrite = false;
protected boolean flatten = false;
+ protected boolean preserveLastModified = false;
protected int verbosity = Project.MSG_VERBOSE;
protected boolean includeEmpty = true;
@@ -138,6 +139,13 @@
}
/**
+ * Give the copied files the same last modified time as the original files.
+ */
+ public void setPreserveLastModified(String preserve) {
+ preserveLastModified = Project.toBoolean(preserve);
+ }
+
+ /**
* Used to force listing of all names of copied files.
*/
public void setVerbose(boolean verbose) {
@@ -320,7 +328,8 @@
project.copyFile(fromFile,
toFile,
filtering,
- forceOverwrite);
+ forceOverwrite,
+ preserveLastModified);
} catch (IOException ioe) {
String msg = "Failed to copy " + fromFile + " to " + toFile
+ " due to " + ioe.getMessage();