Class Copy
- java.lang.Object
- 
- org.apache.tools.ant.ProjectComponent
- 
- org.apache.tools.ant.Task
- 
- org.apache.tools.ant.taskdefs.Copy
 
 
 
- 
- All Implemented Interfaces:
- java.lang.Cloneable
 - Direct Known Subclasses:
- Move,- Sync.MyCopy
 
 public class Copy extends Task Copies a file or directory to a new file or directory. Files are only copied if the source file is newer than the destination file, or when the destination file does not exist. It is possible to explicitly overwrite existing files. This implementation is based on Arnout Kuiper's initial design document, the following mailing list discussions, and the copyfile/copydir tasks. - Since:
- Ant 1.2
 
- 
- 
Field SummaryFields Modifier and Type Field Description protected java.util.Hashtable<java.io.File,java.io.File>completeDirMapprotected java.io.FiledestDirprotected java.io.FiledestFileprotected java.util.Hashtable<java.lang.String,java.lang.String[]>dirCopyMapprotected booleanfailonerrorprotected java.io.Filefileprotected java.util.Hashtable<java.lang.String,java.lang.String[]>fileCopyMapprotected java.util.Vector<ResourceCollection>filesetsprotected FileUtilsfileUtilsprotected booleanfilteringprotected booleanflattenprotected booleanforceOverwriteprotected booleanincludeEmptyprotected MappermapperElementprotected booleanpreserveLastModifiedprotected java.util.Vector<ResourceCollection>rcsprotected intverbosity- 
Fields inherited from class org.apache.tools.ant.ProjectComponentdescription, location, project
 
- 
 - 
Constructor SummaryConstructors Constructor Description Copy()Copy task constructor.
 - 
Method SummaryModifier and Type Method Description voidadd(ResourceCollection res)Add a collection of files to copy.voidadd(FileNameMapper fileNameMapper)Add a nested filenamemapper.voidaddFileset(FileSet set)Add a set of files to copy.protected voidbuildMap(java.io.File fromDir, java.io.File toDir, java.lang.String[] names, FileNameMapper mapper, java.util.Hashtable<java.lang.String,java.lang.String[]> map)Add to a map of files/directories to copy.protected java.util.Map<Resource,java.lang.String[]>buildMap(Resource[] fromResources, java.io.File toDir, FileNameMapper mapper)Create a map of resources to copy.FilterChaincreateFilterChain()Add a FilterChain.FilterSetcreateFilterSet()Add a filterset.MappercreateMapper()Define the mapper to map source to destination files.protected voiddoFileOperations()Actually does the file (and possibly empty directory) copies.protected voiddoResourceOperations(java.util.Map<Resource,java.lang.String[]> map)Actually does the resource copies.voidexecute()Perform the copy operation.java.lang.StringgetEncoding()Get the character encoding to be used.protected FileUtilsgetFileUtils()Get the FileUtils for this task.protected java.util.Vector<FilterChain>getFilterChains()Get the filterchains being applied to this operation.protected java.util.Vector<FilterSet>getFilterSets()Get the filtersets being applied to this operation.booleangetForce()Whether read-only destinations will be overwritten.java.lang.StringgetOutputEncoding()Get the character encoding for output files.booleangetPreserveLastModified()Get whether to give the copied files the same last modified time as the original files.booleanisEnableMultipleMapping()Get whether multiple mapping is enabled.protected voidscan(java.io.File fromDir, java.io.File toDir, java.lang.String[] files, java.lang.String[] dirs)Compares source files to destination files to see if they should be copied.protected java.util.Map<Resource,java.lang.String[]>scan(Resource[] fromResources, java.io.File toDir)Compares source resources to destination files to see if they should be copied.voidsetEnableMultipleMappings(boolean enableMultipleMappings)Set method of handling mappers that return multiple mappings for a given source path.voidsetEncoding(java.lang.String encoding)Set the character encoding.voidsetFailOnError(boolean failonerror)Set whether to fail when errors are encountered.voidsetFile(java.io.File file)Set a single source file to copy.voidsetFiltering(boolean filtering)Set filtering mode.voidsetFlatten(boolean flatten)Set whether files copied from directory trees will be "flattened" into a single directory.voidsetForce(boolean f)Whether read-only destinations will be overwritten.voidsetGranularity(long granularity)Set the number of milliseconds leeway to give before deciding a target is out of date.voidsetIncludeEmptyDirs(boolean includeEmpty)Set whether to copy empty directories.voidsetOutputEncoding(java.lang.String encoding)Set the character encoding for output files.voidsetOverwrite(boolean overwrite)Set overwrite mode regarding existing destination file(s).voidsetPreserveLastModified(boolean preserve)Give the copied files the same last modified time as the original files.voidsetPreserveLastModified(java.lang.String preserve)Deprecated.since 1.5.x.voidsetQuiet(boolean quiet)Set quiet mode.voidsetTodir(java.io.File destDir)Set the destination directory.voidsetTofile(java.io.File destFile)Set the destination file.voidsetVerbose(boolean verbose)Set verbose mode.protected booleansupportsNonFileResources()Whether this task can deal with non-file resources.protected voidvalidateAttributes()Ensure we have a consistent and legal set of attributes, and set any internal flags necessary based on different combinations of attributes.- 
Methods inherited from class org.apache.tools.ant.TaskbindToOwner, getOwningTarget, getRuntimeConfigurableWrapper, getTaskName, getTaskType, getWrapper, handleErrorFlush, handleErrorOutput, handleFlush, handleInput, handleOutput, init, isInvalid, log, log, log, log, maybeConfigure, perform, reconfigure, setOwningTarget, setRuntimeConfigurableWrapper, setTaskName, setTaskType
 - 
