我有一个javascript函数,我想在Ant构建文件中定义一次,然后在多个目标中调用它而不必在每个目标中重新定义它。
这是我的职能:
divs = project.getProperty("division").split(",");
runMacro = function(macro_name) {
for (i=0; i < divs.length; i++)
{
try {
if (typeof(divs[i]) != "undefined" && divs[i] != "" && divs[i] !== null) {
macro = project.createTask(macro_name);
macro.setDynamicAttribute("division", divs[i]);
macro.setDynamicAttribute("application-version", "${app-version}");
macro.perform();
} else {
throw "Divison name cannot be null.";
}
}
catch (error) {
print("Something went wrong: ", error);
}
}
}
我想从Ant build.xml中的任何目标调用它
<target name="app-status" description="." >
<script language="javascript">
<![CDATA[
myScript.runMacro("get-app-status");
]]>
</script>
</target>
在Ant脚本中是否可以将JS函数定义为全局函数?
谢谢!
答案 0 :(得分:0)
感谢考斯汀。
是的,可以使用Scriptdef来实现。
我还没有尝试过,但是代码如下:
<scriptdef name="process-divsion" language="javascript">
<attribute name="macro-name"/>
<![CDATA[
divs = project.getProperty('division').split(',');
for (i=0; i < divs.length; i++)
{
try {
print("Processing divison: ", divs[i]);
if (typeof(divs[i]) != "undefined" && divs[i] != "" && divs[i] !== null) {
macro = project.createTask(attributes.get("macro-name"));
macro.setDynamicAttribute( "division", divs[i]);
macro.perform();
}
else {
throw "Division name cannot be null.";
}
}
catch (error) {
print("Something went wrong: ", error);
}
}
]]>
</scriptdef>
然后可以在build.xml中的任何地方调用它
<target name="app-status" >
<process-division macro-name="get-app-status" />
</target>