ANT:将字符串拆分为多个参数 [英] ANT: split string into multiple parameters

查看:37
本文介绍了ANT:将字符串拆分为多个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚完成了一个 ANT 脚本(完美地完成了这项工作).但是有一个新的要求,脚本必须改变.目标是拥有名为 deploy-all.txt 的 ANT 部署文件.该文件看起来像

I have just finished an ANT script (does the job perfectly). But there is a new requirement and script has to change. The goal is to have ANT deployment file called deploy-all.txt. The file will look like

client1=name1=server1+server2=repositoryX
client2=name2=server1+server3=repositoryY
client3=name3=server2+server4=repositoryZ

将只有 1 个客户端,只有一个名称,从 1 到几台服务器,只有一个存储库和一种类型.

There will be only 1 client, only one name, from 1 to few servers, only one repository, and one type.

目标是什么:对于每一行,我需要获取变量,以便我可以解析它们并执行任务.输出应该是

What the goal is: for each line i need to get variables so i can parse them and perform tasks. The output should be

client=client1
name=name1
server=server1
server=server2 (could be more)
repository=repository1

必须为每一行解析这些参数.我确实有一个名为 GENERIC.WAR 的通用 APP - 已经完成并且运行良好.现在应用程序需要单独的插件来构建/部署.

Those parameters have to be parsed for each line. I do have generic APP that is called GENERIC.WAR - that is done and works well. Now applicaiton require separate plugins that needs to be build/deploy.

该脚本将做什么:

a) copy generic GENERIC.WAR file to server1 and server2 with name scecified as client1 (SCP using scp task)
b) get svn repository based on repositoryX (svn update ant task)
c) get name and loop through existing code (using xmltask from checked out repository) to find right code to compile and zip it (have done it already)

所以我有我可以执行的所有任务,但不能将每个元素解析为每行的单独参数.我尝试了 for listsequential 但到目前为止没有成功.我可以将所有参数分开,但只有 1 个参数.我无法将它作为多个参数来获取,我可以遍历每一行并解析为其他任务.

So I have all tasks i can perform but can not parse each element as a separate parameter for each line. I tried for list and sequential but no success so far. I could separate all but as only 1 parameter. I can not get it as multiple parameters that i can loop through each line and parse to additional tasks.

因为这只是一个开始 - 我可以改变设计 - 例如使用 XML 而不是 TXT.是否可行,如果可行,请您帮我想想怎么做.

Because it is just a start - i can change design - to have a XML for example instead of TXT. Is it doable and if yes could you please help me with idea how to.

推荐答案

如果您从以命名空间"的方式定义属性键开始,我认为您会发现更容易实现所需的内容,例如

If you start by defining your property keys in a "namespaced" fashion, I think you will find it easier to achieve what you need, e.g.

client.list=client1,client2
client1.name=client1
client1.server.list=server1,server2
client1.repository=repositoryX
client2.name=name2
client2.server.list=server1,server3
client2.repository=repositoryY

现在您拥有一组带有唯一键的属性,您可以在循环和/或宏定义中交叉引用这些属性.

Now you have a set of properties with unique keys which you can cross-reference in loops and/or macrodefs.

以下是您如何使用它的示例:

Here's an example of how you could use that:

<property file="deploy-all.txt"/>

<target name="test">
    <for list="${client.list}" param="client">
        <sequential>
            <deploy client="@{client}"/>
        </sequential>
    </for>
</target>

<macrodef name="deploy">
    <attribute name="client"/>
    <sequential>
        <echo>client: @{client}</echo>
        <for list="${@{client}.server.list}" param="server">
            <sequential>
                <echo>server: @{server}</echo>
            </sequential>
        </for>
        <echo>repository: ${@{client}.repository}</echo>
    </sequential>
</macrodef>

输出:

test:
     [echo] client: client1
     [echo] server: server1
     [echo] server: server2
     [echo] repository: repositoryX
     [echo] client: client2
     [echo] server: server1
     [echo] server: server3
     [echo] repository: repositoryY

这篇关于ANT:将字符串拆分为多个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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