Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass multiple parameters to a target in Ant?

Tags:

ant

I have this dummy target:

<mkdir dir="${project.stage}/release 
<war destfile="${project.stage}/release/sigma.war">
    ...
    ...
  </war>

What I want to do is provide two parameters say "abc" & "xyz" which will replace the word release with the values of abc and xyz parameters respectively.

For the first parameter say abc="test", the code above will create a test directory and put the war inside it.Similarly for xyz="production" it will create a folder production and put the war file inside it.

I tried this by using

<antcall target="create.war">
    <param name="test" value="${test.param.name}"/>
    <param name="production" value="${prod.param.name}"/>
</antcall>

in the target which depends on the dummy target provided above. Is this the right way to do this.I guess there must be some way to pass multiple parameters and then loop through the parameters one at a time.

like image 577
EMM Avatar asked Sep 26 '11 08:09

EMM


1 Answers

unfortunately ant doesn't support iteration like for or foreach loops unless you are refering to files. There is however the ant contrib tasks which solve most if not all of your iteration problems.

You will have to install the .jar first by following the instructions here : http://ant-contrib.sourceforge.net/#install

This should take about 10 seconds. After you can simply use the foreach task to iterate through you custom list. As an example you can follow the below build.xml file :

<project name="test" default="build">
<!--Needed for antcontrib-->
<taskdef resource="net/sf/antcontrib/antcontrib.properties"/>
<target name="build">
<property name="test" value="value_1"/>
<property name="production" value="value_2"/>

<!--Iterate through every token and call target with parameter dir-->
<foreach list="${test},${production}" param="dir" target="create.war"/>

</target>
<target name="create.war">
    <echo message="My path is : ${dir}"/>
</target>
</project>

Output :

build:

create.war:
 [echo] My path is : value_1

create.war:
 [echo] My path is : value_2

BUILD SUCCESSFUL
Total time: 0 seconds

I hope it helps :)

Second solution without using ant contrib. You could encapsulate all your logic into a macrodef and simply call it twice. In any case you would need to write the two parameters at some point in your build file. I don't think there is any way to iterate through properties without using external .jars or BSF languages.

<project name="test" default="build">
<!--Needed for antcontrib-->
<macrodef name="build.war">
  <attribute name="dir"/>
  <attribute name="target"/>
  <sequential>
  <antcall target="@{target}">
        <param name="path" value="@{dir}"/>
  </antcall>
  </sequential>
</macrodef>

   <target name="build">
   <property name="test" value="value_1"/>
   <property name="production" value="value_2"/>

   <build.war dir="${test}" target="create.war"/>
   <build.war dir="${production}" target="create.war"/>


   </target>
   <target name="create.war">
      <echo message="My path is : ${path}"/>
   </target>
</project>
like image 97
FailedDev Avatar answered Oct 26 '22 23:10

FailedDev