source: other-projects/trunk/realistic-books/packages/AntInstaller/web/manual1.7.0/manual/CoreTasks/copy.html@ 19253

Last change on this file since 19253 was 19253, checked in by davidb, 15 years ago

Establishing a source code repository for Veronica's Realistic Book's software

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