Any way of parametrizing whether or not to append inner elements in ant?
I am writing an ant file for compiling a flex project (but this question may apply to non-flex ant scripts as well).
I had several targets there that look like this:
<target name="first">
<mxmlc file="${src.dir}/FirstClass.as" output="${output.dir}/First.swf" ...identical_compiler_attributes...>
...identical_compiler_inner_elements...
<compiler.define name="AN_ATTRIBUTE" value="A_VALUE" />
</mxmlc>
</target>
<target name="second">
<mxmlc file="${src.dir}/SecondClass.as" output="${output.dir}/Second.swf" ...identical_compiler_attributes...>
...identical_compiler_inner_elements...
<!-- no additional compiler.define calls needed -->
</mxmlc>
</target>
I wanted to avoid duplication of common mxmlc attributes and inner element by using the <antcall>
ant task, so I came up with something like this:
<target name="first">
<antcall target="helper_target">
<param name="src.file" value="FirstClass.as"/>
<param name="output.file" value="First.swf"/>
</antcall>
</target>
<target name="second">
<antcall target="helper_target">
<param name="src.file" value="SecondClass.as"/>
<param name="output.file" value="Second.swf"/>
</antcall>
</target>
<target name="helper_target">
<mxmlc file="${src.dir}/${src.file}" output="${output.dir}/${output.file}" ...identical_compiler_attributes...>
...identical_compiler_inner_ele开发者_开发知识库ments...
<!-- WHAT DO I DO ABOUT THE compiler.define?? -->
</mxmlc>
</target>
This solves most duplication nicely. But what do I do about the <compiler.define>
and other inner elements that differ between the mxmlc calls? The builtin if
mechanism of ant doesn't help me here - i can't invoke a target in the middle of an mxmlc element....
Any ideas? (I know ant-contrib has some kind of if mechanism. Would rather have a pure-ant solution, and not even sure if ant-contrib's if will help here).
This sounds like a candidate for an Ant presetdef
task. The manual describes the task thus:
The preset definition generates a new definition based on a current definition with some attributes or elements preset.
I can't provide an example for mxmlc
as I don't have Flex here. But here's an example using an exec
task:
<presetdef name="exec.preset">
<exec executable="sh" dir=".">
<arg value="-c" />
<arg value="echo" />
</exec>
</presetdef>
<exec.preset>
<arg value="hello world" />
</exec.preset>
If you run this using ant -verbose
you'll see
exec.preset] Executing 'sh' with arguments:
[exec.preset] '-c'
[exec.preset] 'echo'
[exec.preset] 'hello world'
[exec.preset]
The extra arg provided in the preset call is added to the exec.preset - which is just what you want.
精彩评论