2

I have a file build.properties:

a=test1
b=
c=test2

And a file build.xml :

<property file="build.properties" />    

<for list="${a},${b},${c}" param="params">
    <sequential>
        <echo message="@{params}"></echo>
        <if>
            <equals arg1="@{params}" arg2=""/>
            <then><echo message="empty"/></then>
            <else><echo message="ok"/></else>
        </if>
    </sequential>
</for>

I obtain:

[echo] test1
[echo] ok
[echo] test 2
[echo] ok

But I want:

[echo] test1
[echo] ok
[echo] empty
[echo] test 2
[echo] ok

What's happen?

Thanks for any help.

trainmaster
  • 145
  • 10

1 Answers1

2

The for task of Ant-Contrib uses the Java StringTokenizer class to tokenize the list of parameters. When tokenizing using this class, empty tokens are skipped, which is the case for property b in your case.

One solution is to add whitespace to the comma-separated list, and trim the token in the loop body:

<for list="${a}, ${b}, ${c}" param="params">
    <sequential>
        <propertyregex override="yes" property="trimmed.param" input="@{params}"
                regexp=" " replace="" global="true" defaultValue="@{params}" />
        <echo message="${trimmed.param}"/>
        <if>
            <equals arg1="${trimmed.param}" arg2=""/>   
            <then>
                <echo message="empty"/>
            </then>
            <else>
                <echo message="ok"/>
            </else>
        </if>
    </sequential>
</for>
M A
  • 71,713
  • 13
  • 134
  • 174