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 14 KiB

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