Methods inherited from class org.apache.tools.ant.ProjectComponentclone, getDescription, getLocation, getProject, setDescription, setLocation, setProject
 
- 
 
- 
- 
- 
Field Detail- 
fileprotected java.io.File file 
 - 
destFileprotected java.io.File destFile 
 - 
destDirprotected java.io.File destDir 
 - 
rcsprotected java.util.Vector<ResourceCollection> rcs 
 - 
filesetsprotected java.util.Vector<ResourceCollection> filesets 
 - 
filteringprotected boolean filtering 
 - 
preserveLastModifiedprotected boolean preserveLastModified 
 - 
forceOverwriteprotected boolean forceOverwrite 
 - 
flattenprotected boolean flatten 
 - 
verbosityprotected int verbosity 
 - 
includeEmptyprotected boolean includeEmpty 
 - 
failonerrorprotected boolean failonerror 
 - 
fileCopyMapprotected java.util.Hashtable<java.lang.String,java.lang.String[]> fileCopyMap 
 - 
dirCopyMapprotected java.util.Hashtable<java.lang.String,java.lang.String[]> dirCopyMap 
 - 
completeDirMapprotected java.util.Hashtable<java.io.File,java.io.File> completeDirMap 
 - 
mapperElementprotected Mapper mapperElement 
 - 
fileUtilsprotected FileUtils fileUtils 
 
- 
 - 
Method Detail- 
getFileUtilsprotected FileUtils getFileUtils() Get the FileUtils for this task.- Returns:
- the fileutils object.
 
 - 
setFilepublic void setFile(java.io.File file) Set a single source file to copy.- Parameters:
- file- the file to copy.
 
 - 
setTofilepublic void setTofile(java.io.File destFile) Set the destination file.- Parameters:
- destFile- the file to copy to.
 
 - 
setTodirpublic void setTodir(java.io.File destDir) Set the destination directory.- Parameters:
- destDir- the destination directory.
 
 - 
createFilterChainpublic FilterChain createFilterChain() Add a FilterChain.- Returns:
- a filter chain object.
 
 - 
createFilterSetpublic FilterSet createFilterSet() Add a filterset.- Returns:
- a filter set object.
 
 - 
setPreserveLastModified@Deprecated public void setPreserveLastModified(java.lang.String preserve) Deprecated.since 1.5.x. setPreserveLastModified(String) has been deprecated and replaced with setPreserveLastModified(boolean) to consistently let the Introspection mechanism work.Give the copied files the same last modified time as the original files.- Parameters:
- preserve- a boolean string.
 
 - 
