【问题标题】:Ant task to copy a file if it doesn't exist at the target location如果目标位置不存在文件,则 Ant 任务复制文件
【发布时间】:2014-12-01 12:29:45
【问题描述】:

只有当文件在目标位置不存在时,是否有任何 Ant 任务将文件复制到目标位置?

【问题讨论】:

    标签: ant build build-automation


    【解决方案1】:

    分三种情况:

    1. 目标文件不存在。
    2. 目标文件存在,但比源文件旧。
    3. 目标文件存在并且比源文件新。

    这个问题要求复制仅在案例 1 中发生。默认情况下复制在案例 1 和 2 中发生;将 overwrite 设置为 true 会使复制在所有三种情况下都发生。答案是使用present 选择器:

    <copy todir="dest">
      <fileset dir="src">
        <present present="srconly" targetdir="dest" />
      </fileset>
    </copy>
    

    【讨论】:

      【解决方案2】:

      copy task 有一个覆盖属性,默认设置为 false。

      overwrite = 覆盖现有文件,即使目标文件是 较新。必需=否;默认为 false。

      因此,当 overwrite="false"(默认设置)时,您可以执行复制任务,这意味着:

        <copy todir="../dest/dir">
          <fileset dir="src_dir">
            <exclude name="**/*.java"/>
          </fileset>
        </copy>
      

      或单个文件:

      <copy file="myfile.txt" todir="../some/other/dir"/>
      

      完全符合您的要求。

      【讨论】:

      • 只有在源文件不比目标文件新时才有效。如果它较新,则目标文件将被覆盖。
      • @Subby 你是对的,IMO 是一个错误。从复制任务的来源: if (forceOverwrite || !destFile.exists() || (file.lastModified() - 粒度 > destFile.lastModified())) { fileCopyMap.put(file.getAbsolutePath(), new String[ ] {destFile.getAbsolutePath()}); } else { log(file + " 省略,因为 " + destFile + " 是最新的。", Project.MSG_VERBOSE); => 原因很明显
      • 如果目标文件夹不存在怎么办?它会创建一个目标文件夹吗?
      • @vogash 是的,它的行为类似于带有 -P 参数的 shell mkdir 命令。
      • @Rebse 老实说,我不认为这是一个错误。它在文档中。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-04
      • 2011-01-03
      • 1970-01-01
      • 2012-04-12
      • 1970-01-01
      • 2018-02-18
      相关资源
      最近更新 更多