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

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