3

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

我需要能够提取文件的创建日期和文件的最后修改日期。(尽管当前进程Wed, 31 Dec 1969 19:00:00 EST无论如何都会给每个文件一个时间戳——至少在 Windows 机器上运行构建时是这样。)

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

4

4 回答 4

4

我有这个工作。

正如Mark O'Connor所指出的,您无法从早期版本的 Java 中获取文件创建时间。但是,用于此任务的原始 Java 程序对创建日期和最后修改日期1都使用了lastModified方法。我做同样的事情很好。

我创建了一个<scriptdef>从文件中提取上次修改日期的方法。在 Java 1.6 及更高版本中,您可以直接访问 Rhino JavaScript 库,因此您不再需要 BeanShell 库。

<scriptdef name="file.mdate" language="javascript"> 
    <attribute name="file"/> 
    <attribute name="property"/> 
        file_name = attributes.get("file"); 
        property_to_set = attributes.get("property");

        file = new java.io.File(file_name); 
        file_date = file.lastModified();

        date_format = new java.text.SimpleDateFormat("EEE, dd MMM YYYY HH:mm:ss zzz");
        formated_date = date_format.format(new java.util.Date(file_date));
        project.setNewProperty(property_to_set, formated_date);
</scriptdef> 

定义后,我可以将其用作 Ant 任务:

<file.mdate
    file="${file.name}"
    property="file.modified.date"/>
<echo>The file "${file}" was modified on ${file.modified.date}</echo>
于 2013-02-06T22:15:33.080 回答
2

这取决于您的操作系统,fe Unix 不存储文件创建时间,请参阅此处
的详细信息 两种可能的解决方案:

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

<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 文件系统上,当询问创建时间时,您将获得最后修改时间。

<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 引擎,但出现如下错误:

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

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

于 2013-02-06T23:11:23.343 回答
1

问题是标准 Java File 对象只支持返回最后修改日期的方法:

Java 7 使用新的 NIO 类解决了这个问题:

显然,要在 ANT 中利用这一点,您需要编写自定义任务或嵌入脚本。

于 2013-02-05T23:20:59.117 回答
0

David Ws 的回答中有一个错误:它在第二次运行时不起作用,因为该属性在第二次运行时不会被覆盖。

变化:project.setNewProperty应该是project.setProperty

完整的代码片段:

<scriptdef name="filedate" language="javascript">
    <attribute name="file"/>
    <attribute name="property"/>
        <![CDATA[
            file_name = attributes.get("file");
            property_to_set = attributes.get("property");

            file = new java.io.File(file_name);
            file_date = file.lastModified();

            date_format = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            formated_date = date_format.format(new java.util.Date(file_date));
            project.setProperty(property_to_set, formated_date);
        ]]>
</scriptdef>
于 2014-09-16T15:50:53.080 回答