setPreserveLastModifiedpublic void setPreserveLastModified(boolean preserve) Give the copied files the same last modified time as the original files.- Parameters:
- preserve- if true preserve the modified time; default is false.
 
 - 
getPreserveLastModifiedpublic boolean getPreserveLastModified() Get whether to give the copied files the same last modified time as the original files.- Returns:
- the whether destination files will inherit the modification times of the corresponding source files.
- Since:
- 1.32, Ant 1.5
 
 - 
getFilterSetsprotected java.util.Vector<FilterSet> getFilterSets() Get the filtersets being applied to this operation.- Returns:
- a vector of FilterSet objects.
 
 - 
getFilterChainsprotected java.util.Vector<FilterChain> getFilterChains() Get the filterchains being applied to this operation.- Returns:
- a vector of FilterChain objects.
 
 - 
setFilteringpublic void setFiltering(boolean filtering) Set filtering mode.- Parameters:
- filtering- if true enable filtering; default is false.
 
 - 
setOverwritepublic void setOverwrite(boolean overwrite) Set overwrite mode regarding existing destination file(s).- Parameters:
- overwrite- if true force overwriting of destination file(s) even if the destination file(s) are younger than the corresponding source file. Default is false.
 
 - 
setForcepublic void setForce(boolean f) Whether read-only destinations will be overwritten.Defaults to false - Parameters:
- f- boolean
- Since:
- Ant 1.8.2
 
 - 
getForcepublic boolean getForce() Whether read-only destinations will be overwritten.- Returns:
- boolean
- Since:
- Ant 1.8.2
 
 - 
setFlattenpublic void setFlatten(boolean flatten) Set whether files copied from directory trees will be "flattened" into a single directory. If there are multiple files with the same name in the source directory tree, only the first file will be copied into the "flattened" directory, unless the forceoverwrite attribute is true.- Parameters:
- flatten- if true flatten the destination directory. Default is false.
 
 - 
setVerbosepublic void setVerbose(boolean verbose) Set verbose mode. Used to force listing of all names of copied files.- Parameters:
- verbose- whether to output the names of copied files. Default is false.
 
 - 
setIncludeEmptyDirspublic void setIncludeEmptyDirs(boolean includeEmpty) Set whether to copy empty directories.- Parameters:
- includeEmpty- if true copy empty directories. Default is true.
 
 - 
setQuietpublic void setQuiet(boolean quiet) Set quiet mode. Used to hide messages when a file or directory to be copied does not exist.- Parameters:
- quiet- whether or not to display error messages when a file or directory does not exist. Default is false.
 
 - 
setEnableMultipleMappingspublic void setEnableMultipleMappings(boolean enableMultipleMappings) Set method of handling mappers that return multiple mappings for a given source path.- Parameters:
- enableMultipleMappings- If true the task will copy to all the mappings for a given source path, if false, only the first file or directory is processed. By default, this setting is false to provide backward compatibility with earlier releases.
- Since:
- Ant 1.6
 
 - 
isEnableMultipleMappingpublic boolean isEnableMultipleMapping() Get whether multiple mapping is enabled.- Returns:
- true if multiple mapping is enabled; false otherwise.
 
 - 
setFailOnErrorpublic void setFailOnError(boolean failonerror) Set whether to fail when errors are encountered. If false, note errors to the output but keep going. Default is true.- Parameters:
- failonerror- true or false.
 
 - 
addFilesetpublic void addFileset(FileSet set) Add a set of files to copy.- Parameters:
- set- a set of files to copy.
 
 - 
addpublic void add(ResourceCollection res) Add a collection of files to copy.- Parameters:
- res- a resource collection to copy.
- Since:
- Ant 1.7
 
 - 
createMapperpublic Mapper createMapper() throws BuildException Define the mapper to map source to destination files.- Returns:
- a mapper to be configured.
- Throws:
- BuildException- if more than one mapper is defined.
 
 - 
