You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

copy.html 13 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Language" content="en-us">
  4. <link rel="stylesheet" type="text/css" href="../stylesheets/style.css">
  5. <title>Copy Task</title>
  6. </head>
  7. <body>
  8. <h2><a name="copy">Copy</a></h2>
  9. <h3>Description</h3>
  10. <p>Copies a file or resource collection to a new file or directory. By default, files are
  11. only copied if the source file is newer than the destination file,
  12. or when the destination file does not exist. However, you can explicitly
  13. overwrite files with the <code>overwrite</code> attribute.</p>
  14. <p><a href="../CoreTypes/resources.html#collection">Resource
  15. Collection</a>s are used to select a group of files to copy. To use a
  16. resource collection, the <code>todir</code> attribute must be set.</p>
  17. <p>
  18. <strong>Note: </strong>If you employ filters in your copy operation,
  19. you should limit the copy to text files. Binary files will be corrupted
  20. by the copy operation.
  21. This applies whether the filters are implicitly defined by the
  22. <a href="filter.html">filter</a> task or explicitly provided to the copy
  23. operation as <a href="../CoreTypes/filterset.html">filtersets</a>.
  24. <em>See <a href="#encoding">encoding note</a></em>.
  25. </p>
  26. <h3>Parameters</h3>
  27. <table border="1" cellpadding="2" cellspacing="0">
  28. <tr>
  29. <td valign="top"><b>Attribute</b></td>
  30. <td valign="top"><b>Description</b></td>
  31. <td align="center" valign="top"><b>Required</b></td>
  32. </tr>
  33. <tr>
  34. <td valign="top">file</td>
  35. <td valign="top">The file to copy.</td>
  36. <td valign="top" align="center">Yes, unless a nested
  37. resource collection element is used.</td>
  38. </tr>
  39. <tr>
  40. <td valign="top">preservelastmodified</td>
  41. <td valign="top">Give the copied files the same last modified
  42. time as the original source files.</td>
  43. <td valign="top" align="center">No; defaults to false.</td>
  44. </tr>
  45. <tr>
  46. <td valign="top">tofile</td>
  47. <td valign="top">The file to copy to.</td>
  48. <td valign="top" align="center" rowspan="2">With the <code>file</code>
  49. attribute, either <code>tofile</code> or <code>todir</code> can be used.
  50. With nested resource collection elements, if the number of included files
  51. is greater than 1, or if only the <code>dir</code> attribute is
  52. specified in the <code>&lt;fileset&gt;</code>, or if the
  53. <code>file</code> attribute is also specified, then only
  54. <code>todir</code> is allowed.</td>
  55. </tr>
  56. <tr>
  57. <td valign="top">todir</td>
  58. <td valign="top">The directory to copy to.</td>
  59. </tr>
  60. <tr>
  61. <td valign="top">overwrite</td>
  62. <td valign="top">Overwrite existing files even if the destination
  63. files are newer.</td>
  64. <td valign="top" align="center">No; defaults to false.</td>
  65. </tr>
  66. <tr>
  67. <td valign="top">filtering</td>
  68. <td valign="top">Indicates whether token filtering using the global
  69. build-file filters should take place during the copy.
  70. <em>Note</em>: Nested <code>&lt;filterset&gt;</code> elements will
  71. always be used, even if this attribute is not specified, or its value is
  72. <code>false</code> (<code>no</code>, or <code>off</code>).</td>
  73. <td valign="top" align="center">No; defaults to false.</td>
  74. </tr>
  75. <tr>
  76. <td valign="top">flatten</td>
  77. <td valign="top">Ignore the directory structure of the source files,
  78. and copy all files into the directory specified by the <code>todir</code>
  79. attribute. Note that you can achieve the same effect by using a
  80. <a href="../CoreTypes/mapper.html#flatten-mapper">flatten mapper</a>.</td>
  81. <td valign="top" align="center">No; defaults to false.</td>
  82. </tr>
  83. <tr>
  84. <td valign="top">includeEmptyDirs</td>
  85. <td valign="top">Copy any empty directories included in the FileSet(s).
  86. </td>
  87. <td valign="top" align="center">No; defaults to true.</td>
  88. </tr>
  89. <tr>
  90. <td valign="top">failonerror</td>
  91. <td valign="top">If false, log a warning message, but do not stop the
  92. build, when the file to copy does not exist or one of the nested
  93. filesets points to a directory that doesn't exist or an error occurs
  94. while copying.
  95. </td>
  96. <td valign="top" align="center">No; defaults to true.</td>
  97. </tr>
  98. <tr>
  99. <td valign="top">verbose</td>
  100. <td valign="top">Log the files that are being copied.</td>
  101. <td valign="top" align="center">No; defaults to false.</td>
  102. </tr>
  103. <tr>
  104. <td valign="top">encoding</td>
  105. <td valign="top">The encoding to assume when filter-copying the
  106. files. <em>since Ant 1.5</em>.</td>
  107. <td align="center">No - defaults to default JVM encoding</td>
  108. </tr>
  109. <tr>
  110. <td valign="top">outputencoding</td>
  111. <td valign="top">The encoding to use when writing the files.
  112. <em>since Ant 1.6</em>.</td>
  113. <td align="center">No - defaults to the value of the encoding
  114. attribute if given or the default JVM encoding otherwise.</td>
  115. </tr>
  116. <tr>
  117. <td valign="top">enablemultiplemappings</td>
  118. <td valign="top">
  119. If true the task will process to all the mappings for a
  120. given source path. If false the task will only process
  121. the first file or directory. This attribute is only relevant
  122. if there is a mapper subelement.
  123. <em>since Ant 1.6</em>.</td>
  124. <td align="center">No - defaults to false.</td>
  125. </tr>
  126. <tr>
  127. <td valign="top">granularity</td>
  128. <td valign="top">The number of milliseconds leeway to give before
  129. deciding a file is out of date. This is needed because not every
  130. file system supports tracking the last modified time to the
  131. millisecond level. Default is 1 second, or 2 seconds on DOS
  132. systems. This can also be useful if source and target files live
  133. on separate machines with clocks being out of sync. <em>since Ant
  134. 1.6.2</em>.</td>
  135. </tr>
  136. </table>
  137. <h3>Parameters specified as nested elements</h3>
  138. <h4>fileset or any other resource collection</h4>
  139. <p><a href="../CoreTypes/resources.html#collection">Resource
  140. Collection</a>s are used to select groups of files to copy. To use a
  141. resource collection, the <code>todir</code> attribute must be set.</p>
  142. <p>Prior to Ant 1.7 only <code>&lt;fileset&gt;</code> has been
  143. supported as a nested element.</p>
  144. <h4>mapper</h4>
  145. <p>You can define filename transformations by using a nested <a
  146. href="../CoreTypes/mapper.html">mapper</a> element. The default mapper used by
  147. <code>&lt;copy&gt;</code> is the <a
  148. href="../CoreTypes/mapper.html#identity-mapper">identity mapper</a>.</p>
  149. <p>
  150. <em>Since Ant 1.6.3</em>,
  151. one can use a filenamemapper type in place of the mapper element.
  152. </p>
  153. <p>Note that the source name handed to the mapper depends on the
  154. resource collection you use. If you use <code>&lt;fileset&gt;</code>
  155. or any other collection that provides a base directory, the name
  156. passed to the mapper will be a relative filename, relative to the base
  157. directory. In any other case the absolute filename of the source will
  158. be used.</p>
  159. <h4>filterset</h4>
  160. <p><a href="../CoreTypes/filterset.html">FilterSet</a>s are used to replace
  161. tokens in files that are copied.
  162. To use a FilterSet, use the nested <code>&lt;filterset&gt;</code> element.</p>
  163. <h4>filterchain</h4>
  164. <p>The Copy task supports nested <a href="../CoreTypes/filterchain.html">
  165. FilterChain</a>s.</p>
  166. <p>
  167. If <code>&lt;filterset&gt;</code> and <code>&lt;filterchain&gt;</code> elements are used inside the
  168. same <code>&lt;copy&gt;</code> task, all <code>&lt;filterchain&gt;</code> elements are processed first
  169. followed by <code>&lt;filterset&gt;</code> elements.
  170. </p>
  171. <h3>Examples</h3>
  172. <p><b>Copy a single file</b></p>
  173. <pre>
  174. &lt;copy file=&quot;myfile.txt&quot; tofile=&quot;mycopy.txt&quot;/&gt;
  175. </pre>
  176. <p><b>Copy a single file to a directory</b></p>
  177. <pre>
  178. &lt;copy file=&quot;myfile.txt&quot; todir=&quot;../some/other/dir&quot;/&gt;
  179. </pre>
  180. <p><b>Copy a directory to another directory</b></p>
  181. <pre>
  182. &lt;copy todir=&quot;../new/dir&quot;&gt;
  183. &lt;fileset dir=&quot;src_dir&quot;/&gt;
  184. &lt;/copy&gt;
  185. </pre>
  186. <p><b>Copy a set of files to a directory</b></p>
  187. <pre>
  188. &lt;copy todir=&quot;../dest/dir&quot;&gt;
  189. &lt;fileset dir=&quot;src_dir&quot;&gt;
  190. &lt;exclude name=&quot;**/*.java&quot;/&gt;
  191. &lt;/fileset&gt;
  192. &lt;/copy&gt;
  193. &lt;copy todir=&quot;../dest/dir&quot;&gt;
  194. &lt;fileset dir=&quot;src_dir&quot; excludes=&quot;**/*.java&quot;/&gt;
  195. &lt;/copy&gt;
  196. </pre>
  197. <p><b>Copy a set of files to a directory, appending
  198. <code>.bak</code> to the file name on the fly</b></p>
  199. <pre>
  200. &lt;copy todir=&quot;../backup/dir&quot;&gt;
  201. &lt;fileset dir=&quot;src_dir&quot;/&gt;
  202. &lt;globmapper from=&quot;*&quot; to=&quot;*.bak&quot;/&gt;
  203. &lt;/copy&gt;
  204. </pre>
  205. <p><b>Copy a set of files to a directory, replacing @TITLE@ with Foo Bar
  206. in all files.</b></p>
  207. <pre>
  208. &lt;copy todir=&quot;../backup/dir&quot;&gt;
  209. &lt;fileset dir=&quot;src_dir&quot;/&gt;
  210. &lt;filterset&gt;
  211. &lt;filter token=&quot;TITLE&quot; value=&quot;Foo Bar&quot;/&gt;
  212. &lt;/filterset&gt;
  213. &lt;/copy&gt;
  214. </pre>
  215. <p><b>Collect all items from the current CLASSPATH setting into a
  216. destination directory, flattening the directory structure.</b></p>
  217. <pre>
  218. &lt;copy todir=&quot;dest&quot; flatten=&quot;true&quot;&gt;
  219. &lt;path&gt;
  220. &lt;pathelement path=&quot;${java.class.path}&quot;/&gt;
  221. &lt;/path&gt;
  222. &lt;/copy&gt;
  223. </pre>
  224. <p><b>Copies some resources to a given directory.</b></p>
  225. <pre>
  226. &lt;copy todir=&quot;dest&quot; flatten=&quot;true&quot;&gt;
  227. &lt;resources&gt;
  228. &lt;file file=&quot;src_dir/file1.txt&quot;/&gt;
  229. &lt;url url=&quot;http://ant.apache.org/index.html&quot;/&gt;
  230. &lt;/resources&gt;
  231. &lt;/copy&gt;
  232. </pre>
  233. <p><b>Copies the two newest resources into a destination directory.</b></p>
  234. <pre>
  235. &lt;copy todir=&quot;dest&quot; flatten=&quot;true&quot;&gt;
  236. &lt;first count=&quot;2&quot;&gt;
  237. &lt;sort&gt;
  238. &lt;date xmlns=&quot;antlib:org.apache.tools.ant.types.resources.comparators&quot;/&gt;
  239. &lt;resources&gt;
  240. &lt;file file=&quot;src_dir/file1.txt&quot;/&gt;
  241. &lt;file file=&quot;src_dir/file2.txt&quot;/&gt;
  242. &lt;file file=&quot;src_dir/file3.txt&quot;/&gt;
  243. &lt;url url=&quot;http://ant.apache.org/index.html&quot;/&gt;
  244. &lt;/resources&gt;
  245. &lt;/sort&gt;
  246. &lt;/first&gt;
  247. &lt;/copy&gt;
  248. </pre>
  249. <p><strong>Unix Note:</strong> File permissions are not retained when files
  250. are copied; they end up with the default <code>UMASK</code> permissions
  251. instead. This
  252. is caused by the lack of any means to query or set file permissions in the
  253. current Java runtimes. If you need a permission-preserving copy function,
  254. use <code>&lt;exec executable="cp" ... &gt;</code> instead.
  255. </p>
  256. <p><strong>Windows Note:</strong> If you copy a file to a directory
  257. where that file already exists, but with different casing,
  258. the copied file takes on the case of the original. The workaround is to
  259. <a href="delete.html">delete</a>
  260. the file in the destination directory before you copy it.
  261. </p>
  262. <p>
  263. <strong><a name="encoding">Important Encoding Note:</a></strong>
  264. The reason that binary files when filtered get corrupted is that
  265. filtering involves reading in the file using a Reader class. This
  266. has an encoding specifing how files are encoded. There are a number
  267. of different types of encoding - UTF-8, UTF-16, Cp1252, ISO-8859-1,
  268. US-ASCII and (lots) others. On Windows the default character encoding
  269. is Cp1252, on Unix it is usually UTF-8. For both of these encoding
  270. there are illegal byte sequences (more in UTF-8 than for Cp1252).
  271. </p>
  272. <p>
  273. How the Reader class deals with these illegal sequences is up to the
  274. implementation
  275. of the character decoder. The current Sun Java implemenation is to
  276. map them to legal characters. Previous Sun Java (1.3 and lower) threw
  277. a MalformedInputException. IBM Java 1.4 also thows this exception.
  278. It is the mapping of the characters that cause the corruption.
  279. </p>
  280. <p>
  281. On Unix, where the default is normally UTF-8, this is a <em>big</em>
  282. problem, as it is easy to edit a file to contain non US Ascii characters
  283. from ISO-8859-1, for example the Danish oe character. When this is
  284. copied (with filtering) by Ant, the character get converted to a
  285. question mark (or some such thing).
  286. </p>
  287. <p>
  288. There is not much that Ant can do. It cannot figure out which
  289. files are binary - a UTF-8 version of Korean will have lots of
  290. bytes with the top bit set. It is not informed about illegal
  291. character sequences by current Sun Java implementions.
  292. </p>
  293. <p>
  294. One trick for filtering containing only US-ASCII is to
  295. use the ISO-8859-1 encoding. This does not seem to contain
  296. illegal character sequences, and the lower 7 bits are US-ASCII.
  297. Another trick is to change the LANG environment variable from
  298. something like "us.utf8" to "us".
  299. </p>
  300. <hr><p align="center">Copyright &copy; 2000-2006 The Apache Software Foundation.
  301. All rights Reserved.</p>
  302. </body></html>