Ant:如何编写可选的嵌套元素 [英] 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.)
Ant 会抱怨我的任务不支持嵌套的
,这很公平.我一直在思考这些问题:
Ant will complain that my task doesn't support nested <if>
which is fair enough. I've been thinking along these lines:
我可以像这样添加一个带有元素"属性的宏定义:
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>
但这意味着调用者(目标)必须指定我想要避免的附加路径(如果许多目标调用此任务,他们将不得不重复 additional-path
元素).
But that would mean that the caller (target) must specify the additional path which I want to avoid (if many targets call this task, they would have to repeat the fileset definitions in the additional-path
element).
如何在macrodef内对附加文件集进行编码,以便Ant 不会抱怨?
How to code the additional filesets inside the macrodef so that Ant doesn't complain?
推荐答案
实现这一目标的一种方法(不确定是否好)是创建两个宏定义 - 一个用于一般用途的公共"和一个用于通用的内部"真正的工作,旨在仅从公共"宏调用.像这样:
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.
这篇关于Ant:如何编写可选的嵌套元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!