0

I have a target that runs a task which sets a property. Another target checks that property and if it's true, it calls the first target again. But once the first target is run and sets the property, it never changes it again! (And I know it should be changing because I can see the condition args change)

(I cannot install contrib library - not my choice - so I'm stuck doing this work around)

<target name="check-service-state">

    <!-- See if the service is running or not -->
    <exec executable="ssh" outputproperty="service.state" failonerror="false">
        <arg value="-t" />
        <arg value="-t" />
        <arg value="${username}@${ssh.host}" />
        <arg value="sudo initctl list | grep ${service.name}" />
    </exec>

    <condition property="service.running" else="false">
        <or>
            <contains string="${service.state}" substring="start/running" />
        </or>
    </condition>
    <echo message="${service.running}" />             
</target>

<target name="restart-service" depends="stop-service">

    <!-- Check if service stopped -->
    <antcall target="check-service-state" />
    <sleep seconds="1" />

    <!-- now try to start again, or wait and recheck -->
    <antcall target="service-not-stopped" />
    <antcall target="service-stopped" />

</target>

<target name="wait-for-service">

    <!-- Check if service stopped -->
    <antcall target="check-service-state" />

    <!-- now try to start again -->
    <antcall target="service-not-stopped" />
    <antcall target="service-stopped" />

</target>

<!-- Acts as a loop/wait check for service stopping -->
<target name="service-not-stopped" if="${service.running}">
    <echo message="${service.state}" />
    <antcall target="wait-for-service" />
</target>

 <!-- Acts as a break from the loop/check for service stopping -->
<target name="service-stopped" unless="${service.running}">
    <antcall target="start-service" />
</target>

The property service.running only changes once and then always stays true even though it should now be false.

Don Rhummy
  • 24,730
  • 42
  • 175
  • 330

1 Answers1

1

Properties are immutable in ANT, which is why it's not changing once it's set the first time. This stackoverflow thread gives a few workarounds. I personally have used javascript to address this as described in the above link:

<scriptdef name="propertyreset" language="javascript" description="Allows to assign @{property} new value"> <attribute name="name"/> <attribute name="value"/> project.setProperty(attributes.get("name"), attributes.get("value")); </scriptdef>

Usage:

<property name="x" value="10"/>
<propertyreset name="x" value="11"/>
<echo>${x}</echo>   <!-- will print 11 -->
Community
  • 1
  • 1
TheEllis
  • 1,666
  • 11
  • 18
  • 2
    @DonRhummy Depends more on your JVM. Javascript (Rhino) has been bundled with Java6, Java8 has a newer Javascript engine called Nashorn. – Mark O'Connor Apr 18 '16 at 20:10