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

问题描述

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

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