31/03/2012, 07:39
Examples
Copy a single file
<copy file="myfile.txt" tofile="mycopy.txt"/>
Copy a single file to a directory
<copy file="myfile.txt" todir="../some/other/dir"/>
Copy a directory to another directory
<copy todir="../new/dir">
<fileset dir="src_dir"/>
</copy>
Copy a set of files to a directory
<copy todir="../dest/dir">
<fileset dir="src_dir">
<exclude name="**/*.java"/>
</fileset>
</copy>
<copy todir="../dest/dir">
<fileset dir="src_dir" excludes="**/*.java"/>
</copy>
Copy a set of files to a directory, appending .bak to the file name on the fly
<copy todir="../backup/dir">
<fileset dir="src_dir"/>
<globmapper from="*" to="*.bak"/>
</copy>
Copy a set of files to a directory, replacing @TITLE@ with Foo Bar in all files.
<copy todir="../backup/dir">
<fileset dir="src_dir"/>
<filterset>
<filter token="TITLE" value="Foo Bar"/>
</filterset>
</copy>
Collect all items from the current CLASSPATH setting into a destination directory, flattening the directory structure.
<copy todir="dest" flatten="true">
<path>
<pathelement path="${java.class.path}"/>
</path>
</copy>
Copies some resources to a given directory.
<copy todir="dest" flatten="true">
<resources>
<file file="src_dir/file1.txt"/>
<url url="http://ant.apache.org/index.html"/>
</resources>
</copy>
If the example above didn't use the flatten attribute, the <file> resource would have returned its full path as source and target name and would not have been copied at all. In general it is a good practice to use an explicit mapper together with resources that use an absolute path as their names.
Copies the two newest resources into a destination directory.
<copy todir="dest" flatten="true">
<first count="2">
<sort>
<date xmlns="antlib:org.apache.tools.ant.types.resources.comparators"/>
<resources>
<file file="src_dir/file1.txt"/>
<file file="src_dir/file2.txt"/>
<file file="src_dir/file3.txt"/>
<url url="http://ant.apache.org/index.html"/>
</resources>
</sort>
</first>
</copy>
Copy a single file
<copy file="myfile.txt" tofile="mycopy.txt"/>
Copy a single file to a directory
<copy file="myfile.txt" todir="../some/other/dir"/>
Copy a directory to another directory
<copy todir="../new/dir">
<fileset dir="src_dir"/>
</copy>
Copy a set of files to a directory
<copy todir="../dest/dir">
<fileset dir="src_dir">
<exclude name="**/*.java"/>
</fileset>
</copy>
<copy todir="../dest/dir">
<fileset dir="src_dir" excludes="**/*.java"/>
</copy>
Copy a set of files to a directory, appending .bak to the file name on the fly
<copy todir="../backup/dir">
<fileset dir="src_dir"/>
<globmapper from="*" to="*.bak"/>
</copy>
Copy a set of files to a directory, replacing @TITLE@ with Foo Bar in all files.
<copy todir="../backup/dir">
<fileset dir="src_dir"/>
<filterset>
<filter token="TITLE" value="Foo Bar"/>
</filterset>
</copy>
Collect all items from the current CLASSPATH setting into a destination directory, flattening the directory structure.
<copy todir="dest" flatten="true">
<path>
<pathelement path="${java.class.path}"/>
</path>
</copy>
Copies some resources to a given directory.
<copy todir="dest" flatten="true">
<resources>
<file file="src_dir/file1.txt"/>
<url url="http://ant.apache.org/index.html"/>
</resources>
</copy>
If the example above didn't use the flatten attribute, the <file> resource would have returned its full path as source and target name and would not have been copied at all. In general it is a good practice to use an explicit mapper together with resources that use an absolute path as their names.
Copies the two newest resources into a destination directory.
<copy todir="dest" flatten="true">
<first count="2">
<sort>
<date xmlns="antlib:org.apache.tools.ant.types.resources.comparators"/>
<resources>
<file file="src_dir/file1.txt"/>
<file file="src_dir/file2.txt"/>
<file file="src_dir/file3.txt"/>
<url url="http://ant.apache.org/index.html"/>
</resources>
</sort>
</first>
</copy>