Search code examples
anttask

How to retrieve first line from a string property in Ant?


I have a property which generates with lines of string. I'm trying to retrieve/store the first line only instead of all the lines.

I was trying to do something like this but it prints all the lines:

<for param="line" list="${targetDeviceUDID}" delimiter="${line.separator}">
    <sequential >
    <echo>@{line}</echo>
    </sequential>
</for>

Solution

  • You can use Ant's filterchain ability with the headfilter to accomplish this.

    <project name="build" default="echo-first-line">
        <target name="echo-first-line">
            <property name="input" value="abc${line.separator}def${line.separator}123${line.separator}456" />
    
            <echo message="Input:${line.separator}${input}" />
    
            <echo message="${line.separator}" />
    
            <loadresource property="output">
                <propertyresource name="input" />
                <filterchain>
                    <headfilter lines="1" />
                </filterchain>
            </loadresource>
     
            <echo message="Output:${line.separator}${output}" />
       </target>
    </project>