Amit
Amit

Reputation: 131

Retry task on exec task in Ant gives incorrect resultproperty

I have a piece of code in my ant build.xml

<retry retrycount="10" retrydelay="30000">
            <exec executable="${env.M2_HOME}/bin/mvn.cmd" output="@{log}" failonerror="true" resultproperty="res" timeout="@{timeoutmillis}" errorproperty="error">
                ...
            </exec>
</retry>
<echo message="${res}"/>

I retry my cmd task if if fails once upto 10 times. But even if it were to succeed after retrying a few turns, the value returned in res is 1 even though it is a build success. I expect it to be 0 as if it was SUCCESS.

Upvotes: 0

Views: 987

Answers (2)

Amit
Amit

Reputation: 131

This is not a direct answer to the question I asked. But taking into consideration what CAustin and jdpjamesp said, I'm reading the content of my output log to see whether the command failed or not.

The cmd will return 1 even if it passed after some n number of failures. But the output log in case of mvn will have "BUILD SUCCESS" only if the command passes at the end. So searching for this string works for me. The log should not be appended after every time though.

So I implemented this-

<property name="pass.string" value="BUILD SUCCESS" />
<resourcecount property="pass.string.count">
    <fileset id="matches" file="@{log}">
    <contains text="${pass.string}" />
    </fileset>
</resourcecount>
<if>
    <equals arg1="${pass.string.count}" arg2="0" />
    <then>
         ..
    </then>
    <else>
        ..
    </else>
</if>

Upvotes: 0

CAustin
CAustin

Reputation: 4614

Properties in Ant are immutable (they're not variables), so even if your exec task eventually succeeds, the result property will have already been set to whatever it got from the first run.

You can get past this by creating a sequential block and using the local task inside it to control your property scope.

    <retry retrycount="10">
        <sequential>
            <local name="do.fail" />

            <input addproperty="do.fail" validargs="y,n" message="fail?" />

            <fail>
                <condition>
                    <equals arg1="${do.fail}" arg2="y" />
                </condition>
            </fail>
        </sequential>
    </retry>

Running the above example will prompt the user until "n" is provided as input (or 10 retries).

Upvotes: 0

Related Questions