Methods Summary |
---|
public void | add(org.apache.tools.ant.types.ResourceCollection rc)Adds a collection of filesystem resources to copy.
myCopy.add(rc);
|
public void | addFileset(org.apache.tools.ant.types.FileSet set)Adds a set of files to copy.
add(set);
|
public void | addPreserveInTarget(org.apache.tools.ant.taskdefs.Sync$SyncTarget s)A container for patterns and selectors that can be used to
specify files that should be kept in the target even if they
are not present in any source directory.
You must not invoke this method more than once.
if (syncTarget != null) {
throw new BuildException("you must not specify multiple "
+ "preserveintarget elements.");
}
syncTarget = s;
|
private static void | assertTrue(java.lang.String message, boolean condition)Pseudo-assert method.
if (!condition) {
throw new BuildException("Assertion Error: " + message);
}
|
private void | configureTask(org.apache.tools.ant.Task helper)
helper.setProject(getProject());
helper.setTaskName(getTaskName());
helper.setOwningTarget(getOwningTarget());
helper.init();
|
public void | execute()Execute the sync task.
// The destination of the files to copy
File toDir = myCopy.getToDir();
// The complete list of files to copy
Set allFiles = myCopy.nonOrphans;
// If the destination directory didn't already exist,
// or was empty, then no previous file removal is necessary!
boolean noRemovalNecessary = !toDir.exists() || toDir.list().length < 1;
// Copy all the necessary out-of-date files
log("PASS#1: Copying files to " + toDir, Project.MSG_DEBUG);
myCopy.execute();
// Do we need to perform further processing?
if (noRemovalNecessary) {
log("NO removing necessary in " + toDir, Project.MSG_DEBUG);
return; // nope ;-)
}
// Get rid of all files not listed in the source filesets.
log("PASS#2: Removing orphan files from " + toDir, Project.MSG_DEBUG);
int[] removedFileCount = removeOrphanFiles(allFiles, toDir);
logRemovedCount(removedFileCount[0], "dangling director", "y", "ies");
logRemovedCount(removedFileCount[1], "dangling file", "", "s");
// Get rid of empty directories on the destination side
if (!myCopy.getIncludeEmptyDirs()) {
log("PASS#3: Removing empty directories from " + toDir,
Project.MSG_DEBUG);
int removedDirCount = removeEmptyDirectories(toDir, false);
logRemovedCount(removedDirCount, "empty director", "y", "ies");
}
|
public void | init()Initialize the sync task.
// Instantiate it
myCopy = new MyCopy();
configureTask(myCopy);
// Default config of <mycopy> for our purposes.
myCopy.setFiltering(false);
myCopy.setIncludeEmptyDirs(false);
myCopy.setPreserveLastModified(true);
|
private void | logRemovedCount(int count, java.lang.String prefix, java.lang.String singularSuffix, java.lang.String pluralSuffix)
File toDir = myCopy.getToDir();
String what = (prefix == null) ? "" : prefix;
what += (count < 2) ? singularSuffix : pluralSuffix;
if (count > 0) {
log("Removed " + count + " " + what + " from " + toDir,
Project.MSG_INFO);
} else {
log("NO " + what + " to remove from " + toDir,
Project.MSG_VERBOSE);
}
|
private int | removeEmptyDirectories(java.io.File dir, boolean removeIfEmpty)Removes all empty directories from a directory.
Note that a directory that contains only empty
directories, directly or not, will be removed!
Recurses depth-first to find the leaf directories
which are empty and removes them, then unwinds the
recursion stack, removing directories which have
become empty themselves, etc...
int removedCount = 0;
if (dir.isDirectory()) {
File[] children = dir.listFiles();
for (int i = 0; i < children.length; ++i) {
File file = children[i];
// Test here again to avoid method call for non-directories!
if (file.isDirectory()) {
removedCount += removeEmptyDirectories(file, true);
}
}
if (children.length > 0) {
// This directory may have become empty...
// We need to re-query its children list!
children = dir.listFiles();
}
if (children.length < 1 && removeIfEmpty) {
log("Removing empty directory: " + dir, Project.MSG_DEBUG);
dir.delete();
++removedCount;
}
}
return removedCount;
|
private int[] | removeOrphanFiles(java.util.Set nonOrphans, java.io.File toDir)Removes all files and folders not found as keys of a table
(used as a set!).
If the provided file is a directory, it is recursively
scanned for orphaned files which will be removed as well.
If the directory is an orphan, it will also be removed.
int[] removedCount = new int[] {0, 0};
String[] excls =
(String[]) nonOrphans.toArray(new String[nonOrphans.size() + 1]);
// want to keep toDir itself
excls[nonOrphans.size()] = "";
DirectoryScanner ds = null;
if (syncTarget != null) {
FileSet fs = new FileSet();
fs.setDir(toDir);
fs.setCaseSensitive(syncTarget.isCaseSensitive());
fs.setFollowSymlinks(syncTarget.isFollowSymlinks());
// preserveInTarget would find all files we want to keep,
// but we need to find all that we want to delete - so the
// meaning of all patterns and selectors must be inverted
PatternSet ps = syncTarget.mergePatterns(getProject());
fs.appendExcludes(ps.getIncludePatterns(getProject()));
fs.appendIncludes(ps.getExcludePatterns(getProject()));
fs.setDefaultexcludes(!syncTarget.getDefaultexcludes());
// selectors are implicitly ANDed in DirectoryScanner. To
// revert their logic we wrap them into a <none> selector
// instead.
FileSelector[] s = syncTarget.getSelectors(getProject());
if (s.length > 0) {
NoneSelector ns = new NoneSelector();
for (int i = 0; i < s.length; i++) {
ns.appendSelector(s[i]);
}
fs.appendSelector(ns);
}
ds = fs.getDirectoryScanner(getProject());
} else {
ds = new DirectoryScanner();
ds.setBasedir(toDir);
}
ds.addExcludes(excls);
ds.scan();
String[] files = ds.getIncludedFiles();
for (int i = 0; i < files.length; i++) {
File f = new File(toDir, files[i]);
log("Removing orphan file: " + f, Project.MSG_DEBUG);
f.delete();
++removedCount[1];
}
String[] dirs = ds.getIncludedDirectories();
// ds returns the directories in lexicographic order.
// iterating through the array backwards means we are deleting
// leaves before their parent nodes - thus making sure (well,
// more likely) that the directories are empty when we try to
// delete them.
for (int i = dirs.length - 1; i >= 0; --i) {
File f = new File(toDir, dirs[i]);
if (f.list().length < 1) {
log("Removing orphan directory: " + f, Project.MSG_DEBUG);
f.delete();
++removedCount[0];
}
}
return removedCount;
|
public void | setFailOnError(boolean failonerror)If false, note errors to the output but keep going.
myCopy.setFailOnError(failonerror);
|
public void | setGranularity(long granularity)The number of milliseconds leeway to give before deciding a
target is out of date.
Default is 0 milliseconds, or 2 seconds on DOS systems.
myCopy.setGranularity(granularity);
|
public void | setIncludeEmptyDirs(boolean includeEmpty)Used to copy empty directories.
myCopy.setIncludeEmptyDirs(includeEmpty);
|
public void | setOverwrite(boolean overwrite)Overwrite any existing destination file(s).
myCopy.setOverwrite(overwrite);
|
public void | setTodir(java.io.File destDir)Sets the destination directory.
myCopy.setTodir(destDir);
|
public void | setVerbose(boolean verbose)Used to force listing of all names of copied files.
myCopy.setVerbose(verbose);
|