Move (Apache Ant API)
Description: declaration: package: org.apache.tools.ant.taskdefs, class: Move

# Class Move

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

All Implemented Interfaces:
  `Cloneable`

public class Move extends Copy
Moves a file or directory to a new file or directory. By default, the destination file is overwritten if it already exists. When overwrite is turned off, then files are only moved if the source file is newer than the destination file, or when the destination file does not exist.

Source files and directories are only deleted when the file or directory has been copied to the destination successfully. Filtering also works.

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

  ### Fields inherited from class Copy

  `completeDirMap, destDir, destFile, dirCopyMap, failonerror, file, fileCopyMap, filesets, fileUtils, filtering, flatten, forceOverwrite, includeEmpty, mapperElement, preserveLastModified, rcs, verbosity`
  Modifier and Type
  Field
  Description
  `protected Hashtable<File,File>`
  `completeDirMap`
  `protected File`
  `destDir`
  `protected File`
  `destFile`
  `protected Hashtable<String,String[]>`
  `dirCopyMap`
  `protected boolean`
  `failonerror`
  `protected File`
  `file`
  `protected Hashtable<String,String[]>`
  `fileCopyMap`
  `protected 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 Vector<ResourceCollection>`
  `rcs`
  `protected int`
  `verbosity`

  ### Fields inherited from class Task

  `target, taskName, taskType, wrapper`
  Modifier and Type
  Field
  Description
  `protected Target`
  `target`
  Deprecated.
  since 1.6.x.
  `protected String`
  `taskName`
  Deprecated.
  since 1.6.x.
  `protected String`
  `taskType`
  Deprecated.
  since 1.6.x.
  `protected RuntimeConfigurable`
  `wrapper`
  Deprecated.
  since 1.6.x.

  ### Fields inherited from class ProjectComponent

  `description, location, project`
  Modifier and Type
  Field
  Description
  `protected String`
  `description`
  Deprecated.
  since 1.6.x.
  `protected Location`
  `location`
  Deprecated.
  since 1.6.x.
  `protected Project`
  `project`
  Deprecated.
  since 1.6.x.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `Move()`
  Constructor of object.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `protected void`
  `deleteDir(File d)`
  Go and delete the directory tree.
  `protected void`
  `deleteDir(File d, boolean deleteFiles)`
  Go and delete the directory tree.
  `protected void`
  `doFileOperations()`
  Override copy's doFileOperations to move the files instead of copying them.
  `protected boolean`
  `okToDelete(File d)`
  Its only ok to delete a directory tree if there are no files in it.
  `protected boolean`
  `renameFile(File sourceFile, File destFile, boolean filtering, boolean overwrite)`
  Attempts to rename a file from a source to a destination.
  `void`
  `setPerformGcOnFailedDelete(boolean b)`
  Whether to perform a garbage collection before retrying a failed delete.
  `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 Copy

  `add, add, addFileset, buildMap, buildMap, createFilterChain, createFilterSet, createMapper, doResourceOperations, execute, getEncoding, getFileUtils, getFilterChains, getFilterSets, getForce, getOutputEncoding, getPreserveLastModified, isEnableMultipleMapping, scan, scan, setEnableMultipleMappings, setEncoding, setFailOnError, setFile, setFiltering, setFlatten, setForce, setGranularity, setIncludeEmptyDirs, setOutputEncoding, setOverwrite, setPreserveLastModified, setPreserveLastModified, setQuiet, setTodir, setTofile, setVerbose, supportsNonFileResources`
  Modifier and Type
  Method
  Description
  `void`
  `add(ResourceCollection res)`
  Add a collection of files to copy.
  `void`
  `add(FileNameMapper fileNameMapper)`
  Add a nested filenamemapper.
  `void`
  `addFileset(FileSet set)`
  Add a set of files to copy.
  `protected void`
  `buildMap(File fromDir, File toDir, String[] names, FileNameMapper mapper, Hashtable<String,String[]> map)`
  Add to a map of files/directories to copy.
  `protected Map<Resource,String[]>`
  `buildMap(Resource[] fromResources, 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`
  `doResourceOperations(Map<Resource,String[]> map)`
  Actually does the resource copies.
  `void`
  `execute()`
  Perform the copy operation.
  `String`
  `getEncoding()`
  Get the character encoding to be used.
  `protected FileUtils`
  `getFileUtils()`
  Get the FileUtils for this task.
  `protected Vector<FilterChain>`
  `getFilterChains()`
  Get the filterchains being applied to this operation.
  `protected Vector<FilterSet>`
  `getFilterSets()`
  Get the filtersets being applied to this operation.
  `boolean`
  `getForce()`
  Whether read-only destinations will be overwritten.
  `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(File fromDir, File toDir, String[] files, String[] dirs)`
  Compares source files to destination files to see if they should be copied.
  `protected Map<Resource,String[]>`
  `scan(Resource[] fromResources, 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(String encoding)`
  Set the character encoding.
  `void`
  `setFailOnError(boolean failonerror)`
  Set whether to fail when errors are encountered.
  `void`
  `setFile(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(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(String preserve)`
  Deprecated.
  since 1.5.x.
  `void`
  `setQuiet(boolean quiet)`
  Set quiet mode.
  `void`
  `setTodir(File destDir)`
  Set the destination directory.
  `void`
  `setTofile(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.

  ### Methods inherited from class 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`
  Modifier and Type
  Method
  Description
  `final void`
  `bindToOwner(Task owner)`
  Bind a task to another; use this when configuring a newly created task to do work on behalf of another.
  `Target`
  `getOwningTarget()`
  Returns the container target of this task.
  `RuntimeConfigurable`
  `getRuntimeConfigurableWrapper()`
  Returns the wrapper used for runtime configuration.
  `String`
  `getTaskName()`
  Returns the name to use in logging messages.
  `String`
  `getTaskType()`
  Return the type of task.
  `protected RuntimeConfigurable`
  `getWrapper()`
  Return the runtime configurable structure for this task.
  `protected void`
  `handleErrorFlush(String output)`
  Handles an error line by logging it with the WARN priority.
  `protected void`
  `handleErrorOutput(String output)`
  Handles an error output by logging it with the WARN priority.
  `protected void`
  `handleFlush(String output)`
  Handles output by logging it with the INFO priority.
  `protected int`
  `handleInput(byte[] buffer, int offset, int length)`
  Handle an input request by this task.
  `protected void`
  `handleOutput(String output)`
  Handles output by logging it with the INFO priority.
  `void`
  `init()`
  Called by the project to let the task initialize properly.
  `protected final boolean`
  `isInvalid()`
  Has this task been marked invalid?
  `void`
  `log(String msg)`
  Logs a message with the default (INFO) priority.
  `void`
  `log(String msg, int msgLevel)`
  Logs a message with the given priority.
  `void`
  `log(String msg, Throwable t, int msgLevel)`
  Logs a message with the given priority.
  `void`
  `log(Throwable t, int msgLevel)`
  Logs a message with the given priority.
  `void`
  `maybeConfigure()`
  Configures this task - if it hasn't been done already.
  `final void`
  `perform()`
  Performs this task if it's still valid, or gets a replacement version and performs that otherwise.
  `void`
  `reconfigure()`
  Force the task to be reconfigured from its RuntimeConfigurable.
  `void`
  `setOwningTarget(Target target)`
  Sets the target container of this task.
  `void`
  `setRuntimeConfigurableWrapper(RuntimeConfigurable wrapper)`
  Sets the wrapper to be used for runtime configuration.
  `void`
  `setTaskName(String name)`
  Sets the name to use in logging messages.
  `void`
  `setTaskType(String type)`
  Sets the name with which the task has been invoked.

  ### Methods inherited from class ProjectComponent

  `clone, getDescription, getLocation, getProject, setDescription, setLocation, setProject`
  Modifier and Type
  Method
  Description
  `Object`
  `clone()`
  `String`
  `getDescription()`
  Returns the description of the current action.
  `Location`
  `getLocation()`
  Returns the file/location where this task was defined.
  `Project`
  `getProject()`
  Returns the project to which this component belongs.
  `void`
  `setDescription(String desc)`
  Sets a description of the current action.
  `void`
  `setLocation(Location location)`
  Sets the file/location where this task was defined.
  `void`
  `setProject(Project project)`
  Sets the project object of this component.

  ### Methods inherited from class Object

  `equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Constructor Details

  - ### Move

    public Move()
    Constructor of object. This sets the forceOverwrite attribute of the Copy parent class to true.

- ## Method Details

  - ### setPerformGcOnFailedDelete

    public void setPerformGcOnFailedDelete(boolean b)
    Whether to perform a garbage collection before retrying a failed delete.

    This may be required on Windows (where it is set to true by default) but also on other operating systems, for example when deleting directories from an NFS share.

    Parameters:
      `b` - boolean
    Since:
      Ant 1.8.3

  - ### 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..
    Overrides:
      `validateAttributes` in class `Copy`
    Throws:
      `BuildException` - if an error occurs.

  - ### doFileOperations

    protected void doFileOperations()
    Override copy's doFileOperations to move the files instead of copying them.
    Overrides:
      `doFileOperations` in class `Copy`

  - ### okToDelete

    protected boolean okToDelete(File d)
    Its only ok to delete a directory tree if there are no files in it.
    Parameters:
      `d` - the directory to check
    Returns:
      true if a deletion can go ahead

  - ### deleteDir

    protected void deleteDir(File d)
    Go and delete the directory tree.
    Parameters:
      `d` - the directory to delete

  - ### deleteDir

    protected void deleteDir(File d, boolean deleteFiles)
    Go and delete the directory tree.
    Parameters:
      `d` - the directory to delete
      `deleteFiles` - whether to delete files

  - ### renameFile

    protected boolean renameFile(File sourceFile, File destFile, boolean filtering, boolean overwrite) throws IOException, BuildException
    Attempts to rename a file from a source to a destination. If overwrite is set to true, this method overwrites existing file even if the destination file is newer. Otherwise, the source file is renamed only if the destination file is older than it. Method then checks if token filtering is used. If it is, this method returns false assuming it is the responsibility to the copyFile method.
    Parameters:
      `sourceFile` - the file to rename
      `destFile` - the destination file
      `filtering` - if true, filtering is in operation, file will be copied/deleted instead of renamed
      `overwrite` - if true force overwrite even if destination file is newer than source file
    Returns:
      true if the file was renamed
    Throws:
      `IOException` - if an error occurs
      `BuildException` - if an error occurs