Tuesday, 15 June 2010

Ant get file creation timestamp -



Ant get file creation timestamp -

i writing manifest.xml file during ant build opencms project.

i need able pull file's create date, , lastly modified date on file. (although current process giving each file timestamp of wed, 31 dec 1969 19:00:00 est anyway -- @ to the lowest degree on windows machines when run build.)

is there way can pull creation date timestamp of file in ant? i'm using standard ant tasks , ant-contrib tasks.

it depends on os, f.e. unix doesn't store file creation time, see details here 2 possible solutions :

solution 1, works on windows java >= 6, no addons needed

<project> <!-- works on windows only, uses jdk builtin rhino javascript engine (since jdk6) utilize dir command without /t:c lastmodificationtime --> <macrodef name="getfiletimes"> <attribute name="dir" /> <attribute name="file" /> <attribute name="setprop" default="@{file}_ctime" /> <sequential> <exec executable="cmd" dir="@{dir}" outputproperty="@{setprop}"> <arg value="/c" /> <arg line="dir @{file} /t:c|find ' @{file}'" /> </exec> <script language="javascript"> tmp = project.getproperty("@{setprop}").split("\\s+") ; project.setproperty("@{setprop}", tmp[0] + "/" + tmp[1]) ; </script> </sequential> </macrodef> <getfiletimes dir="c:/tmp" file="bookmarks.html" /> <echo> $${bookmarks.html_ctime} => ${bookmarks.html_ctime} </echo> </project>

solution 2, needs java 7 , groovy-all-2.1.0.jar (contained in groovy binary release)adjust simpledateformat liking. on unix filesystems when asking creationtime you'll lastly modification time.

<project> <taskdef name="groovy" classname="org.codehaus.groovy.ant.groovy"/> <!-- solution java 7, uses nio bundle needs groovy-all-2.1.0.jar --> <macrodef name="getfiletimes"> <attribute name="file"/> <attribute name="ctimeprop" default="@{file}_ctime"/> <attribute name="mtimeprop" default="@{file}_mtime"/> <sequential> <groovy> import java.nio.file.* import java.nio.file.attribute.* import java.text.* import java.util.date.* path path = paths.get("@{file}") basicfileattributeview view = files.getfileattributeview(path, basicfileattributeview.class) basicfileattributes attributes = view.readattributes() lastmodifiedtime = attributes.lastmodifiedtime() createtime = attributes.creationtime() dateformat df = new simpledateformat("dd-mmm-yyyy hh:mm:ss", locale.us) df.format(new date(createtime.tomillis())) properties.'@{ctimeprop}' = df.format(new date(createtime.tomillis())) properties.'@{mtimeprop}' = df.format(new date(lastmodifiedtime.tomillis())) </groovy> </sequential> </macrodef> <getfiletimes file="c:/tmp/bookmarks.html"/> <echo> $${c:/tmp/bookmarks.html_ctime} => ${c:/tmp/bookmarks.html_ctime} $${c:/tmp/bookmarks.html_mtime} => ${c:/tmp/bookmarks.html_mtime} </echo> </project>

i tried using builtin javascript engine, got errors :

sun.org.mozilla.javascript.internal.evaluatorexception: missing name after . operator

imo, simple things using javascript <script language="javascipt"> sufficient, if need import java packages etc. .. it's pita. groovy works.

ant

No comments:

Post a Comment