多次调用ant目标不会重置属性

时间:2016-04-18 19:25:25

标签: ant

我有一个运行设置属性的任务的目标。另一个目标检查该属性,如果它是真的,它再次调用第一个目标。但是一旦第一个目标运行并设置了属性,它就不会再次更改它! (而且我知道它应该改变,因为我可以看到条件args改变了)

(我无法安装contrib库 - 不是我的选择 - 所以我不得不做这项工作)

<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>

属性service.running只会更改一次,然后始终保持为真,即使它现在应该为假。

1 个答案:

答案 0 :(得分:1)

属性在ANT中是不可变的,这就是为什么它在第一次设置后不会改变的原因。 This stackoverflow thread提供了一些解决方法。我个人使用javascript来解决这个问题,如上面的链接所述:

<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>

用法:

<property name="x" value="10"/>
<propertyreset name="x" value="11"/>
<echo>${x}</echo>   <!-- will print 11 -->