难道我有办法检查蚂蚁目录(不是文件)的存在?(Do I have a way to check th

2019-06-26 12:56发布

如何检查使用Ant文件夹是否存在?

我们可以检查文件是否存在,但我们可以做同样的文件夹呢?

Answer 1:

您可以使用可用的任务类型设置为“目录”。

例如:

<available file="${dir}" type="dir"/>

做条件处理的标准方法是用条件的任务 。 在下面的例子中,如果目录存在,而除非目录存在运行doBar会响应消息运行doFoo会响应的消息。

dir.check目标由两个doFoo和doBar需要,它设置dir.exists属性取决于可用任务的结果真的还是假的。 如果属性格式设置为true的doFoo目标将只运行,如果没有设置或设置为false,它doBar将只运行。

<?xml version="1.0"?>
<project name="test" default="doFoo" basedir=".">
  <property name="directory" value="c:\test\directory"/>

  <target name="doFoo" depends="dir.check" if="dir.exists">
    <echo>${directory} exists</echo>
  </target>

  <target name="doBar" depends="dir.check" unless="dir.exists">
    <echo>${directory} missing"</echo>
  </target>

  <target name="dir.check">
    <condition property="dir.exists">
      <available file="${directory}" type="dir"/>
    </condition>
  </target>
</project>

羚羊提供了额外的任务,包括一个如果任务,可以使处理更简单(和我更直观),您可以从下载羚羊任务下载页面 。



Answer 2:

这里是结合一个小例子available元素成if测试。

<!-- Test if a directory called "my_directory" is present -->
<if>
  <available file="my_directory" type="dir" />
  <then>
    <echo message="Directory exists" />
  </then>
  <else>
    <echo message="Directory does not exist" />
  </else>
</if>

警告 :你需要在你的ANT_HOME \ lib目录蚂蚁contrib.jar否则你不会有访问if元素,你的脚本将失败,此错误:

Problem: failed to create task or type if
Cause: The name is undefined.
Action: Check the spelling.
Action: Check that any custom tasks/types have been declared.
Action: Check that any <presetdef>/<macrodef> declarations have taken place. 


Answer 3:

这里是我的解决方案,它不需要设置属性,并使用目标与“如果”或“除非”:

宏:

<macrodef name="assertDirAvailable">
    <attribute name="dir" />
    <sequential>
        <fail message="The directory '@{dir}' was expected to be available but is not">
            <condition>
                <not>
                    <available file="@{dir}" type="dir" />
                </not>
            </condition>
        </fail>
    </sequential>
</macrodef>

用法:

<assertDirAvailable dir="${dirToCheck}" />


Answer 4:

使用ANT 1.8版本我的解决方案,旧版本可能无法正常工作,由于如果/除非不支持$ {} evalTrueOrFalse语法。

<?xml version="1.0" encoding="UTF-8"?>
<project name="DoMagic" default="build" basedir=".">

<property environment="env" />
<property name="name" value="Do the ANT Magic" />
<property name="somedir" value="./must_exist_folder"/>
<tstamp><format property="TODAY" pattern="yyyy-MM-dd HH:mm:ss" /></tstamp>

<target name="doMagic" if="${dir.exists}">
  <echo message="Do the magic stuff" />
</target>

<target name="doUsage" unless="${dir.exists}">
  <echo message="Do usage and help" />
</target>

<target name="build">
  <echo message="Do the magic" />

  <condition property="dir.exists" else="false"><available file="${somedir}" type="dir" /></condition>
  <echo message="Folder found: ${dir.exists}" />
  <antcall target="doCustomize"></antcall>
  <antcall target="doUsage"></antcall>
</target>

</project>
  • ANT 1.6或早期ANT 1.7不起作用,升级到1.8 ANT释放。
  • 目标属性,如果 除非评估$ {VAR}语法真/假
  • 条件其他属性值设置为属性(如果可用)的条件是假的,没有它变量没有设置。 NotSet值不一样明确的虚假值。
  • 调用任何目标,但其是否实际运行,如果/除非属性定义

http://ant.apache.org/manual/properties.html#if+unless
[如果/除非在蚂蚁1.7.1及更早版本,这些属性只能是属性名称。 蚂蚁1.8.0,你可以改为使用属性扩展。 相比老款的风格,这给你更多的灵活性。



Answer 5:

这是另一种方法,可以调用只有一个任务,而无需使用ANT-contrib.jar。

<target name="my-task" depends="dir-check">
    <antcall target="my-task-install"/>
    <antcall target="my-task-update"/>
</target>
<target name="my-task-install" unless="dir.exists" >
    {some task}
</target>
<target name="my-task-update" if="dir.exists" >
    {another task}
</target>
<target name="dir-check">
    <condition property="dir.exists">
        <available file="my-dir" type="dir" />
    </condition>
</target>


文章来源: Do I have a way to check the existence of a directory in Ant (not a file)?