如何在Ant中将多个参数传递给目标? [英] How to pass multiple parameters to a target in Ant?

查看:170
本文介绍了如何在Ant中将多个参数传递给目标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个虚拟目标:

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

我想做的是提供两个参数,分别是"abc"和""xyz",它将分别用abc和xyz参数的值替换 release 一词.

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.

对于第一个参数abc ="test",上面的代码将创建一个 test 目录,并将其中的内容放入其中.对于xyz ="production",它将创建一个文件夹生产并将战争文件放入其中.

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.

我通过使用

<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.

推荐答案

不幸的是,除非您引用文件,否则ant不支持for或foreach循环之类的迭代.但是,有ant contrib任务可以解决大多数(如果不是全部)迭代问题.

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.

您必须先按照此处的说明安装.jar: http://ant- contrib.sourceforge.net/#install

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

这大约需要10秒钟.之后,您可以简单地使用foreach任务遍历您的自定义列表.例如,您可以遵循以下build.xml文件:

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>

输出:

build:

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

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

BUILD SUCCESSFUL
Total time: 0 seconds

我希望它可以帮助:)

第二种解决方案,不使用ant contrib.您可以将所有逻辑封装到macrodef中,然后只需调用两次即可.无论如何,您都需要在构建文件中的某个时刻编写两个参数.我认为不使用外部.jars或BSF语言就无法通过属性进行迭代.

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>

这篇关于如何在Ant中将多个参数传递给目标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