【问题标题】:Ant task to run an Ant target only if a file exists?仅当文件存在时才运行 Ant 目标的 Ant 任务?
【发布时间】:2009-02-06 14:49:43
【问题描述】:

是否存在仅当给定文件存在时才会执行块的 ANT 任务?我的问题是我有一个通用的 ant 脚本,它应该进行一些特殊处理,但前提是存在特定的配置文件。

【问题讨论】:

标签: file ant build-automation


【解决方案1】:

AvailableCondition

<target name="check-abc">
    <available file="abc.txt" property="abc.present"/>
</target>

<target name="do-if-abc" depends="check-abc" if="abc.present">
    ...
</target> 

【讨论】:

  • Available 是一个不那么明显的名称。谷歌显示人们编写自己的标签这一事实让我更加困惑
  • 它可能有效,但如果您需要应用于大量文件,这似乎不是一个好的解决方案
  • 如果有人想知道,ifunless 属性仅启用或禁用它们所附加的目标,即始终执行目标的依赖项。否则,依赖于设置您正在检查的属性的目标是行不通的。
  • 看起来&lt;Available&gt; 已被弃用。我用过这个:&lt;target name="do-if-abc" if="${file::exists('abc.txt')}"&gt; ... &lt;/target&gt; 检查:nant.sourceforge.net/release/0.85/help/functions/…
  • @Loïc 1: 在哪里声明 &lt;available&gt; 已弃用? 2: ${file::existst...} 似乎无法与 Ant (Apache ANT 1.9.7) 一起使用
【解决方案2】:

从编码的角度来看,这可能更有意义(可通过 ant-contrib 获得:http://ant-contrib.sourceforge.net/):

<target name="someTarget">
    <if>
        <available file="abc.txt"/>
        <then>
            ...
        </then>
        <else>
            ...
        </else>
    </if>
</target>

【讨论】:

  • 我认为这只在 ant-contrib 中可用。
【解决方案3】:

自 Ant 1.8.0 以来,显然也存在资源

http://ant.apache.org/manual/Tasks/conditions.html

测试资源是否存在。自从 蚂蚁 1.8.0

要测试的实际资源是 指定为嵌套元素。

一个例子:

<resourceexists>
  <file file="${file}"/>
</resourceexists>

我打算从上面对这个问题的好答案中重新编写示例,然后我发现了这个

从 Ant 1.8.0 开始,您可以改为使用 财产扩张;真值 (或 on 或 yes)将启用该项目, 而假(或关闭或否)将 禁用它。其他值还在 假定为属性名称等 仅当命名的项目才启用 属性已定义。

与旧款相比,这款 为您提供额外的灵活性, 因为你可以覆盖条件 从命令行或父级 脚本:

<target name="-check-use-file" unless="file.exists">
    <available property="file.exists" file="some-file"/>
</target>
<target name="use-file" depends="-check-use-file" if="${file.exists}">
    <!-- do something requiring that file... -->
</target>
<target name="lots-of-stuff" depends="use-file,other-unconditional-stuff"/>

来自http://ant.apache.org/manual/properties.html#if+unless的蚂蚁手册

希望这个例子对某些人有用。他们没有使用resourceexists,但大概你可以?.....

【讨论】:

  • 请注意,if="${file.exists}" 应替换为 if="file.exists",如 ifunless 仅按名称检查属性的存在,而不是其值。
  • @Vadzim 正如答案所述,这只适用于 Ant 1.8,并且 1.8 确实支持属性扩展。 ant.apache.org/manual/properties.html#if+unless
【解决方案4】:

我认为值得参考这个类似的答案:https://stackoverflow.com/a/5288804/64313

这是另一个快速解决方案。使用&lt;available&gt; 标签可能还有其他变化:

# exit with failure if no files are found
<property name="file" value="${some.path}/some.txt" />
<fail message="FILE NOT FOUND: ${file}">
    <condition><not>
        <available file="${file}" />
    </not></condition>
</fail>

【讨论】:

    【解决方案5】:

    检查使用文件名过滤器,如DB_*/**/*.sql

    如果存在与通配符过滤器相​​对应的一个或多个文件,这是一种执行操作的变体。也就是说,您不知道文件的确切名称。

    在这里,我们递归地在名为“DB_*”的任何子目录中查找“*.sql”文件。您可以根据需要调整过滤器。

    注意:Apache Ant 1.7 及更高版本!

    如果存在匹配的文件,这里是设置属性的目标:

    <target name="check_for_sql_files">
        <condition property="sql_to_deploy">
            <resourcecount when="greater" count="0">
                <fileset dir="." includes="DB_*/**/*.sql"/>
            </resourcecount>
        </condition>
    </target>
    

    这是一个“条件”目标,仅在文件存在时运行:

    <target name="do_stuff" depends="check_for_sql_files" if="sql_to_deploy">
        <!-- Do stuff here -->
    </target>
    

    【讨论】:

      【解决方案6】:

      您可以通过命令对名称与您需要的名称相同的文件列表执行操作来完成此操作。这比创建一个特殊的目标要容易和直接得多。而且你不需要任何额外的工具,只需要纯 Ant。

      <delete>
          <fileset includes="name or names of file or files you need to delete"/>
      </delete>
      

      请参阅:FileSet

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-08
        • 1970-01-01
        • 1970-01-01
        • 2013-01-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多