It depends on your OS, f.e. Unix doesn't store the file creation time, see details here
Two possible solutions :
Solution 1, works on Windows only with Java >= 6, no addons needed
<project>
<!-- Works on Windows only, uses the jdk builtin
rhino javascript engine (since jdk6)
use dir command without /T:C to get 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 and groovy-all-x.x.x.jar (contained in groovy binary release)
Adjust the SimpleDateFormat to your liking.
On Unix filesystems when asking for creationtime you'll get the last modification time.
<project>
<taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy"/>
<!-- Solution for Java 7, uses the nio package
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 also using the builtin javascript engine, but i got errors like :
sun.org.mozilla.javascript.internal.EvaluatorException: missing name after . operator
IMO, for simple things using javascript <script language="javascript">
is sufficient, but if you need to import java packages etc. .. it's a PITA. Groovy simply works.