org.apache.tools.ant.taskdefs
Class Copy

java.lang.Object
  extended by org.apache.tools.ant.ProjectComponent
      extended by org.apache.tools.ant.Task
          extended by 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 Summary
protected  java.util.Hashtable<java.io.File,java.io.File> completeDirMap
           
protected  java.io.File destDir
           
protected  java.io.File destFile
           
protected  java.util.Hashtable<java.lang.String,java.lang.String[]> dirCopyMap
           
protected  boolean failonerror
           
protected  java.io.File file
           
protected  java.util.Hashtable<java.lang.String,java.lang.String[]> fileCopyMap
           
protected  java.util.Vector<ResourceCollection> filesets
           
protected  FileUtils fileUtils
           
protected  boolean filtering
           
protected  boolean flatten
           
protected  boolean forceOverwrite
           
protected  boolean includeEmpty
           
protected  Mapper mapperElement
           
protected  boolean preserveLastModified
           
protected  java.util.Vector<ResourceCollection> rcs
           
protected  int verbosity
           
 
Fields inherited from class org.apache.tools.ant.Task
target, taskName, taskType, wrapper
 
Fields inherited from class org.apache.tools.ant.ProjectComponent
description, location, project
 
Constructor Summary
Copy()
          Copy task constructor.
 
Method Summary
 void add(FileNameMapper fileNameMapper)
          Add a nested filenamemapper.
 void add(ResourceCollection res)
          Add a collection of files to copy.
 void addFileset(FileSet set)
          Add a set of files to copy.
protected  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.
protected  java.util.Map<Resource,java.lang.String[]> buildMap(Resource[] fromResources, java.io.File toDir, FileNameMapper mapper)
          Create a map of resources to copy.
 FilterChain createFilterChain()
          Add a FilterChain.
 FilterSet createFilterSet()
          Add a filterset.
 Mapper createMapper()
          Define the mapper to map source to destination files.
protected  void doFileOperations()
          Actually does the file (and possibly empty directory) copies.
protected  void doResourceOperations(java.util.Map<Resource,java.lang.String[]> map)
          Actually does the resource copies.
 void execute()
          Perform the copy operation.
 java.lang.String getEncoding()
          Get the character encoding to be used.
protected  FileUtils getFileUtils()
          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.
 boolean getForce()
          Whether read-only destinations will be overwritten.
 java.lang.String getOutputEncoding()
          Get the character encoding for output files.
 boolean getPreserveLastModified()
          Get whether to give the copied files the same last modified time as the original files.
 boolean isEnableMultipleMapping()
          Get whether multiple mapping is enabled.
protected  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.
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.
 void setEnableMultipleMappings(boolean enableMultipleMappings)
          Set method of handling mappers that return multiple mappings for a given source path.
 void setEncoding(java.lang.String encoding)
          Set the character encoding.
 void setFailOnError(boolean failonerror)
          Set whether to fail when errors are encountered.
 void setFile(java.io.File file)
          Set a single source file to copy.
 void setFiltering(boolean filtering)
          Set filtering mode.
 void setFlatten(boolean flatten)
          Set whether files copied from directory trees will be "flattened" into a single directory.
 void setForce(boolean f)
          Whether read-only destinations will be overwritten.
 void setGranularity(long granularity)
          Set the number of milliseconds leeway to give before deciding a target is out of date.
 void setIncludeEmptyDirs(boolean includeEmpty)
          Set whether to copy empty directories.
 void setOutputEncoding(java.lang.String encoding)
          Set the character encoding for output files.
 void setOverwrite(boolean overwrite)
          Set overwrite mode regarding existing destination file(s).
 void setPreserveLastModified(boolean preserve)
          Give the copied files the same last modified time as the original files.
 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.
 void setQuiet(boolean quiet)
          Set quiet mode.
 void setTodir(java.io.File destDir)
          Set the destination directory.
 void setTofile(java.io.File destFile)
          Set the destination file.
 void setVerbose(boolean verbose)
          Set verbose mode.
protected  boolean supportsNonFileResources()
          Whether this task can deal with non-file resources.
protected  void validateAttributes()
          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.Task
bindToOwner, 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.ProjectComponent
clone, getDescription, getLocation, getProject, setDescription, setLocation, setProject
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

file

protected java.io.File file

destFile

protected java.io.File destFile

destDir

protected java.io.File destDir

rcs

protected java.util.Vector<ResourceCollection> rcs

filesets

protected java.util.Vector<ResourceCollection> filesets

filtering

protected boolean filtering

preserveLastModified

protected boolean preserveLastModified

forceOverwrite

