2016-04-18 10 views
0

プロパティを設定するタスクを実行するターゲットがあります。別のターゲットがそのプロパティをチェックし、それが真であれば、最初のターゲットを再度呼び出します。しかし、最初のターゲットが実行され、プロパティが設定されると、それは決して再び変更されません! (私は、条件引数が変更さ見ることができるので、私はそれを変更する必要があります知っている)antターゲットを複数回呼び出すとプロパティがリセットされない

(私は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は一度だけ変更され、現在はfalseになっていても常にtrueのままです。

答えて

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

そして意志のcontribなし(1.9.6)のAntでこの作品? –

+2

@DonRhummyあなたのJVMにもっと依存します。 Javascript(Rhino)はJava6にバンドルされていますが、Java8にはNashornという新しいJavascriptエンジンがあります。 –

関連する問題