ANT: split string into multiple parameters

2019-08-12 09:58发布

问题:

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

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

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.

What that script will do:

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)

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.

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.

回答1:

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>

Output:

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