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

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

问题描述

我是一个Ant构建一个OpenCms的项目中写的manifest.xml 文件。

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

我需要能够拉起一个文件的创建日期和最后修改日期的文件。 (虽然目前的进程给每个文件的时间戳周三,1969年12月31日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.

推荐答案

这取决于您的操作系统,F.E. Unix的不存储文件的创建时间,这里查看详细信息结果
两种可能的解决方案:结果

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

解决方法1,适用于Windows只能在Java> = 6,无需插件

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的全2.1.0.jar(包含在的常规二进制版本)结果调整的SimpleDateFormat根据自己的喜好。结果
在Unix文件系统要求CREATIONTIME时,你会得到最后的修改时间。

Solution 2, needs Java 7 and groovy-all-2.1.0.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的简单的事情&LT; SCRIPT LANGUAGE =javascipt的&GT; 就足够了,但如果你需要导入Java包等..这是一个PITA。 Groovy的简单工作。

IMO, for simple things using javascript <script language="javascipt"> is sufficient, but if you need to import java packages etc. .. it's a PITA. Groovy simply works.

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

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