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
|
---|
30 | only copied if the source file is newer than the destination file,
|
---|
31 | or when the destination file does not exist. However, you can explicitly
|
---|
32 | overwrite files with the <code>overwrite</code> attribute.</p>
|
---|
33 |
|
---|
34 | <p><a href="../CoreTypes/resources.html#collection">Resource
|
---|
35 | Collection</a>s are used to select a group of files to copy. To use a
|
---|
36 | resource 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,
|
---|
40 | you should limit the copy to text files. Binary files will be corrupted
|
---|
41 | by the copy operation.
|
---|
42 | This applies whether the filters are implicitly defined by the
|
---|
43 | <a href="filter.html">filter</a> task or explicitly provided to the copy
|
---|
44 | operation 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><fileset></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><filterset></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
|
---|
162 | Collection</a>s are used to select groups of files to copy. To use a
|
---|
163 | resource collection, the <code>todir</code> attribute must be set.</p>
|
---|
164 | <p>Prior to Ant 1.7 only <code><fileset></code> has been
|
---|
165 | supported 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><copy></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
|
---|
178 | resource collection you use. If you use <code><fileset></code>
|
---|
179 | or any other collection that provides a base directory, the name
|
---|
180 | passed to the mapper will be a relative filename, relative to the base
|
---|
181 | directory. In any other case the absolute filename of the source will
|
---|
182 | be used.</p>
|
---|
183 |
|
---|
184 | <h4>filterset</h4>
|
---|
185 | <p><a href="../CoreTypes/filterset.html">FilterSet</a>s are used to replace
|
---|
186 | tokens in files that are copied.
|
---|
187 | To use a FilterSet, use the nested <code><filterset></code> element.</p>
|
---|
188 |
|
---|
189 | <h4>filterchain</h4>
|
---|
190 | <p>The Copy task supports nested <a href="../CoreTypes/filterchain.html">
|
---|
191 | FilterChain</a>s.</p>
|
---|
192 |
|
---|
193 | <p>
|
---|
194 | If <code><filterset></code> and <code><filterchain></code> elements are used inside the
|
---|
195 | same <code><copy></code> task, all <code><filterchain></code> elements are processed first
|
---|
196 | followed by <code><filterset></code> elements.
|
---|
197 | </p>
|
---|
198 |
|
---|
199 | <h3>Examples</h3>
|
---|
200 | <p><b>Copy a single file</b></p>
|
---|
201 | <pre>
|
---|
202 | <copy file="myfile.txt" tofile="mycopy.txt"/>
|
---|
203 | </pre>
|
---|
204 | <p><b>Copy a single file to a directory</b></p>
|
---|
205 | <pre>
|
---|
206 | <copy file="myfile.txt" todir="../some/other/dir"/>
|
---|
207 | </pre>
|
---|
208 | <p><b>Copy a directory to another directory</b></p>
|
---|
209 | <pre>
|
---|
210 | <copy todir="../new/dir">
|
---|
211 | <fileset dir="src_dir"/>
|
---|
212 | </copy>
|
---|
213 | </pre>
|
---|
214 | <p><b>Copy a set of files to a directory</b></p>
|
---|
215 | <pre>
|
---|
216 | <copy todir="../dest/dir">
|
---|
217 | <fileset dir="src_dir">
|
---|
218 | <exclude name="**/*.java"/>
|
---|
219 | </fileset>
|
---|
220 | </copy>
|
---|
221 |
|
---|
222 | <copy todir="../dest/dir">
|
---|
223 | <fileset dir="src_dir" excludes="**/*.java"/>
|
---|
224 | </copy>
|
---|
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 | <copy todir="../backup/dir">
|
---|
230 | <fileset dir="src_dir"/>
|
---|
231 | <globmapper from="*" to="*.bak"/>
|
---|
232 | </copy>
|
---|
233 | </pre>
|
---|
234 |
|
---|
235 | <p><b>Copy a set of files to a directory, replacing @TITLE@ with Foo Bar
|
---|
236 | in all files.</b></p>
|
---|
237 | <pre>
|
---|
238 | <copy todir="../backup/dir">
|
---|
239 | <fileset dir="src_dir"/>
|
---|
240 | <filterset>
|
---|
241 | <filter token="TITLE" value="Foo Bar"/>
|
---|
242 | </filterset>
|
---|
243 | </copy>
|
---|
244 | </pre>
|
---|
245 |
|
---|
246 | <p><b>Collect all items from the current CLASSPATH setting into a
|
---|
247 | destination directory, flattening the directory structure.</b></p>
|
---|
248 | <pre>
|
---|
249 | <copy todir="dest" flatten="true">
|
---|
250 | <path>
|
---|
251 | <pathelement path="${java.class.path}"/>
|
---|
252 | </path>
|
---|
253 | </copy>
|
---|
254 | </pre>
|
---|
255 |
|
---|
256 | <p><b>Copies some resources to a given directory.</b></p>
|
---|
257 | <pre>
|
---|
258 | <copy todir="dest" flatten="true">
|
---|
259 | <resources>
|
---|
260 | <file file="src_dir/file1.txt"/>
|
---|
261 | <url url="http://ant.apache.org/index.html"/>
|
---|
262 | </resources>
|
---|
263 | </copy>
|
---|
264 | </pre>
|
---|
265 |
|
---|
266 | <p><b>Copies the two newest resources into a destination directory.</b></p>
|
---|
267 | <pre>
|
---|
268 | <copy todir="dest" flatten="true">
|
---|
269 | <first count="2">
|
---|
270 | <sort>
|
---|
271 | <date xmlns="antlib:org.apache.tools.ant.types.resources.comparators"/>
|
---|
272 | <resources>
|
---|
273 | <file file="src_dir/file1.txt"/>
|
---|
274 | <file file="src_dir/file2.txt"/>
|
---|
275 | <file file="src_dir/file3.txt"/>
|
---|
276 | <url url="http://ant.apache.org/index.html"/>
|
---|
277 | </resources>
|
---|
278 | </sort>
|
---|
279 | </first>
|
---|
280 | </copy>
|
---|
281 | </pre>
|
---|
282 |
|
---|
283 |
|
---|
284 | <p><strong>Unix Note:</strong> File permissions are not retained when files
|
---|
285 | are copied; they end up with the default <code>UMASK</code> permissions
|
---|
286 | instead. This
|
---|
287 | is caused by the lack of any means to query or set file permissions in the
|
---|
288 | current Java runtimes. If you need a permission-preserving copy function,
|
---|
289 | use <code><exec executable="cp" ... ></code> instead.
|
---|
290 | </p>
|
---|
291 |
|
---|
292 | <p><strong>Windows Note:</strong> If you copy a file to a directory
|
---|
293 | where that file already exists, but with different casing,
|
---|
294 | the copied file takes on the case of the original. The workaround is to
|
---|
295 | <a href="delete.html">delete</a>
|
---|
296 | the 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>
|
---|