Ant script - string splitting and access using indexing

I need to write an Ant script that will load a properties file to read one property from it. The value (multiline) looks something like this:

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

      

I need to iterate over each line and execute a shell command that looks like this:

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

      

I can 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 a fairly simple task in my opinion. I tried searching but without any success.

I would appreciate it if someone could help me with this or point me to an appropriate document.

Many thanks,

Pratik

+3


source to share


1 answer


A couple of problems with your assumptions:

  • The format of your input file is not a standard Java properties file, so it cannot be loaded using the standard loadproperties task in ANT.
  • ANT is not a programming language. The "for" task you specified is not part of the ANT core (requires 3rd party ant -contrib.jar)

So I would suggest using an inline 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>

      

+2


source







All Articles