protected boolean forceOverwrite

flatten

protected boolean flatten

verbosity

protected int verbosity

includeEmpty

protected boolean includeEmpty

failonerror

protected boolean failonerror

fileCopyMap

protected java.util.Hashtable<java.lang.String,java.lang.String[]> fileCopyMap

dirCopyMap

protected java.util.Hashtable<java.lang.String,java.lang.String[]> dirCopyMap

completeDirMap

protected java.util.Hashtable<java.io.File,java.io.File> completeDirMap

mapperElement

protected Mapper mapperElement

fileUtils

protected FileUtils fileUtils
Constructor Detail

Copy

public Copy()
Copy task constructor.

Method Detail

getFileUtils

protected FileUtils getFileUtils()
Get the FileUtils for this task.

Returns:
the fileutils object.

setFile

public void setFile(java.io.File file)
Set a single source file to copy.

Parameters:
file - the file to copy.

setTofile

public void setTofile(java.io.File destFile)
Set the destination file.

Parameters:
destFile - the file to copy to.

setTodir

public void setTodir(java.io.File destDir)
Set the destination directory.

Parameters:
destDir - the destination directory.

createFilterChain

public FilterChain createFilterChain()
Add a FilterChain.

Returns:
a filter chain object.

createFilterSet

public FilterSet createFilterSet()
Add a filterset.

Returns:
a filter set object.

setPreserveLastModified

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.

setPreserveLastModified

public 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.

getPreserveLastModified

public 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

getFilterSets

protected java.util.Vector<FilterSet> getFilterSets()
Get the filtersets being applied to this operation.

Returns:
a vector of FilterSet objects.

getFilterChains

protected java.util.Vector<FilterChain> getFilterChains()
Get the filterchains being applied to this operation.

Returns:
a vector of FilterChain objects.

setFiltering

public void setFiltering(boolean filtering)
Set filtering mode.

Parameters:
filtering - if true enable filtering; default is false.

setOverwrite

public 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.

setForce

public void setForce(boolean f)
Whether read-only destinations will be overwritten.

Defaults to false

Since:
Ant 1.8.2

getForce

public boolean getForce()
Whether read-only destinations will be overwritten.

Since:
Ant 1.8.2

setFlatten

public 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.

setVerbose

public 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.

setIncludeEmptyDirs

public void setIncludeEmptyDirs(boolean includeEmpty)
Set whether to copy empty directories.

Parameters:
includeEmpty - if true copy empty directories. Default is true.

setQuiet

public 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.

setEnableMultipleMappings

public 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

isEnableMultipleMapping

public boolean isEnableMultipleMapping()
Get whether multiple mapping is enabled.

Returns:
true if multiple mapping is enabled; false otherwise.

setFailOnError

public 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.

addFileset

public void addFileset(FileSet set)
Add a set of files to copy.

Parameters:
set - a set of files to copy.

add

public void add(ResourceCollection res)
Add a collection of files to copy.

Parameters:
res - a resource collection to copy.
Since:
Ant 1.7

createMapper

public 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.

add

public void add(FileNameMapper fileNameMapper)
Add a nested filenamemapper.

Parameters:
fileNameMapper - the mapper to add.
Since:
Ant 1.6.3

setEncoding

public void setEncoding(java.lang.String encoding)
Set the character encoding.

Parameters:
encoding - the character encoding.
Since:
1.32, Ant 1.5

getEncoding

public java.lang.String getEncoding()
Get the character encoding to be used.

Returns:
the character encoding, null if not set.
Since:
1.32, Ant 1.5

setOutputEncoding

public void setOutputEncoding(java.lang.String encoding)
Set the character encoding for output files.

Parameters:
encoding - the output character encoding.
Since:
Ant 1.6

getOutputEncoding

public java.lang.String getOutputEncoding()
Get the character encoding for output files.

Returns:
the character encoding for output files, null if not set.
Since:
Ant 1.6

setGranularity

public 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

execute

public void execute()
             throws BuildException
Perform the copy operation.

Overrides:
execute in class Task
Throws:
BuildException - if an error occurs.

validateAttributes

protected void validateAttributes()
                           throws BuildException
Ensure 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.

scan

protected 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.

scan

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.

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

buildMap

protected 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 FileNameMapper value.
map - a map of source file to array of destination files.

buildMap

protected 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 FileNameMapper value.
Returns:
a map of source resource to array of destination files.
Since:
Ant 1.7

doFileOperations

protected void doFileOperations()
Actually does the file (and possibly empty directory) copies. This is a good method for subclasses to override.


doResourceOperations

protected 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

supportsNonFileResources

protected 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