使用本机Ant任务执行简单的计算 [英] Performing Simple Calculations with Native Ant Tasks

查看:89
本文介绍了使用本机Ant任务执行简单的计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仅使用本机ANT任务,如何创建自定义ANT任务来执行以下操作:

Using only native ANT tasks, how can I create a custom ANT task to do the following:

  • 计算自2000年1月1日本地时间以来的天数,并将其存储在属性中.
  • 计算自当地时间午夜以来的秒数,除以2,并将其存储在属性中.

然后将上述属性值附加到其他属性值并写入文件.

The above property values will then be appended to others and written to a file.

推荐答案

ANT不是通用的编程语言,因此您需要编写自定义任务或使用

ANT is not a general purpose programming language, so you need to write a custom task or alternatively use something like the groovy plugin

以下示例演示了使用 Joda Time 库的常规任务如何将属性设置为您已指定.

The following example demonstrates how a groovy task using the Joda Time library can set the properties as you've specified.

<taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy" classpathref="build.path"/>
<groovy>
    import org.joda.time.*

    def now      = new DateTime()
    def midnight = new DateMidnight()
    def year2000 = new DateTime(2000,1,1,0,0,0,0)

    properties["year2000.days"] = Days.daysBetween(year2000, now).days
    properties["midnight.seconds"] = Seconds.secondsBetween(midnight, now).seconds
    properties["midnight.seconds.halved"] = Seconds.secondsBetween(midnight, now).dividedBy(2).seconds
</groovy>

我不能推荐Joda Time足够高,Java中的标准日期和时间操作实在太烂了!

I can't recommend Joda Time highly enough, standard Date and Time manipulation in Java just sucks!

上面的常规任务将在您的类路径上需要以下jar:

The groovy task above will require the following jars on your classpath:

  • groovy-all-1.7.4.jar
  • joda-time-1.6.1.jar

我建议使用常春藤插件,通过添加一个解析"目标来管理这些目标,该目标可以下载jar并自动设置类路径:

I'd recommend using the ivy plugin to manage these by adding a "resolve" target that downloads the jars and sets the classpath automatically:

<target name="resolve">
    <ivy:resolve/>
    <ivy:cachepath pathid="build.path"/>
</target>

以下是 ivy.xml ,其中列出了要下载的依赖项:

The following is the ivy.xml that lists the dependencies to be downloaded:

<ivy-module version="2.0">
    <info organisation="org.myspotontheweb" module="demo"/>
    <dependencies>
        <dependency org="org.codehaus.groovy" name="groovy-all" rev="1.7.4" conf="default"/>
        <dependency org="joda-time" name="joda-time" rev="1.6.1" conf="default"/>
    </dependencies>
</ivy-module>

这篇关于使用本机Ant任务执行简单的计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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