Balaji
Balaji

Reputation: 1895

Index of list item from ant property

I have a ant parameter with list of values as below.

releases=release1,release2

I use the below for loop to process the values.

<for list="${releases}" param="release">
  <sequential>
    <echo>Processing @{release}</echo>
  </sequential>
<for>

I want to do something like, I want to echo the below statement only when the @{release}=release2

<echo>Processing last release</echo>

I am not quite sure how to get the index of the list items or if it is even possible.

Thanks in advance, Balaji

Upvotes: 0

Views: 2201

Answers (1)

David W.
David W.

Reputation: 107040

That's all? And, you're using Ant-Contrib tasks already. Take a look at the PropertyRegEx task. That's the Ant-Contrib task that will do exactly what you want:

<propertyregex property="last.release"
     input="${releases}"
     regexp=".*,(.*)"
     select="\1"/>

This will set ${last.release} to the last release in your string.

Be careful of strings that are null or don't match your regular expression! You can use the Ant-Contrib if task to test for that:

<propertyregex property="last.release"
     input="${releases}"
     regexp=".*,(.*)"
     select="\1"/>
<if>
    <isset property="last.release"/>
    <then>
        <echo>The last release is ${last.release}</echo>
    </then>
    <else>
        <fail>Oops! Something didn't work...</fail>
    </else>
</if>

Upvotes: 2

Related Questions