如何在Ant中将多个参数传递给目标?

编程入门 行业动态 更新时间:2024-10-24 08:29:07
本文介绍了如何在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: ant- contrib.sourceforge/#install

You will have to install the .jar first by following the instructions here : ant-contrib.sourceforge/#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中将多个参数传递给目标?

本文发布于:2023-11-23 19:41:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1622652.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   中将   参数   目标   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!