Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

execute Ant task if TWO conditions are met

The above ant script implements if dir_is_empty then git-clone else git-fetch using Ant-1.7.1 core statements:

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

(see my other post)


But how to implement a target enabled by two conditions?

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

my current attempt:

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

[...]

I would prefer Ant-1.7.1 core statements. But I am open about other possibilities as Ant contrib, or embedded script... Feel free to post your ideas...

(See also question Execute ANT task just if a condition is met)

like image 394
oHo Avatar asked Aug 07 '13 07:08

oHo


People also ask

How do you run an ant task?

To run the ant build file, open up command prompt and navigate to the folder, where the build. xml resides, and then type ant info. You could also type ant instead. Both will work,because info is the default target in the build file.

What is Antcall in build XML?

Description. Call another target within the same buildfile optionally specifying some properties (params in this context). This task must not be used outside of a target . By default, all of the properties of the current project will be available in the new project.


1 Answers

Even when bound to Ant 1.7.1 you may combine your 3 chk targets into one, see the condition part in the snippet. Since Ant 1.9.1 (better use Ant 1.9.3 because of bugs in Ant 1.9.1 see this answer for details) it is possible to add if and unless attributes on all tasks and nested elements, so no extra target needed, f.e. :

<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>
like image 51
Rebse Avatar answered Sep 16 '22 21:09

Rebse