Top Description Fields Constructors Methods
org.apache.tools.ant.taskdefs

public Class Copy

extends Task
Class Inheritance
Known Direct Subclasses
org.apache.tools.ant.taskdefs.Move, org.apache.tools.ant.taskdefs.Sync.MyCopy
Imports
java.io.File, .IOException, java.util.ArrayList, .Arrays, .HashMap, .HashSet, .Hashtable, .List, .Map, .Set, .Vector, org.apache.tools.ant.BuildException, .DirectoryScanner, .Project, .Task, org.apache.tools.ant.types.FileSet, .FilterChain, .FilterSet, .FilterSetCollection, .Mapper, .Resource, .ResourceCollection, org.apache.tools.ant.types.resources.FileProvider, .FileResource, org.apache.tools.ant.util.FileNameMapper, .FileUtils, .FlatFileNameMapper, .IdentityMapper, .LinkedHashtable, .ResourceUtils, .SourceFileScanner, .StringUtils

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

Modifier and TypeField and Description
protected Hashtable<File, File>
protected File
protected File
protected Hashtable<String, String[]>
private boolean
protected boolean
protected File
protected Hashtable<String, String[]>
protected Vector<ResourceCollection>
protected FileUtils
private final Vector<FilterChain>
protected boolean
private final Vector<FilterSet>
protected boolean
private boolean
protected boolean
private long
protected boolean
private String
pack-priv static final String
LINE_SEPARATOR

Deprecated
protected Mapper
private static final String
pack-priv static final File
private String
protected boolean
private boolean
protected Vector<ResourceCollection>
private Resource
protected int
Inherited from org.apache.tools.ant.Task:
targettaskNametaskTypewrapper

Constructor Summary

AccessConstructor and Description
public
Copy()

Copy task constructor.

Method Summary

Modifier and TypeMethod and Description
public void
add(final ResourceCollection
a resource collection to copy.
res
)

Add a collection of files to copy.

public void
add(final FileNameMapper
the mapper to add.
fileNameMapper
)

Add a nested filenamemapper.

private static void
add(File baseDir, final String[] names, final Map<File, List<String>> m)

Adds the given strings to a list contained in the given map.

private static void
add(final File baseDir, final String name, final Map<File, List<String>> m)

Adds the given string to a list contained in the given map.

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

Add a set of files to copy.

protected void
buildMap(final File
the source directory.
fromDir
,
final File
the destination directory.
toDir
,
final String[]
a list of filenames.
names
,
final FileNameMapper
a FileNameMapper value.
mapper
,
final Hashtable<String, String[]>
a map of source file to array of destination files.
map
)

Add to a map of files/directories to copy.

protected Map<Resource, String[]>

Returns:

a map of source resource to array of destination files.
buildMap
(final Resource[]
The source resources.
fromResources
,
final File
the destination directory.
toDir
,
final FileNameMapper
a FileNameMapper value.
mapper
)

Create a map of resources to copy.

private void
copySingleFile()

protected and private methods

public FilterChain

Returns:

a filter chain object.
createFilterChain
()

Add a FilterChain.

public FilterSet

Returns:

a filter set object.
createFilterSet
()

Add a filterset.

public Mapper

Returns:

a mapper to be configured.
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(final Map<Resource, String[]>
a map of source resource to array of destination files.
map
)

Actually does the resource copies.

public void
execute()

Overrides org.apache.tools.ant.Task.execute.

Perform the copy operation.
private String
getDueTo(final Exception ex)

Returns a reason for failure based on the exception thrown.

public String

Returns:

the character encoding, null if not set.
getEncoding
()

Get the character encoding to be used.

protected FileUtils

Returns:

the fileutils object.
getFileUtils
()

Get the FileUtils for this task.

protected Vector<FilterChain>

Returns:

a vector of FilterChain objects.
getFilterChains
()

Get the filterchains being applied to this operation.

protected Vector<FilterSet>

Returns:

a vector of FilterSet objects.
getFilterSets
()

Get the filtersets being applied to this operation.

public boolean

Returns:

boolean
getForce
()

Whether read-only destinations will be overwritten.

private static File
getKeyFile(final File f)

Either returns its argument or a placeholder if the argument is null.

private FileNameMapper
getMapper()

returns the mapper to use based on nested elements or the flatten attribute.

private String

Returns:

ex.getMessage() if ex.getMessage() is not null otherwise return ex.toString()
getMessage
(final Exception
the exception to handle
ex
)

Handle getMessage() for exceptions.

public String

Returns:

the character encoding for output files, null if not set.
getOutputEncoding
()

Get the character encoding for output files.

public boolean

Returns:

the whether destination files will inherit the modification times of the corresponding source files.
getPreserveLastModified
()

