Ant 获取文件创建时间戳 [英] Ant get file creation timestamp

查看:21
本文介绍了Ant 获取文件创建时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 OpenCMS 项目构建 Ant 期间编写一个 manifest.xml 文件.

I am writing a manifest.xml file during an Ant build for an OpenCMS project.

我需要能够提取文件的创建日期和文件的上次修改日期.(尽管当前进程为每个文件提供了 Wed, 31 Dec 1969 19:00:00 EST 的时间戳 - 至少在运行构建时的 Windows 机器上是这样.)

I need to be able to pull up a file's create date, and last modified date on a file. (Although the current process is giving each file a timestamp of Wed, 31 Dec 1969 19:00:00 EST anyway -- at least on Windows machines when they run the build.)

有没有办法在 Ant 中提取文件的创建日期时间戳?我正在使用标准 Ant 任务和 Ant-Contrib 任务.

Is there a way I can pull up the creation date timestamp of a file in Ant? I'm using standard Ant tasks and Ant-Contrib tasks.

推荐答案

这取决于您的操作系统,例如Unix 不存储文件创建时间,在此处查看详细信息
两种可能的解决方案:

It depends on your OS, f.e. Unix doesn't store the file creation time, see details here
Two possible solutions :

解决方案 1,仅适用于 Java >= 6 的 Windows,不需要插件

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>

解决方案 2,需要 Java 7 和 groovy-all-xxxjar(包含在 groovy 二进制版本)
根据您的喜好调整 SimpleDateFormat.
在 Unix 文件系统上,当要求创建时间时,您将获得最后修改时间.

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>

我也尝试过使用内置的 javascript 引擎,但我遇到了如下错误:

I tried also using the builtin javascript engine, but i got errors like :

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

IMO,对于简单的事情,使用 javascript <script language="javascript"> 就足够了,但是如果您需要导入 java 包等......这是一个 PITA.Groovy 很简单.

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.

这篇关于Ant 获取文件创建时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