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 KiB

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