Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ant copy files without overwriting

Tags:

java

build

ant

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.

like image 848
user204069 Avatar asked Aug 17 '10 13:08

user204069


People also ask

What is FileSet in Ant?

A FileSet is a group of files. These files can be found in a directory tree starting in a base directory and are matched by patterns taken from a number of PatternSets and Selectors. PatternSets can be specified as nested <patternset> elements.

What is PathElement in ant?

Path: This object represents a path as used by CLASSPATH or PATH environment variable. A path might also be described as a collection of unique filesystem resources. and PathElement: Helper class, holds the nested <pathelement> values.

What is target in Ant build?

A target is a container of tasks and datatypes that cooperate to reach a desired state during the build process. Targets can depend on other targets and Apache Ant ensures that these other targets have been executed before the current target.


4 Answers

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>
like image 60
Joe Avatar answered Oct 17 '22 04:10

Joe


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

like image 25
Ali Tofigh Avatar answered Oct 17 '22 03:10

Ali Tofigh


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)

like image 11
Beta033 Avatar answered Oct 17 '22 03:10

Beta033


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

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

like image 3
Sean Patrick Floyd Avatar answered Oct 17 '22 03:10

Sean Patrick Floyd