addpublic void add(FileNameMapper fileNameMapper) Add a nested filenamemapper.- Parameters:
- fileNameMapper- the mapper to add.
- Since:
- Ant 1.6.3
 
 - 
setEncodingpublic void setEncoding(java.lang.String encoding) Set the character encoding.- Parameters:
- encoding- the character encoding.
- Since:
- 1.32, Ant 1.5
 
 - 
getEncodingpublic java.lang.String getEncoding() Get the character encoding to be used.- Returns:
- the character encoding, nullif not set.
- Since:
- 1.32, Ant 1.5
 
 - 
setOutputEncodingpublic void setOutputEncoding(java.lang.String encoding) Set the character encoding for output files.- Parameters:
- encoding- the output character encoding.
- Since:
- Ant 1.6
 
 - 
getOutputEncodingpublic java.lang.String getOutputEncoding() Get the character encoding for output files.- Returns:
- the character encoding for output files,
 nullif not set.
- Since:
- Ant 1.6
 
 - 
setGranularitypublic void setGranularity(long granularity) Set the number of milliseconds leeway to give before deciding a target is out of date.Default is 1 second, or 2 seconds on DOS systems. - Parameters:
- granularity- the granularity used to decide if a target is out of date.
- Since:
- Ant 1.6.2
 
 - 
executepublic void execute() throws BuildExceptionPerform the copy operation.- Overrides:
- executein class- Task
- Throws:
- BuildException- if an error occurs.
 
 - 
validateAttributesprotected void validateAttributes() throws BuildExceptionEnsure we have a consistent and legal set of attributes, and set any internal flags necessary based on different combinations of attributes.- Throws:
- BuildException- if an error occurs.
 
 - 
scanprotected void scan(java.io.File fromDir, java.io.File toDir, java.lang.String[] files, java.lang.String[] dirs)Compares source files to destination files to see if they should be copied.- Parameters:
- fromDir- The source directory.
- toDir- The destination directory.
- files- A list of files to copy.
- dirs- A list of directories to copy.
 
 - 
scanprotected java.util.Map<Resource,java.lang.String[]> scan(Resource[] fromResources, java.io.File toDir) Compares source resources to destination files to see if they should be copied.- Parameters:
- fromResources- The source resources.
- toDir- The destination directory.
- Returns:
- a Map with the out-of-date resources as keys and an array of target file names as values.
- Since:
- Ant 1.7
 
 - 
buildMapprotected void buildMap(java.io.File fromDir, java.io.File toDir, java.lang.String[] names, FileNameMapper mapper, java.util.Hashtable<java.lang.String,java.lang.String[]> map)Add to a map of files/directories to copy.- Parameters:
- fromDir- the source directory.
- toDir- the destination directory.
- names- a list of filenames.
- mapper- a- FileNameMappervalue.
- map- a map of source file to array of destination files.
 
 - 
buildMapprotected java.util.Map<Resource,java.lang.String[]> buildMap(Resource[] fromResources, java.io.File toDir, FileNameMapper mapper) Create a map of resources to copy.- Parameters:
- fromResources- The source resources.
- toDir- the destination directory.
- mapper- a- FileNameMappervalue.
- Returns:
- a map of source resource to array of destination files.
- Since:
- Ant 1.7
 
 - 
doFileOperationsprotected void doFileOperations() Actually does the file (and possibly empty directory) copies. This is a good method for subclasses to override.
 - 
doResourceOperationsprotected void doResourceOperations(java.util.Map<Resource,java.lang.String[]> map) Actually does the resource copies. This is a good method for subclasses to override.- Parameters:
- map- a map of source resource to array of destination files.
- Since:
- Ant 1.7
 
 - 
supportsNonFileResourcesprotected boolean supportsNonFileResources() Whether this task can deal with non-file resources.<copy> can while <move> can't since we don't know how to remove non-file resources. This implementation returns true only if this task is <copy>. Any subclass of this class that also wants to support non-file resources needs to override this method. We need to do so for backwards compatibility reasons since we can't expect subclasses to support resources. - Returns:
- true if this task supports non file resources.
- Since:
- Ant 1.7
 
 
- 
 
-