StevenHB
StevenHB

Reputation: 185

Trouble using ant length condition on a string

I'm trying to deal with a string that has a long error message instead of a short value when something goes wrong. My ant script contains the following code:

<!-- build.svnversion is invalid if it's longer than 15 characters --> 
<length property="build.svnversion.length" string="${build.svnversion}" />
<echo message="build.svnversion.length: ${build.svnversion.length}"/>

<condition property="build.svnversion" value="N/A" else="${build.svnversion}">
    <length string="${build.svnversion}" when="gt" length="15" />
</condition>
<echo message="build.svnversion: ${build.svnversion}" />

This produces the following output:

[echo] build.svnversion.length: 148
[echo] build.svnversion: svn: The path '.' appears to be part of a Subversion 1.7 or greater
[echo] working copy.  Please upgrade your Subversion client to use this
[echo] working copy.

Why isn't build.svnversion getting set to N/A since its length is clearly greater than 15?

Upvotes: 1

Views: 1786

Answers (1)

David W.
David W.

Reputation: 107040

Are you attempting to reset the property build.svnversion?

Once a property is set, it cannot be unset or changed1.

In your <condition/> task, you'll have to use another property name.

<!-- build.svnversion is invalid if it's longer than 15 characters --> 
<length property="build.svnversion.length"
    string="${build.svnversion}" />

<echo message="build.svnversion.length: ${build.svnversion.length}"/>

<condition property="real.build.svnversion"
    value="N/A"
    else="${build.svnversion}">
    <length string="${real.build.svnversion}"
        when="gt" length="15" />
</condition>
<echo message="real.build.svnversion: ${real.build.svnversion}" />

1.. You can use Ant-Contrib's <var/> task to change a property's value or to completely unset it, but that's considered bad form.

Upvotes: 3

Related Questions