在Ant属性中存储UNIX时间戳?

时间:2011-04-14 15:35:06

标签: java ant timestamp

我想在Ant属性中存储UNIX时间戳(即自纪元以来的秒数),以便稍后在几个构建目标中使用。这似乎是不可能的:

<tstamp>
  <format property="build.time" />
</tstamp>`

...生成格式化的时间戳。

<propertyfile file="foo.properties">
    <entry key="build.time" type="date" default="now" />
</propertyfile>

...还会生成格式化的时间戳。

我希望在不使用<exec>或类似内容的情况下实现这一点(因为我们有时会在Windows上运行构建)。

1 个答案:

答案 0 :(得分:5)

快速谷歌提出:

http://www.norio.be/blog/2010/08/getting-unix-time-epoch-ant-build-file

<target name="print-epoch">
  <script language="javascript">
  <![CDATA[
    property = project.setProperty("now",Math.floor((new Date()).getTime()/1000));
  ]]>
  </script>
  <echo message="${now}" />
</target>

其他方法将是更清洁的IMO

  1. 创建自己的自定义反任务。这真的不那么难; http://ant.apache.org/manual/develop.html

  2. 使用Maven exec插件执行Java来执行此操作: http://mojo.codehaus.org/exec-maven-plugin/