Java Ant 复制文件而不覆盖

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3502964/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 01:21:50  来源:igfitidea点击:

Ant copy files without overwriting

javaantbuild

提问by user204069

Is there any command in ant to copy files from one folder structure to another without checking the last modified date/timeoverwriting files. Basically I want all extra files from folder A to folder B. That is: no files of folder B are replaced but extra files of folder A comes to folder B.

ant 中是否有任何命令可以将文件从一个文件夹结构复制到另一个文件夹结构而不检查上次修改的日期/时间覆盖文件。基本上我想要从文件夹 A 到文件夹 B 的所有额外文件。也就是说:没有文件夹 B 的文件被替换,但文件夹 A 的额外文件来到文件夹 B

I saw "ant copy" and "ant move" commands, but didn't help. Any suggestions.

我看到了“ant copy”和“ant move”命令,但没有帮助。有什么建议。

回答by Sean Patrick Floyd

If there isn't an elegant solution you can always do it in a sequence:

如果没有优雅的解决方案,您始终可以按顺序进行:

  1. <move>folder <B>to <C>
  2. <copy>files of folder <A>to <B>
  3. <copy>files of folder <C>to <B>using overwrite=true
  1. <move>文件夹<B><C>
  2. <copy>文件夹的文件<A><B>
  3. <copy><C><B>使用的文件夹的文件overwrite=true

(Do things the other way around, replacing the new files, not the old ones)

(反过来做,替换新文件,而不是旧文件)

回答by Joeri Hendrickx

Just use the <copy>tag with a fileset as **/*and set the overwrite attribute to false explicitly. I use this all the time:

只需将<copy>标记与文件集一起使用**/*,并将覆盖属性显式设置为 false。我经常用这个:

<copy todir="/your/target" overwrite="false">
  <fileset dir="/your/source" />
</copy>

回答by Mark O'Connor

The simple solution to your problem is to use the overwrite parameter to the copy command. This will ensure that files are not replaced

解决您的问题的简单方法是使用 copy 命令的 overwrite 参数。这将确保文件不会被替换

    <copy todir="B" verbose="true" overwrite="false">
        <fileset dir="A"/>
    </copy>

The more complicated example is to use a fileset selector

更复杂的例子是使用文件集选择器

    <copy todir="B" verbose="true">
        <fileset dir="A">
            <present targetdir="B" present="srconly"/>
            <date datetime="01/01/2001 12:00 AM" when="after"/>
        </fileset>
    </copy>

In this example I've used a presentselector to choose a file isn't present in the target directory (functionally the same as an overwrite check) and I've added an extra datecondition to test if the file has been modfied since a certain date.

在此示例中,我使用了当前选择器来选择目标目录中不存在的文件(在功能上与覆盖检查相同),并且我添加了一个额外的日期条件来测试该文件是否已被修改某个日期。

The selectorlist is quite extensive.

选择列表是相当广泛的。

回答by Joe

Ant's copy task's overwrite attribute states: "Overwrite existing files even if the destination files are newer."

Ant 的复制任务的覆盖属性指出:“即使目标文件较新,也会覆盖现有文件。”

So setting it to false only prevents them from being overwritten if they are newer, but if the original files are newer, they overwrite no matter what.

因此,将其设置为 false 只会防止它们在较新时被覆盖,但如果原始文件较新,则无论如何都会覆盖。

The only way i've found to get around this is to touch the destination files before copying, i.e:

我发现解决此问题的唯一方法是在复制之前触摸目标文件,即:

<touch>
  <fileset dir="${dest.dir}"/>
</touch>
<copy todir="${dest.dir}" overwrite="false">
  <fileset dir="${src.dir}"/>
</copy>

回答by Beta033

While <touch>works, it updates the file dates in the destination and as such it is not exactly true to the request.

<touch>工作时,它会更新目标中的文件日期,因此它并不完全符合请求。

According to the documentation If you have at least Ant 1.6.2, you can use the granularity attribute.

根据文档 如果您至少有 Ant 1.6.2,则可以使用粒度属性。

The number of milliseconds leeway to give before deciding a file is out of date. This is needed because not every file system supports tracking the last modified time to the millisecond level. Default is 1 second, or 2 seconds on DOS systems. This can also be useful if source and target files live on separate machines with clocks being out of sync. since Ant 1.6.2.

在决定文件之前给予的毫秒数已过时。这是必需的,因为并非每个文件系统都支持将最后修改时间跟踪到毫秒级别。默认值为 1 秒,或在 DOS 系统上为 2 秒。如果源文件和目标文件位于不同的机器上且时钟不同步,这也很有用。从 Ant 1.6.2 开始。

just set it to a large number. I use 9223372036854, which should be about 292 years (probably this is enough). (I just chopped the last 6 digits off Long.max) which is enough to not generate warnings about dates being in the future

只需将其设置为一个大数字。我用的是9223372036854,应该是292年左右(大概这个就够了)。(我只是将 Long.max 的最后 6 位数字剪掉了)这足以不产生关于未来日期的警告

<copy todir="${dest.dir}" overwrite="false" granularity="9223372036854">
  <fileset dir="${src.dir}"/>
</copy>

this will guarantee that if the files are there at allthey will be see as not old enough by the ant task and thus, not overwritten (and also not touched by another task)

这将保证如果文件在那里它们将被 ant 任务视为不够旧,因此不会被覆盖(也不会被另一个任务触及)

回答by Ali Tofigh

you can do it as below:

你可以这样做:

Copy a single file

复制单个文件

<copy file="myfile.txt" tofile="mycopy.txt"/>

Copy a single file to a directory

将单个文件复制到目录

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

Copy a directory to another directory

复制一个目录到另一个目录

<copy todir="../new/dir"><fileset dir="src_dir"/></copy>

<copy todir="../new/dir"><fileset dir="src_dir"/></copy>

Copy a set of files to a directory and more...

将一组文件复制到一个目录和更多...

please see this link for more information.Copy Task

请参阅此链接了解更多信息。复制任务