Apache Ant Copy Task

This task is used to copy file or resource to a new location. It copies only if the source files is newer than the destination file. We can also explicitly overwrite it by using overwrite attribute.

The todir attribute is used to set the destination path. This task uses various attributes which are given below.

Apache Ant Copy Task Attributes

AttributeDescriptionRequired
fileThe file which is to be copied.Yes
preservelastmodifiedPreserve last modified name.No
tofileThe file to copy to the destination file.if the file attribute is also specified, then only todir is allowed.
todirThe destination directory name.
overwriteOverwrite existing files even if the destination files are newer.No
forceOverwrite read-only destination files.No
filteringFilter during the copy.No
flattenCopy files by ignoring the directory structure of the source files.No
includeEmptyDirsCopy empty directories.No
failonerrorDisplay error message if copy is failed.No
quietIf true and failonerror is false, then do not log a warning message.No
verboseIt logs the files that are being copied.No
encodingEncoding for copying file.No
outputencodingDisplay the encoding to be used.No

Apache Ant Copy Task Example

Lets see an example in which we are copying data from one file to another. See the example below.

Copy single file

// build.xml

Copy file to directory

Copy directory to another directory





Latest Courses