2013-08-07 18 views
7

Powyższy skryptu implementuje if dir_is_empty then git-clone else git-fetch użyciu ANT-1.7.1 podstawowych sprawozdań:wykonać zadanie Ant, jeśli spełnione są dwa warunki

<target name="update" depends="git.clone, git.fetch" /> 

<target name="check.dir"> 
    <fileset dir="${dir}" id="fileset"/> 
    <pathconvert refid="fileset" property="dir.contains-files" setonempty="false"/> 
</target> 

<target name="git.clone" depends="check.dir" unless="dir.contains-files"> 
    <exec executable="git"> 
    <arg value="clone"/> 
    <arg value="${repo}"/> 
    <arg value="${dir}"/> 
    </exec> 
</target> 

<target name="git.fetch" depends="check.dir" if="dir.contains-files" > 
    <exec executable="git" dir="${dir}"> 
    <arg value="fetch"/> 
    </exec> 
</target> 

(patrz my other post)


Ale jak zaimplementować target włączone przez dwa warunki?

if dir_does_not_exist or dir_is_empty then git-clone else git-fetch

moja obecna próba:

<target name="git.clone" 
     depends="chk.exist, chk.empty" 
     unless="!dir.exist || dir.noempty" > 
    [...] 
</target> 

<target name="chk.exist"> 
    <condition property="dir.exist"> 
    <available file="${dir}/.git" type="dir"/> 
    </condition> 
</target> 

[...] 

wolałbym Ant-1.7.1 podstawowych oświadczenia. Ale jestem otwarty na temat innych możliwości jak Ant contrib lub embedded script ... Zapraszam do zamieścić swoje pomysły ...

(Patrz również pytanie Execute ANT task just if a condition is met)

Odpowiedz

7

Nawet po związaniu z Ant 1.7.1 możesz połączyć 3 cele CHK do jednego, zobacz condition udział w fragmencie. Od wersji Antela 1.9.1 (lepiej używać Anta 1.9.3 z powodu błędów w Ant 1,9.1 see this answer for details) możliwe jest dodanie if and unless attributes dla wszystkich zadań i elementów zagnieżdżonych, więc nie jest potrzebny dodatkowy cel, np. :

<project xmlns:if="ant:if" xmlns:unless="ant:unless"> 

    <condition property="cloned" else="false"> 
    <and> 
     <available file="${dir}/.git" type="dir" /> 
     <resourcecount when="gt" count="0"> 
     <fileset dir="${dir}/.git" /> 
     </resourcecount> 
    </and> 
    </condition> 

    <exec executable="git" unless:true="${cloned}"> 
    <arg value="clone" /> 
    <arg value="${repo}" /> 
    <arg value="${dir}" /> 
    </exec> 

    <exec executable="git" dir="${dir}" if:true="${cloned}"> 
    <arg value="fetch" /> 
    </exec> 

</project> 
2

Z dokumentacji na targets:

W klauzuli if/unless można określić tylko jedną właściwość. Jeśli chcesz sprawdzić wiele warunków, można użyć cel dependend obliczania wynik dla sprawdzenia:

<target name="myTarget" depends="myTarget.check" if="myTarget.run"> 
    <echo>Files foo.txt and bar.txt are present.</echo> 
</target> 

<target name="myTarget.check"> 
    <condition property="myTarget.run"> 
     <and> 
      <available file="foo.txt"/> 
      <available file="bar.txt"/> 
     </and> 
    </condition> 
</target> 

Ponadto istniały pewne dyskusje na temat [email protected] i użytkownik @ mrówka .apache.org mailing-list:


Na przykład, następujący target łączy w sobie dwie właściwości (dir.exist i dir.noempty), aby utworzyć kolejną (cloned) za pomocą operatorów <and> i <istrue> (wiele innych operators are documented jak <or>, <xor>, <not>, <isfalse>, <equals>, <length>).

<target name="chk" depends="chk.exist, chk.empty" > 
    <condition property="cloned"> 
    <and> 
     <istrue value="dir.exist" /> 
     <istrue value="dir.noempty" /> 
    </and> 
    </condition> 
</target> 

Powyższy property"cloned" jest używany przez cele git.clone i git.fetch następująco:

<target name="update" depends="git.clone, git.fetch" /> 

<target name="git.clone" depends="chk" unless="cloned" > 
    <exec executable="git" > 
    <arg value="clone" /> 
    <arg value="${repo}" /> 
    <arg value="${dir}" /> 
    </exec> 
</target> 

<target name="git.fetch" depends="chk" if="cloned" > 
    <exec executable="git" dir="${dir}"> 
    <arg value="fetch"/> 
    </exec> 
</target> 

<target name="chk.exist" > 
    <condition property="dir.exist" > 
    <available file="${dir}" type="dir" /> 
    </condition> 
</target> 

<target name="chk.empty" > 
    <fileset dir="${dir}" id="fileset" /> 
    <pathconvert refid="fileset" property="dir.noempty" setonempty="false" /> 
</target> 
Powiązane problemy