Pratik Pandey
Pratik Pandey

Reputation: 31

Ant script -- split string and access via indexing

I need to write an Ant script which would load a properties file, read a single property out of it. The value (multiline) is something like:

path/to/file1a;path/to/file1b,
path/to/file2a;path/to/file2b,
..
..

I need to iterate over every line, and execute a shell command which looks like:

myCommand -param1 path/to/file1a -param2 path/to/file1b  #Command inside a single iteration

I have able to figure out how to loop:

<for list="${ValueFromPropertyFile}" param="a">  
    <sequential>
        <exec executable="myCommand">
            <arg value="-param1" />
            <arg value="----  split(@{a}, ";")[0]  ----" />
            <arg value="-param2" />
            <arg value="----  split(@{a}, ";")[1]  ----" />
        </exec>
    </sequential>
</for>

This is quite a simple task in my opinion. I tried searching, but without any success.

I would appreciate if someone could help me out with this, or point me to a relevant document.

Many thanks,

Pratik

Upvotes: 3

Views: 3894

Answers (1)

Mark O&#39;Connor
Mark O&#39;Connor

Reputation: 77951

Couple of problems with your assumptions:

  1. The format of your input file is not a standard Java properties file, so it can't be loaded using the standard loadproperties task in ANT.
  2. ANT is not a programming language. The "for" task you've quoted is not part of core ANT (Requires the 3rd party ant-contrib.jar)

So I'd suggest using an embedded script to solve your problem.

Example

The project is self documenting:

$ ant -p
Buildfile: /home/mark/tmp/build.xml

    This is a demo project answering the following stackoverflow question:
    http://stackoverflow.com/questions/14625896

    First install 3rd party dependencies and generate the test files

        ant bootstrap generate-test-files

    Then run the build

        ant

    Expect the following output

        parse-data-file:
            [exec] build/myCommand -param1 path/to/file1a -param2 path/to/file1b
            [exec] build/myCommand -param1 path/to/file2a -param2 path/to/file2b

build.xml

<project name="demo" default="parse-data-file">

    <description>
    This is a demo project answering the following stackoverflow question:
    http://stackoverflow.com/questions/14625896

    First install 3rd party dependencies and generate the test files

        ant bootstrap generate-test-files

    Then run the build

        ant

    Expect the following output

        parse-data-file:
            [exec] build/myCommand -param1 path/to/file1a -param2 path/to/file1b
            [exec] build/myCommand -param1 path/to/file2a -param2 path/to/file2b

    </description>

    <target name="bootstrap" description="Install 3rd party dependencies">
        <mkdir dir="${user.home}/.ant/lib"/>
        <get dest="${user.home}/.ant/lib/groovy-all.jar" src="http://search.maven.org/remotecontent?filepath=org/codehaus/groovy/groovy-all/2.1.0/groovy-all-2.1.0.jar"/>
    </target>

    <target name="generate-test-files" description="Generate the input data and sample script">
        <echo file="build/input.txt">path/to/file1a;path/to/file1b,
path/to/file2a;path/to/file2b,</echo>

        <echo file="build/myCommand"> #!/bin/bash
echo $0 $*</echo>

        <chmod file="build/myCommand" perm="755"/>
    </target>

    <target name="parse-data-file" description="Parse data file">
        <taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy"/>

        <groovy>
            new File("build/input.txt").eachLine { line ->
                def fields = line.split(/[;,]/)

                ant.exec(executable:"build/myCommand") {
                    arg(value:"-param1")
                    arg(value:fields[0])
                    arg(value:"-param2")
                    arg(value:fields[1])
                }
            }
        </groovy>
    </target>

    <target name="clean" description="Cleanup build files">
        <delete dir="build"/>
    </target>

</project>

Upvotes: 2

Related Questions