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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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, you should
  19. limit the copy to text files. Binary files will be corrupted by the copy operation.
  20. This applies whether the filters are implicitly defined by the
  21. <a href="filter.html">filter</a> task or explicitly provided to the copy
  22. operation as <a href="../CoreTypes/filterset.html">filtersets</a>
  23. </p>
  24. <h3>Parameters</h3>
  25. <table border="1" cellpadding="2" cellspacing="0">
  26. <tr>
  27. <td valign="top"><b>Attribute</b></td>
  28. <td valign="top"><b>Description</b></td>
  29. <td align="center" valign="top"><b>Required</b></td>
  30. </tr>
  31. <tr>
  32. <td valign="top">file</td>
  33. <td valign="top">The file to copy.</td>
  34. <td valign="top" align="center">Yes, unless a nested
  35. resource collection element is used.</td>
  36. </tr>
  37. <tr>
  38. <td valign="top">preservelastmodified</td>
  39. <td valign="top">Give the copied files the same last modified
  40. time as the original source files.</td>
  41. <td valign="top" align="center">No; defaults to false.</td>
  42. </tr>
  43. <tr>
  44. <td valign="top">tofile</td>
  45. <td valign="top">The file to copy to.</td>
  46. <td valign="top" align="center" rowspan="2">With the <code>file</code>
  47. attribute, either <code>tofile</code> or <code>todir</code> can be used.
  48. With nested resource collection elements, if the number of included files
  49. is greater than 1, or if only the <code>dir</code> attribute is
  50. specified in the <code>&lt;fileset&gt;</code>, or if the
  51. <code>file</code> attribute is also specified, then only
  52. <code>todir</code> is allowed.</td>
  53. </tr>
  54. <tr>
  55. <td valign="top">todir</td>
  56. <td valign="top">The directory to copy to.</td>
  57. </tr>
  58. <tr>
  59. <td valign="top">overwrite</td>
  60. <td valign="top">Overwrite existing files even if the destination
  61. files are newer.</td>
  62. <td valign="top" align="center">No; defaults to false.</td>
  63. </tr>
  64. <tr>
  65. <td valign="top">filtering</td>
  66. <td valign="top">Indicates whether token filtering using the global
  67. build-file filters should take place during the copy.
  68. <em>Note</em>: Nested <code>&lt;filterset&gt;</code> elements will
  69. always be used, even if this attribute is not specified, or its value is
  70. <code>false</code> (<code>no</code>, or <code>off</code>).</td>
  71. <td valign="top" align="center">No; defaults to false.</td>
  72. </tr>
  73. <tr>
  74. <td valign="top">flatten</td>
  75. <td valign="top">Ignore the directory structure of the source files,
  76. and copy all files into the directory specified by the <code>todir</code>
  77. attribute. Note that you can achieve the same effect by using a
  78. <a href="../CoreTypes/mapper.html#flatten-mapper">flatten mapper</a>.</td>
  79. <td valign="top" align="center">No; defaults to false.</td>
  80. </tr>
  81. <tr>
  82. <td valign="top">includeEmptyDirs</td>
  83. <td valign="top">Copy any empty directories included in the FileSet(s).
  84. </td>
  85. <td valign="top" align="center">No; defaults to true.</td>
  86. </tr>
  87. <tr>
  88. <td valign="top">failonerror</td>
  89. <td valign="top">If false, log a warning message, but do not stop the
  90. build, when the file to copy does not exist or one of the nested
  91. filesets points to a directory that doesn't exist or an error occurs
  92. while copying.
  93. </td>
  94. <td valign="top" align="center">No; defaults to true.</td>
  95. </tr>
  96. <tr>
  97. <td valign="top">verbose</td>
  98. <td valign="top">Log the files that are being copied.</td>
  99. <td valign="top" align="center">No; defaults to false.</td>
  100. </tr>
  101. <tr>
  102. <td valign="top">encoding</td>
  103. <td valign="top">The encoding to assume when filter-copying the
  104. files. <em>since Ant 1.5</em>.</td>
  105. <td align="center">No - defaults to default JVM encoding</td>
  106. </tr>
  107. <tr>
  108. <td valign="top">outputencoding</td>
  109. <td valign="top">The encoding to use when writing the files.
  110. <em>since Ant 1.6</em>.</td>
  111. <td align="center">No - defaults to the value of the encoding
  112. attribute if given or the default JVM encoding otherwise.</td>
  113. </tr>
  114. <tr>
  115. <td valign="top">enablemultiplemappings</td>
  116. <td valign="top">
  117. If true the task will process to all the mappings for a
  118. given source path. If false the task will only process
  119. the first file or directory. This attribute is only relevant
  120. if there is a mapper subelement.
  121. <em>since Ant 1.6</em>.</td>
  122. <td align="center">No - defaults to false.</td>
  123. </tr>
  124. <tr>
  125. <td valign="top">granularity</td>
  126. <td valign="top">The number of milliseconds leeway to give before
  127. deciding a file is out of date. This is needed because not every
  128. file system supports tracking the last modified time to the
  129. millisecond level. Default is 1 second, or 2 seconds on DOS
  130. systems. This can also be useful if source and target files live
  131. on separate machines with clocks being out of sync. <em>since Ant
  132. 1.6.2</em>.</td>
  133. </tr>
  134. </table>
  135. <h3>Parameters specified as nested elements</h3>
  136. <h4>fileset or any other resource collection</h4>
  137. <p><a href="../CoreTypes/resources.html#collection">Resource
  138. Collection</a>s are used to select groups of files to copy. To use a
  139. resource collection, the <code>todir</code> attribute must be set.</p>
  140. <p>Prior to Ant 1.7 only <code>&lt;fileset&gt;</code> has been
  141. supported as a nested element.</p>
  142. <h4>mapper</h4>
  143. <p>You can define filename transformations by using a nested <a
  144. href="../CoreTypes/mapper.html">mapper</a> element. The default mapper used by
  145. <code>&lt;copy&gt;</code> is the <a
  146. href="../CoreTypes/mapper.html#identity-mapper">identity mapper</a>.</p>
  147. <p>
  148. <em>Since Ant 1.6.3</em>,
  149. one can use a filenamemapper type in place of the mapper element.
  150. </p>
  151. <p>Note that the source name handed to the mapper depends on the
  152. resource collection you use. If you use <code>&lt;fileset&gt;</code>
  153. or any other collection that provides a base directory, the name
  154. passed to the mapper will be a relative filename, relative to the base
  155. directory. In any other case the absolute filename of the source will
  156. be used.</p>
  157. <h4>filterset</h4>
  158. <p><a href="../CoreTypes/filterset.html">FilterSet</a>s are used to replace
  159. tokens in files that are copied.
  160. To use a FilterSet, use the nested <code>&lt;filterset&gt;</code> element.</p>
  161. <h4>filterchain</h4>
  162. <p>The Copy task supports nested <a href="../CoreTypes/filterchain.html">
  163. FilterChain</a>s.</p>
  164. <p>
  165. If <code>&lt;filterset&gt;</code> and <code>&lt;filterchain&gt;</code> elements are used inside the
  166. same <code>&lt;copy&gt;</code> task, all <code>&lt;filterchain&gt;</code> elements are processed first
  167. followed by <code>&lt;filterset&gt;</code> elements.
  168. </p>
  169. <h3>Examples</h3>
  170. <p><b>Copy a single file</b></p>
  171. <pre>
  172. &lt;copy file=&quot;myfile.txt&quot; tofile=&quot;mycopy.txt&quot;/&gt;
  173. </pre>
  174. <p><b>Copy a single file to a directory</b></p>
  175. <pre>
  176. &lt;copy file=&quot;myfile.txt&quot; todir=&quot;../some/other/dir&quot;/&gt;
  177. </pre>
  178. <p><b>Copy a directory to another directory</b></p>
  179. <pre>
  180. &lt;copy todir=&quot;../new/dir&quot;&gt;
  181. &lt;fileset dir=&quot;src_dir&quot;/&gt;
  182. &lt;/copy&gt;
  183. </pre>
  184. <p><b>Copy a set of files to a directory</b></p>
  185. <pre>
  186. &lt;copy todir=&quot;../dest/dir&quot;&gt;
  187. &lt;fileset dir=&quot;src_dir&quot;&gt;
  188. &lt;exclude name=&quot;**/*.java&quot;/&gt;
  189. &lt;/fileset&gt;
  190. &lt;/copy&gt;
  191. &lt;copy todir=&quot;../dest/dir&quot;&gt;
  192. &lt;fileset dir=&quot;src_dir&quot; excludes=&quot;**/*.java&quot;/&gt;
  193. &lt;/copy&gt;
  194. </pre>
  195. <p><b>Copy a set of files to a directory, appending
  196. <code>.bak</code> to the file name on the fly</b></p>
  197. <pre>
  198. &lt;copy todir=&quot;../backup/dir&quot;&gt;
  199. &lt;fileset dir=&quot;src_dir&quot;/&gt;
  200. &lt;globmapper from=&quot;*&quot; to=&quot;*.bak&quot;/&gt;
  201. &lt;/copy&gt;
  202. </pre>
  203. <p><b>Copy a set of files to a directory, replacing @TITLE@ with Foo Bar
  204. in all files.</b></p>
  205. <pre>
  206. &lt;copy todir=&quot;../backup/dir&quot;&gt;
  207. &lt;fileset dir=&quot;src_dir&quot;/&gt;
  208. &lt;filterset&gt;
  209. &lt;filter token=&quot;TITLE&quot; value=&quot;Foo Bar&quot;/&gt;
  210. &lt;/filterset&gt;
  211. &lt;/copy&gt;
  212. </pre>
  213. <p><b>Collect all items from the current CLASSPATH setting into a
  214. destination directory, flattening the directory structure.</b></p>
  215. <pre>
  216. &lt;copy todir=&quot;dest&quot; flatten=&quot;true&quot;&gt;
  217. &lt;path&gt;
  218. &lt;pathelement path=&quot;${java.class.path}&quot;/&gt;
  219. &lt;/path&gt;
  220. &lt;/copy&gt;
  221. </pre>
  222. <p><b>Copies some resources to a given directory.</b></p>
  223. <pre>
  224. &lt;copy todir=&quot;dest&quot; flatten=&quot;true&quot;&gt;
  225. &lt;resources&gt;
  226. &lt;file file=&quot;src_dir/file1.txt&quot;/&gt;
  227. &lt;url url=&quot;http://ant.apache.org/index.html&quot;/&gt;
  228. &lt;/resources&gt;
  229. &lt;/copy&gt;
  230. </pre>
  231. <p><b>Copies the two newest resources into a destination directory.</b></p>
  232. <pre>
  233. &lt;copy todir=&quot;dest&quot; flatten=&quot;true&quot;&gt;
  234. &lt;first count=&quot;2&quot;&gt;
  235. &lt;sort&gt;
  236. &lt;date xmlns=&quot;antlib:org.apache.tools.ant.types.resources.comparators&quot;/&gt;
  237. &lt;resources&gt;
  238. &lt;file file=&quot;src_dir/file1.txt&quot;/&gt;
  239. &lt;file file=&quot;src_dir/file2.txt&quot;/&gt;
  240. &lt;file file=&quot;src_dir/file3.txt&quot;/&gt;
  241. &lt;url url=&quot;http://ant.apache.org/index.html&quot;/&gt;
  242. &lt;/resources&gt;
  243. &lt;/sort&gt;
  244. &lt;/first&gt;
  245. &lt;/copy&gt;
  246. </pre>
  247. <p><strong>Unix Note:</strong> File permissions are not retained when files
  248. are copied; they end up with the default <code>UMASK</code> permissions
  249. instead. This
  250. is caused by the lack of any means to query or set file permissions in the
  251. current Java runtimes. If you need a permission-preserving copy function,
  252. use <code>&lt;exec executable="cp" ... &gt;</code> instead.
  253. </p>
  254. <p><strong>Windows Note:</strong> If you copy a file to a directory
  255. where that file already exists, but with different casing,
  256. the copied file takes on the case of the original. The workaround is to
  257. <a href="delete.html">delete</a>
  258. the file in the destination directory before you copy it.
  259. </p>
  260. <hr><p align="center">Copyright &copy; 2000-2005 The Apache Software Foundation.
  261. All rights Reserved.</p>
  262. </body>
  263. </html>