FileDocCategorySizeDatePackage
CopyPath.javaAPI DocApache Ant 1.706482Wed Dec 13 06:16:22 GMT 2006org.apache.tools.ant.taskdefs

CopyPath

public class CopyPath extends org.apache.tools.ant.Task
Copy the contents of a path to a destination, using the mapper of choice
since
Ant 1.7
ant.task
category="filesystem"

Fields Summary
public static final String
ERROR_NO_DESTDIR
No destdir attribute
public static final String
ERROR_NO_PATH
No path
public static final String
ERROR_NO_MAPPER
No mapper
private static final org.apache.tools.ant.util.FileUtils
FILE_UTILS
private org.apache.tools.ant.util.FileNameMapper
mapper
private org.apache.tools.ant.types.Path
path
private File
destDir
private long
granularity
private boolean
preserveLastModified
Constructors Summary
Methods Summary
public voidadd(org.apache.tools.ant.util.FileNameMapper newmapper)
add a mapper

param
newmapper the mapper to add.

        if (mapper != null) {
            throw new BuildException("Only one mapper allowed");
        }
        mapper = newmapper;
    
public org.apache.tools.ant.types.PathcreatePath()
Create a path.

return
a path to be configured.

        if (path == null) {
            path = new Path(getProject());
        }
        return path;
    
public voidexecute()
This is a very minimal derivative of the nomal copy logic.

throws
BuildException if something goes wrong with the build.

        validateAttributes();
        String[] sourceFiles = path.list();
        if (sourceFiles.length == 0) {
            log("Path is empty", Project.MSG_VERBOSE);
            return;
        }

        for (int sources = 0; sources < sourceFiles.length; sources++) {

            String sourceFileName = sourceFiles[sources];
            File sourceFile = new File(sourceFileName);
            String[] toFiles = (String[]) mapper.mapFileName(sourceFileName);

            for (int i = 0; i < toFiles.length; i++) {
                String destFileName = toFiles[i];
                File destFile = new File(destDir, destFileName);

                if (sourceFile.equals(destFile)) {
                    log("Skipping self-copy of " + sourceFileName, Project.MSG_VERBOSE);
                    continue;
                }
                if (sourceFile.isDirectory()) {
                    log("Skipping directory " + sourceFileName);
                    continue;
                }
                try {
                    log("Copying " + sourceFile + " to " + destFile, Project.MSG_VERBOSE);

                    FILE_UTILS.copyFile(sourceFile, destFile, null, null, false,
                            preserveLastModified, null, null, getProject());
                } catch (IOException ioe) {
                    String msg = "Failed to copy " + sourceFile + " to " + destFile + " due to "
                            + ioe.getMessage();
                    if (destFile.exists() && !destFile.delete()) {
                        msg += " and I couldn't delete the corrupt " + destFile;
                    }
                    throw new BuildException(msg, ioe, getLocation());
                }
            }
        }
    
public voidsetDestDir(java.io.File destDir)
The dest dir attribute.

param
destDir the value of the destdir attribute.


                     
        
        this.destDir = destDir;
    
public voidsetGranularity(long granularity)
Set the number of milliseconds leeway to give before deciding a target is out of date. TODO: This is not yet used.

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

        this.granularity = granularity;
    
public voidsetPath(org.apache.tools.ant.types.Path s)
Set the path to be used when running the Java class.

param
s an Ant Path object containing the path.

        createPath().append(s);
    
public voidsetPathRef(org.apache.tools.ant.types.Reference r)
Set the path to use by reference.

param
r a reference to an existing path.

        createPath().setRefid(r);
    
public voidsetPreserveLastModified(boolean preserveLastModified)
Give the copied files the same last modified time as the original files.

param
preserveLastModified if true preserve the modified time; default is false.

        this.preserveLastModified = preserveLastModified;
    
protected voidvalidateAttributes()
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.

        if (destDir == null) {
            throw new BuildException(ERROR_NO_DESTDIR);
        }
        if (mapper == null) {
            throw new BuildException(ERROR_NO_MAPPER);
        }
        if (path == null) {
            throw new BuildException(ERROR_NO_PATH);
        }