检查ant中的存在文件

use*_*636 14 ant

可能重复:
Ant任务检查文件是否存在?

如何在目录中存在文件,如果不在另一个位置使用另一个文件,如何检查文件是否存在.我尝试了以下但不能帮助我想要的东西.有人可以帮忙吗?

 <condition property="somefile.available">
     <or>
         <available file="/home/doc/somefile"/>
         <and>
             <available file="/usr/local/somefile" />
         </and>
     </or>
 </condition>
Run Code Online (Sandbox Code Playgroud)

Mar*_*nor 21

使用条件测试来确定文件是否存在.然后为每个真实条件设定目标

<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>
Run Code Online (Sandbox Code Playgroud)


Ben*_*Ben 12

如果你不介意使用ant-contrib,你可以采用这样的程序方法:

  <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>
Run Code Online (Sandbox Code Playgroud)

否则,您可以使用以设置属性为条件的目标来指示文件是否已存在于首选位置,例如仅当文件存在时才运行Ant目标的Ant任务?但用unless而不是if.