Get whether to give the copied files the same last modified time as the original files.

public boolean

Returns:

true if multiple mapping is enabled; false otherwise.
isEnableMultipleMapping
()

Get whether multiple mapping is enabled.

private void
iterateOverBaseDirs(final Set<File> baseDirs, final Map<File, List<String>> dirsByBasedir, final Map<File, List<String>> filesByBasedir)

protected void
scan(final File
The source directory.
fromDir
,
final File
The destination directory.
toDir
,
final String[]
A list of files to copy.
files
,
final String[]
A list of directories to copy.
dirs
)

Compares source files to destination files to see if they should be copied.

protected Map<Resource, String[]>

Returns:

a Map with the out-of-date resources as keys and an array of target file names as values.
scan
(final Resource[]
The source resources.
fromResources
,
final File
The destination directory.
toDir
)

Compares source resources to destination files to see if they should be copied.

public void
setEnableMultipleMappings(final boolean
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.
enableMultipleMappings
)

Set method of handling mappers that return multiple mappings for a given source path.

public void
setEncoding(final String
the character encoding.
encoding
)

Set the character encoding.

public void
setFailOnError(final boolean
true or false.
failonerror
)

Set whether to fail when errors are encountered.

public void
setFile(final File
the file to copy.
file
)

Set a single source file to copy.

public void
setFiltering(final boolean
if true enable filtering; default is false.
filtering
)

Set filtering mode.

public void
setFlatten(final boolean
if true flatten the destination directory. Default is false.
flatten
)

Set whether files copied from directory trees will be "flattened" into a single directory.

public void
setForce(final boolean
boolean
f
)

Whether read-only destinations will be overwritten.

public void
setGranularity(final long
the granularity used to decide if a target is out of date.
granularity
)

Set the number of milliseconds leeway to give before deciding a target is out of date.

public void
setIncludeEmptyDirs(final boolean
if true copy empty directories. Default is true.
includeEmpty
)

Set whether to copy empty directories.

public void
setOutputEncoding(final String
the output character encoding.
encoding
)

Set the character encoding for output files.

public void
setOverwrite(final boolean
if true force overwriting of destination file(s) even if the destination file(s) are younger than the corresponding source file. Default is false.
overwrite
)

Set overwrite mode regarding existing destination file(s).

