Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check the existence file in ant [duplicate]

Tags:

ant

Possible Duplicate:
Ant task to check if a file exists?

How can one check if a file exist in a directory, if not use another file in different location. I tried the follow but does not help me with what I want. Could someone help?

 <condition property="somefile.available">
     <or>
         <available file="/home/doc/somefile"/>
         <and>
             <available file="/usr/local/somefile" />
         </and>
     </or>
 </condition>
like image 936
user87636 Avatar asked Jul 14 '11 09:07

user87636


People also ask

How does Apache Ant check for a signature?

If the name of the signature is passed, the file is checked for presence of that particular signature; otherwise the file is checked for the existence of any signature. It does not perform rigorous signature validation; it only looks for the presence of a signature. Since Apache Ant 1.7.

How do I test if a file does not exist?

Nonexistence of one file results in false, although if neither exists they are considered equal in terms of content. This test does a byte for byte comparison, so test time scales with byte size. Note: if the files are different sizes, one of them is missing or the filenames match the answer is so obvious the detailed test is omitted.

How do I check if a file exists in Ansible?

This tutorial covers how to use the stat module in Ansible to check if files and folders exist on remote hosts. The easiest way to check if a file exists using Ansible is with the stat module. The purpose of the stat module is to retrieve facts about files and folders and record them in a register.

Where can I find the ant JAR file?

The full location of the Ant jar file. The home directory of Ant installation. The home directory for Ant library files - typically ANT_HOME/lib folder. The example presented in this chapter uses the ant.version built-in property. Er. Himanshu Vasishta


2 Answers

Use condition tests to establish if files are present. Then have a target for each true condition

<target name="go" depends="file-checks, do-something-with-first-file, do-something-with-second-file"/>

<target name="file-checks">
   <available file="/home/doc/somefile"  property="first.file.found"/>
   <available file="/usr/local/somefile" property="second.file.found"/>
</target>

<target name="do-something-with-first-file" if="first.file.found">
   ???
</target>

<target name="do-something-with-second-file" if="second.file.found">
   ???
</target>
like image 91
Mark O'Connor Avatar answered Oct 16 '22 12:10

Mark O'Connor


If you don't mind using ant-contrib you can take the procedural approach like this:

  <if>
    <available file="file1"/>
    <then>
      <property name="file.exists" value="true"/>
    </then>
    <else>
      <if>
        <available file="file2"/>
        <then>
          <copy file="file2" tofile="file1"/>
          <property name="file.exists" value="true"/>
        </then>
      </if>
    </else>
  </if>

Otherwise you can probably use a target that is conditional on a property being set to indicate whether the file already exists in the preferred location, like Ant task to run an Ant target only if a file exists? but using unless instead of if.

like image 37
Ben Avatar answered Oct 16 '22 13:10

Ben