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

查看:33
本文介绍了如何将多个参数传递给 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" 它将创建一个文件夹 production并将war文件放入其中.

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 的第二个解决方案.您可以将所有逻辑封装到一个宏定义中,然后简单地调用它两次.在任何情况下,您都需要在构建文件中的某个位置写入这两个参数.我认为没有任何方法可以在不使用外部 .jar 或 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天全站免登陆