Variable number of parameters using <macrodef> and exec

I use exec to invoke the command line program several times from ant build.xml. This command line program accepts a variable number of arguments for different occasions.

I am currently invoking this external program several times using exec and code that looks cluttered. For example:

<exec dir="./deploy_abc/bin" executable="python" failonerror="true" >
    <arg line="tests.py"/>
    <arg line="-h aaa"/>
    <arg line="-u bbb"/>
    <arg line="-p ccc"/>
</exec>

<exec dir="./deploy_abc/bin" executable="python" failonerror="true" >
    <arg line="tests.py"/>
    <arg line="-h ddd"/>
    <arg line="-u eee"/>
    <arg line="-p fff"/>
    <arg value="this is second test"/>
</exec>

<exec dir="./deploy_abc/bin" executable="python" failonerror="true" >
    <arg line="tests.py"/>
    <arg line="-u bbb"/>
    <arg line="-p ccc"/>
    <arg value="this is another test"/>
</exec>

So, I plan to reorganize this build.xml file using macrodef.

My question is how to pass a variable number of parameters to macro definition. As shown above, I need to pass different arguments to a script-based executable.

+4
source share
1 answer

You can use to support this: macrodef element

. templated task .

, :

<macrodef name="call-exec">
   <attribute name="dir"/>
   <attribute name="executable"/>
   <attribute name="failonerror"/>
   <element name="arg-elements"/>
   <sequential>
      <exec dir="@{dir}" executable="@{executable}"
                       failonerror="@{failonerrer}" >
         <arg-elements />
      </exec>
   </sequential>
</macrodef>

:

<call-exec dir="./deploy_abc/bin" executable="python" failonerror="true" >
  <arg-elements>
    <arg line="tests.py"/>
    <arg line="-u bbb"/>
    <arg line="-p ccc"/>
    <arg value="this is another test"/>
  </arg-elements>
</call-exec>
+7

All Articles