[英]In javascript running from Ant, how can you get an argument value?
I'm defining a macrodef in Ant
, and using javascript
to do the work. 我正在
Ant
定义一个macrodef,并使用javascript
来完成工作。 In this case I'm validating a timezone
. 在这种情况下,我正在验证
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. 问题是
project.getProperty()
不检索传递的属性的值。 Does somebody know how you could get the value of the attribute from within the javascript? 有人知道您如何从javascript中获取属性的值吗?
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
. 为了使用脚本定义蚂蚁任务,我应该使用
scriptdef
而不是macrodef
。 With scriptdef
there are predefined objects to access the attributes and nested elements in your task. 使用
scriptdef
,可以使用预定义的对象来访问任务中的属性和嵌套元素。
This works for accessing attributes from javascript in Ant: 这适用于从Ant中的javascript访问属性:
<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, ie 最好的做法是创建一个属性,将属性作为值,即
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.