问题
Is there any command in ant to copy files from one folder structure to another without checking the last modified date/time overwriting 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.
I saw "ant copy" and "ant move" commands, but didn't help. Any suggestions.
回答1:
Ant's copy task's overwrite attribute states: "Overwrite existing files even if the destination files are newer."
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.
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>
回答2:
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 a set of files to a directory and more...
please see this link for more information.Copy Task
回答3:
While <touch>
works, it updates the file dates in the destination and as such it is not exactly true to the request.
According to the documentation If you have at least Ant 1.6.2, you can use the granularity attribute.
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.
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
<copy todir="${dest.dir}" overwrite="false" granularity="9223372036854">
<fileset dir="${src.dir}"/>
</copy>
this will guarantee that if the files are there at all they will be see as not old enough by the ant task and thus, not overwritten (and also not touched by another task)
回答4:
If there isn't an elegant solution you can always do it in a sequence:
<move>
folder<B>
to<C>
<copy>
files of folder<A>
to<B>
<copy>
files of folder<C>
to<B>
usingoverwrite=true
(Do things the other way around, replacing the new files, not the old ones)
回答5:
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 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 present selector to choose a file isn't present in the target directory (functionally the same as an overwrite check) and I've added an extra date condition to test if the file has been modfied since a certain date.
The selector list is quite extensive.
回答6:
Just use the <copy>
tag with a fileset as **/*
and set the overwrite attribute to false explicitly. I use this all the time:
<copy todir="/your/target" overwrite="false">
<fileset dir="/your/source" />
</copy>
来源:https://stackoverflow.com/questions/3502964/ant-copy-files-without-overwriting