最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - In javascript running from Ant, how can you get an argument value? - Stack Overflow

programmeradmin4浏览0评论

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>

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?

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>

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?

Share Improve this question edited Apr 19, 2013 at 4:57 salemkhoo 3251 gold badge2 silver badges12 bronze badges asked Apr 19, 2013 at 4:48 TimTim 9661 gold badge13 silver badges23 bronze badges 1
  • This may help: ant.apache.org/manual/Tasks/script.html - Look at the example half way down. – techfoobar Commented Apr 19, 2013 at 5:12
Add a comment  | 

2 Answers 2

Reset to default 13

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.

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>

Best is to create a property with attribute as value, i.e.

<macrodef name="validateTimeZone">
    <attribute name="zone" />
    <sequential>
        <echo>result: ${envTZResult}</echo>
        <echo>  validating timezone: @{zone}</echo>
        <!-- edit use local with ant 1.8.x -->
        <local name="zone"/>
        <property name="zone" value="@{zone}"/>
        <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>
发布评论

评论列表(0)

  1. 暂无评论