开发者

How to pass multiple parameters to a target in 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 targ开发者_如何学JAVAet="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.


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>


I admit that I don't understand the question in detail. Is ${project.stage} the same as the xyz and abc parameters? And why are there two parameters xyz and abc mentioned, when only the word "release" should be replaced?

What I know is, that macrodef (docu) is something very versatile and that it might be of good use here:

<project name="Foo" default="create.wars">

    <macrodef name="createwar">
        <attribute name="stage" />
        <sequential>
            <echo message="mkdir dir=@{stage}/release " />
            <echo message="war destfile=@{stage}/release/sigma.war" />
        </sequential>
    </macrodef>

    <target name="create.wars">
        <createwar stage="test" />
        <createwar stage="production" />
    </target>
</project>

The output will be:

create.wars:
     [echo] mkdir dir=test/release 
     [echo] war destfile=test/release/sigma.war
     [echo] mkdir dir=production/release 
     [echo] war destfile=production/release/sigma.war

Perhaps we can start from here and adapt this example as required.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