public void
setPreserveLastModified(final String
a boolean 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.
public void
setPreserveLastModified(final boolean
if true preserve the modified time; default is false.
preserve
)

Give the copied files the same last modified time as the original files.

public void
setQuiet(final boolean
whether or not to display error messages when a file or directory does not exist. Default is false.
quiet
)

Set quiet mode.

public void
setTodir(final File
the destination directory.
destDir
)

Set the destination directory.

public void
setTofile(final File
the file to copy to.
destFile
)

Set the destination file.

public void
setVerbose(final boolean
whether to output the names of copied files. Default is false.
verbose
)

Set verbose mode.

protected boolean

Returns:

true if this task supports non file resources.
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.

Inherited from org.apache.tools.ant.Task:
bindToOwnergetOwningTargetgetRuntimeConfigurableWrappergetTaskNamegetTaskTypegetWrapperhandleErrorFlushhandleErrorOutputhandleFlushhandleInputhandleOutputinitisInvalidloglogloglogmaybeConfigureperformreconfiguresetOwningTargetsetRuntimeConfigurableWrappersetTaskNamesetTaskType

Field Detail

completeDirMapback to summary
protected Hashtable<File, File> completeDirMap
destDirback to summary
protected File destDir
destFileback to summary
protected File destFile
dirCopyMapback to summary
protected Hashtable<String, String[]> dirCopyMap
enableMultipleMappingsback to summary
private boolean enableMultipleMappings
failonerrorback to summary
protected boolean failonerror
fileback to summary
protected File file
fileCopyMapback to summary
protected Hashtable<String, String[]> fileCopyMap
filesetsback to summary
protected Vector<ResourceCollection> filesets
fileUtilsback to summary
protected FileUtils fileUtils
filterChainsback to summary
private final Vector<FilterChain> filterChains
filteringback to summary
protected boolean filtering
filterSetsback to summary
private final Vector<FilterSet> filterSets
flattenback to summary
protected boolean flatten
forceback to summary
private boolean force
forceOverwriteback to summary
protected boolean forceOverwrite
granularityback to summary
private long granularity
includeEmptyback to summary
protected boolean includeEmpty
inputEncodingback to summary
private String inputEncoding
LINE_SEPARATORback to summary
pack-priv static final String LINE_SEPARATOR

Deprecated

Annotations
@Deprecated
mapperElementback to summary
protected Mapper mapperElement
MSG_WHEN_COPYING_EMPTY_RC_TO_FILEback to summary
private static final String MSG_WHEN_COPYING_EMPTY_RC_TO_FILE
NULL_FILE_PLACEHOLDERback to summary
pack-priv static final File NULL_FILE_PLACEHOLDER
outputEncodingback to summary
private String outputEncoding
preserveLastModifiedback to summary
protected boolean preserveLastModified
quietback to summary
private boolean quiet
rcsback to summary
protected Vector<ResourceCollection> rcs
singleResourceback to summary
private Resource singleResource
verbosityback to summary
protected int verbosity

Constructor Detail

Copyback to summary
public Copy()

Copy task constructor.

Method Detail

addback to summary
public void add(final ResourceCollection res)

Add a collection of files to copy.

Parameters
res:ResourceCollection

a resource collection to copy.

Since
Ant 1.7
addback to summary
public void add(final FileNameMapper fileNameMapper)

Add a nested filenamemapper.

Parameters
fileNameMapper:FileNameMapper

the mapper to add.

Since
Ant 1.6.3
addback to summary
private static void add(File baseDir, final String[] names, final Map<File, List<String>> m)

Adds the given strings to a list contained in the given map. The file is the key into the map.

addback to summary
private static void add(final File baseDir, final String name, final Map<File, List<String>> m)

Adds the given string to a list contained in the given map. The file is the key into the map.

addFilesetback to summary
public void addFileset(final FileSet set)

Add a set of files to copy.

Parameters
set:FileSet

a set of files to copy.

buildMapback to summary
protected void buildMap(final File fromDir, final File toDir, final String[] names, final FileNameMapper mapper, final Hashtable<String, String[]> map)

Add to a map of files/directories to copy.

Parameters
fromDir:File

the source directory.

toDir:File

the destination directory.

names:String[]

a list of filenames.

mapper:FileNameMapper

a FileNameMapper value.

map:Hashtable<String, String[]>

a map of source file to array of destination files.

buildMapback to summary
protected Map<Resource, String[]> buildMap(final Resource[] fromResources, final File toDir, final FileNameMapper mapper)

Create a map of resources to copy.

Parameters
fromResources:Resource[]

The source resources.

toDir:File

the destination directory.

mapper:FileNameMapper

a FileNameMapper value.

Returns:Map<Resource, String[]>

a map of source resource to array of destination files.

Since
Ant 1.7
copySingleFileback to summary
private void copySingleFile()

protected and private methods

createFilterChainback to summary
public FilterChain createFilterChain()

Add a FilterChain.

Returns:FilterChain

a filter chain object.

createFilterSetback to summary
public FilterSet createFilterSet()

Add a filterset.

Returns:FilterSet

a filter set object.

createMapperback to summary
public Mapper createMapper() throws BuildException

Define the mapper to map source to destination files.

Returns:Mapper

a mapper to be configured.

Exceptions
BuildException:
if more than one mapper is defined.
doFileOperationsback to summary
protected void doFileOperations()

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

doResourceOperationsback to summary
protected void doResourceOperations(final Map<Resource, String[]> map)

Actually does the resource copies. This is a good method for subclasses to override.

Parameters
map:Map<Resource, String[]>

a map of source resource to array of destination files.

Since
Ant 1.7
executeback to summary
public void execute() throws BuildException

Overrides org.apache.tools.ant.Task.execute.

Perform the copy operation.

Annotations
@Override
Exceptions
BuildException:
if an error occurs.
getDueToback to summary
private String getDueTo(final Exception ex)

Returns a reason for failure based on the exception thrown. If the exception is not IOException output the class name, output the message if the exception is MalformedInput add a little note.

getEncodingback to summary
public String getEncoding()

Get the character encoding to be used.

Returns:String

the character encoding, null if not set.

Since
1.32, Ant 1.5
getFileUtilsback to summary
protected FileUtils getFileUtils()

Get the FileUtils for this task.

Returns:FileUtils

the fileutils object.

getFilterChainsback to summary
protected Vector<FilterChain> getFilterChains()

Get the filterchains being applied to this operation.

Returns:Vector<FilterChain>

a vector of FilterChain objects.

getFilterSetsback to summary
protected Vector<FilterSet> getFilterSets()

Get the filtersets being applied to this operation.

Returns:Vector<FilterSet>

a vector of FilterSet objects.

getForceback to summary
public boolean getForce()

Whether read-only destinations will be overwritten.

Returns:boolean

boolean

Since
Ant 1.8.2
getKeyFileback to summary
private static File getKeyFile(final File f)

Either returns its argument or a placeholder if the argument is null.

getMapperback to summary
private FileNameMapper getMapper()

returns the mapper to use based on nested elements or the flatten attribute.

getMessageback to summary
private String getMessage(final Exception ex)

Handle getMessage() for exceptions.

Parameters
ex:Exception

the exception to handle

Returns:String

ex.getMessage() if ex.getMessage() is not null otherwise return ex.toString()

getOutputEncodingback to summary
public String getOutputEncoding()

Get the character encoding for output files.

Returns:String

the character encoding for output files, null if not set.

Since
Ant 1.6
getPreserveLastModifiedback to summary
public boolean getPreserveLastModified()

Get whether to give the copied files the same last modified time as the original files.

Returns:boolean

the whether destination files will inherit the modification times of the corresponding source files.

Since
1.32, Ant 1.5
isEnableMultipleMappingback to summary
public boolean isEnableMultipleMapping()

Get whether multiple mapping is enabled.

Returns:boolean

true if multiple mapping is enabled; false otherwise.

iterateOverBaseDirsback to summary
private void iterateOverBaseDirs(final Set<File> baseDirs, final Map<File, List<String>> dirsByBasedir, final Map<File, List<String>> filesByBasedir)
scanback to summary
protected void scan(final File fromDir, final File toDir, final String[] files, final String[] dirs)

Compares source files to destination files to see if they should be copied.

Parameters
fromDir:File

The source directory.

toDir:File

The destination directory.

files:String[]

A list of files to copy.

dirs:String[]

A list of directories to copy.

scanback to summary
protected Map<Resource, String[]> scan(final Resource[] fromResources, final File toDir)

Compares source resources to destination files to see if they should be copied.

Parameters
fromResources:Resource[]

The source resources.

toDir:File

The destination directory.

Returns:Map<Resource, String[]>

a Map with the out-of-date resources as keys and an array of target file names as values.

Since
Ant 1.7
setEnableMultipleMappingsback to summary
public void setEnableMultipleMappings(final boolean enableMultipleMappings)

Set method of handling mappers that return multiple mappings for a given source path.

Parameters
enableMultipleMappings:boolean

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
setEncodingback to summary
public void setEncoding(final String encoding)

Set the character encoding.

Parameters
encoding:String

the character encoding.

Since
1.32, Ant 1.5
setFailOnErrorback to summary
public void setFailOnError(final 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:boolean

true or false.

setFileback to summary
public void setFile(final File file)

Set a single source file to copy.

Parameters
file:File

the file to copy.

setFilteringback to summary
public void setFiltering(final boolean filtering)

Set filtering mode.

Parameters
filtering:boolean

if true enable filtering; default is false.

setFlattenback to summary
public void setFlatten(final 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:boolean

if true flatten the destination directory. Default is false.

setForceback to summary
public void setForce(final boolean f)

Whether read-only destinations will be overwritten.

Defaults to false

Parameters
f:boolean

boolean

Since
Ant 1.8.2
setGranularityback to summary
public void setGranularity(final 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:long

the granularity used to decide if a target is out of date.

Since
Ant 1.6.2
setIncludeEmptyDirsback to summary
public void setIncludeEmptyDirs(final boolean includeEmpty)

Set whether to copy empty directories.

Parameters
includeEmpty:boolean

if true copy empty directories. Default is true.

setOutputEncodingback to summary
public void setOutputEncoding(final String encoding)

Set the character encoding for output files.

Parameters
encoding:String

the output character encoding.

Since
Ant 1.6
setOverwriteback to summary
public void setOverwrite(final boolean overwrite)

Set overwrite mode regarding existing destination file(s).

Parameters
overwrite:boolean

if true force overwriting of destination file(s) even if the destination file(s) are younger than the corresponding source file. Default is false.

setPreserveLastModifiedback to summary
public void setPreserveLastModified(final 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:String

a boolean string.

Annotations
@Deprecated
setPreserveLastModifiedback to summary
public void setPreserveLastModified(final boolean preserve)

Give the copied files the same last modified time as the original files.

Parameters
preserve:boolean

if true preserve the modified time; default is false.

setQuietback to summary
public void setQuiet(final boolean quiet)

Set quiet mode. Used to hide messages when a file or directory to be copied does not exist.

Parameters
quiet:boolean

whether or not to display error messages when a file or directory does not exist. Default is false.

setTodirback to summary
public void setTodir(final File destDir)

Set the destination directory.

Parameters
destDir:File

the destination directory.

setTofileback to summary
public void setTofile(final File destFile)

Set the destination file.

Parameters
destFile:File

the file to copy to.

setVerboseback to summary
public void setVerbose(final boolean verbose)

Set verbose mode. Used to force listing of all names of copied files.

Parameters
verbose:boolean

whether to output the names of copied files. Default is false.

supportsNonFileResourcesback to summary
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:boolean

true if this task supports non file resources.

Since
Ant 1.7
validateAttributesback to summary
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.

Exceptions
BuildException:
if an error occurs.