蚂蚁:如何写可选的嵌套元素 [英] Ant: how to write optional nested elements
问题描述
说我需要做的是这样的:
Say that I need to do something like:
<copy todir="${DEPLOYMENT_DIR}" overwrite="true">
<fileset dir="dir1" />
<fileset dir="dir2" />
<fileset dir="dir3" />
...
<if>
<equals arg1="${SPECIAL_BUILD}" arg2="true"/>
<then>
<fileset dir="dir7" />
<fileset dir="dir8" />
...
</then>
</if>
</copy>
(真正的任务是不可复制的,我只是用它来说明这一点。)
(The real task is not copy, I'm just using it to illustrate the point.)
蚂蚁会抱怨说,我的任务不支持嵌套&LT;如果&GT;
这是不够公平。我一直在这样的思考:
Ant will complain that my task doesn't support nested <if>
which is fair enough. I've been thinking along these lines:
我可以添加一个macrodef与元素属性是这样的:
I could add a macrodef with an "element" attribute like this:
<macrodef name="myCopy">
<element name="additional-path" />
<sequential>
<copy todir="${DEPLOYMENT_DIR}" overwrite="true">
<fileset dir="dir1" />
<fileset dir="dir2" />
<fileset dir="dir3" />
...
<additional-path/>
</copy>
</sequential>
</macrodef>
不过,这将意味着,主叫方(目标)必须指定我想避免(如果许多目标调用这个任务,他们将不得不重复在的文件集定义附加的路径附加路径
元素)。
如何code中的附加文件集的 的内部macrodef使蚂蚁不抱怨吗?
How to code the additional filesets inside the macrodef so that Ant doesn't complain?
推荐答案
的一种方式(不知道一个好的)来实现的是创建两个macrodefs - 公用用于一般用途,一个内部,做真正的工作,并打算只从公的宏调用。像这样的:
One way (not sure if a good one) to achieve that is to create two macrodefs - one "public" for general use and one "internal" that does the real work and is intended to be called only from the "public" macro. Like this:
<macrodef name="task-for-public-use">
<sequential>
<if>
<equal arg1="${SPECIAL_BUILD}" arg2="true" />
<then>
<internal-task>
<additional-path>
...
</additional-path>
</internal-task>
</then>
<else>
<internal-task ... />
</else>
</if>
</sequential>
</macrodef>
<macrodef name="internal-task">
<element name="additional-path" />
<sequential>
<copy ...>
...
<additional-path/>
</copy>
</sequential>
</macrodef>
我不喜欢它虽然很多,希望有一个更好的办法。
I don't like it much though and hope there's a better way.
这篇关于蚂蚁:如何写可选的嵌套元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!