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 15 kB

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