## org.apache.tools.ant.taskdefs
Class Copy

    java.lang.Object
      org.apache.tools.ant.ProjectComponent
          org.apache.tools.ant.Task
              org.apache.tools.ant.taskdefs.Copy

Direct Known Subclasses:
  Move

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
Version:
  $Revision: 1.42.2.6 $
Author:
  Glenn McAllister glennm@ca.ibm.com, Stefan Bodewig, Michael McCallum, Magesh Umasankar

Field Summary
`protected java.util.Hashtable` `completeDirMap`
`protected java.io.File` `destDir`
`protected java.io.File` `destFile`
`protected java.util.Hashtable` `dirCopyMap`
`protected java.io.File` `file`
`protected java.util.Hashtable` `fileCopyMap`
`protected java.util.Vector` `filesets`
`protected boolean` `filtering`
`protected boolean` `flatten`
`protected boolean` `forceOverwrite`
`protected boolean` `includeEmpty`
`protected Mapper` `mapperElement`
`protected boolean` `preserveLastModified`
`protected int` `verbosity`

Fields inherited from class org.apache.tools.ant.Task
`description, location, target, taskName, taskType, wrapper`

Fields inherited from class org.apache.tools.ant.ProjectComponent
`project`

Constructor Summary
`Copy()` Copy task constructor.

Method Summary
`void` `addFileset(FileSet set)` Adds 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 map)`
`FilterChain` `createFilterChain()` Adds a FilterChain.
`FilterSet` `createFilterSet()` Adds a filterset.
`Mapper` `createMapper()` Defines the mapper to map source to destination files.
`protected void` `doFileOperations()` Actually does the file (and possibly empty directory) copies.
`void` `execute()` Performs the copy operation.
`java.lang.String` `getEncoding()`
`protected FileUtils` `getFileUtils()`
`protected java.util.Vector` `getFilterChains()` Get the filterchains being applied to this operation.
`protected java.util.Vector` `getFilterSets()` Get the filtersets being applied to this operation.
`boolean` `getPreserveLastModified()` Whether to give the copied files the same last modified time as the original files.
`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.
`void` `setEncoding(java.lang.String encoding)` Sets the character encoding
`void` `setFailOnError(boolean failonerror)` If false, note errors to the output but keep going.
`void` `setFile(java.io.File file)` Sets a single source file to copy.
`void` `setFiltering(boolean filtering)` If true, enables filtering.
`void` `setFlatten(boolean flatten)` When copying directory trees, the files can be "flattened" into a single directory.
`void` `setIncludeEmptyDirs(boolean includeEmpty)` Used to copy empty directories.
`void` `setOverwrite(boolean overwrite)` Overwrite any 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. setPreserveLastModified(String) has been deprecated and replaced with setPreserveLastModified(boolean) to consistently let the Introspection mechanism work.
`void` `setTodir(java.io.File destDir)` Sets the destination directory.
`void` `setTofile(java.io.File destFile)` Sets the destination file.
`void` `setVerbose(boolean verbose)` Used to force listing of all names of copied files.
`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
`getDescription, getLocation, getOwningTarget, getRuntimeConfigurableWrapper, getTaskName, handleErrorFlush, handleErrorOutput, handleFlush, handleOutput, init, isInvalid, log, log, maybeConfigure, perform, setDescription, setLocation, setOwningTarget, setRuntimeConfigurableWrapper, setTaskName`

Methods inherited from class org.apache.tools.ant.ProjectComponent
`getProject, setProject`

Methods inherited from class java.lang.Object
`clone, 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

### filesets

    protected java.util.Vector 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

### fileCopyMap

    protected java.util.Hashtable fileCopyMap

### dirCopyMap

    protected java.util.Hashtable dirCopyMap

### completeDirMap

    protected java.util.Hashtable completeDirMap

### mapperElement

    protected Mapper mapperElement

Constructor Detail

### Copy

    public Copy()

  Copy task constructor.

Method Detail

### getFileUtils

    protected FileUtils getFileUtils()

### setFile

    public void setFile(java.io.File file)

  Sets a single source file to copy.

### setTofile

    public void setTofile(java.io.File destFile)

  Sets the destination file.

### setTodir

    public void setTodir(java.io.File destDir)

  Sets the destination directory.

### createFilterChain

    public FilterChain createFilterChain()

  Adds a FilterChain.

### createFilterSet

    public FilterSet createFilterSet()

  Adds a filterset.

### setPreserveLastModified

    public void setPreserveLastModified(java.lang.String preserve)

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

### setPreserveLastModified

    public void setPreserveLastModified(boolean preserve)

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

### getPreserveLastModified

    public boolean getPreserveLastModified()

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

  Since:
    1.32, Ant 1.5

### getFilterSets

    protected java.util.Vector getFilterSets()

  Get the filtersets being applied to this operation.

  Returns:
    a vector of FilterSet objects

### getFilterChains

    protected java.util.Vector getFilterChains()

  Get the filterchains being applied to this operation.

  Returns:
    a vector of FilterChain objects

### setFiltering

    public void setFiltering(boolean filtering)

  If true, enables filtering.

### setOverwrite

    public void setOverwrite(boolean overwrite)

  Overwrite any existing destination file(s).

### setFlatten

    public void setFlatten(boolean flatten)

  When copying directory trees, the files can 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.

### setVerbose

    public void setVerbose(boolean verbose)

  Used to force listing of all names of copied files.

### setIncludeEmptyDirs

    public void setIncludeEmptyDirs(boolean includeEmpty)

  Used to copy empty directories.

### setFailOnError

    public void setFailOnError(boolean failonerror)

  If false, note errors to the output but keep going.

  Parameters:
    `failonerror` - true or false

### addFileset

    public void addFileset(FileSet set)

  Adds a set of files to copy.

### createMapper

    public Mapper createMapper()
                        throws BuildException

  Defines the mapper to map source to destination files.

  Throws:
    `BuildException`

### setEncoding

    public void setEncoding(java.lang.String encoding)

  Sets the character encoding

  Since:
    1.32, Ant 1.5

### getEncoding

    public java.lang.String getEncoding()

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

### execute

    public void execute()
                 throws BuildException

  Performs the copy operation.

  Overrides:
    `execute` in class `Task`
  Throws:
    `BuildException` - if something goes wrong with the build

### 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`

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

### buildMap

    protected void buildMap(java.io.File fromDir,
                            java.io.File toDir,
                            java.lang.String[] names,
                            FileNameMapper mapper,
                            java.util.Hashtable map)

### doFileOperations

    protected void doFileOperations()

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

Copyright © 2000-2003 Apache Software Foundation. All Rights Reserved.