本文介绍了在从 Ant 运行的 javascript 中,如何获得参数值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Ant 中定义了一个宏定义,并使用 javascript 来完成这项工作.在这种情况下,我正在验证 timezone.

I'm defining a macrodef in Ant, and using javascript to do the work. In this case I'm validating a timezone.

<macrodef name="validateTimeZone">
    <attribute name="zone" />
    <sequential>
        <echo>result: ${envTZResult}</echo>
        <echo>  validating timezone: @{zone}</echo>
        <script language="javascript"><![CDATA[
            importClass(java.util.TimeZone);
            importClass(java.util.Arrays);
            var tz = project.getProperty("zone");
            println("    got attribute: " + tz);
            var result = Arrays.asList(TimeZone.getAvailableIDs()).contains(tz); //testing if timezone is known
            project.setProperty("zoneIsValid", result);
        ]]> 
        </script>
    </sequential>
</macrodef>

问题是 project.getProperty() 不检索传递属性的值.有人知道如何从 javascript 中获取属性的值吗?

The problem is project.getProperty() doesn't retrieve values of passed attributes. Does somebody know how you could get the value of the attribute from within the javascript?

推荐答案

原来我使用了错误类型的标签.为了使用脚本来定义 ant 任务,我应该使用 scriptdef 而不是 macrodef.使用 scriptdef 有预定义的对象来访问任务中的属性和嵌套元素.

Turns out I was using the wrong type of tag. For using scripting to define an ant task, I should have used scriptdef and not macrodef. With scriptdef there are predefined objects to access the attributes and nested elements in your task.

这适用于从 Ant 中的 javascript 访问属性:

This works for accessing attributes from javascript in Ant:

<scriptdef name="validateTimeZone" language="javascript">
    <attribute name="zone" />
    <![CDATA[
        importClass(java.util.TimeZone);
        importClass(java.util.Arrays);
        var tz = attributes.get("zone"); //get attribute defined for scriptdef
        println("    got attribute: " + tz);
        var result = Arrays.asList(TimeZone.getAvailableIDs()).contains(tz); //testing if timezone is known
        project.setProperty("zoneIsValid", result);
    ]]> 
</scriptdef>

这篇关于在从 Ant 运行的 javascript 中,如何获得参数值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 17:41