📄 sync.java
字号:
* @param dir the root directory to scan for empty directories. * @param removeIfEmpty whether to remove the root directory * itself if it becomes empty. * @return the number of empty directories actually removed. */ private int removeEmptyDirectories(File dir, boolean removeIfEmpty) { 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; } // // Various copy attributes/subelements of <copy> passed thru to <mycopy> // /** * Sets the destination directory. * @param destDir the destination directory */ public void setTodir(File destDir) { myCopy.setTodir(destDir); } /** * Used to force listing of all names of copied files. * @param verbose if true force listing of all names of copied files. */ public void setVerbose(boolean verbose) { myCopy.setVerbose(verbose); } /** * Overwrite any existing destination file(s). * @param overwrite if true overwrite any existing destination file(s). */ public void setOverwrite(boolean overwrite) { myCopy.setOverwrite(overwrite); } /** * Used to copy empty directories. * @param includeEmpty If true copy empty directories. */ public void setIncludeEmptyDirs(boolean includeEmpty) { myCopy.setIncludeEmptyDirs(includeEmpty); } /** * If false, note errors to the output but keep going. * @param failonerror true or false */ public void setFailOnError(boolean failonerror) { myCopy.setFailOnError(failonerror); } /** * Adds a set of files to copy. * @param set a fileset */ public void addFileset(FileSet set) { add(set); } /** * Adds a collection of filesystem resources to copy. * @param rc a resource collection * @since Ant 1.7 */ public void add(ResourceCollection rc) { myCopy.add(rc); } /** * The number of milliseconds leeway to give before deciding a * target is out of date. * * <p>Default is 0 milliseconds, or 2 seconds on DOS systems.</p> * @param granularity a <code>long</code> value * @since Ant 1.6.2 */ public void setGranularity(long granularity) { myCopy.setGranularity(granularity); } /** * 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. * * <p>You must not invoke this method more than once.</p> * @param s a preserveintarget nested element * @since Ant 1.7 */ public void addPreserveInTarget(SyncTarget s) { if (syncTarget != null) { throw new BuildException("you must not specify multiple " + "preserveintarget elements."); } syncTarget = s; } /** * Subclass Copy in order to access it's file/dir maps. */ public static class MyCopy extends Copy { // List of files that must be copied, irrelevant from the // fact that they are newer or not than the destination. private Set nonOrphans = new HashSet(); /** Constructor for MyCopy. */ public MyCopy() { } /** * @see Copy#scan(File, File, String[], String[]) */ /** {@inheritDoc} */ protected void scan(File fromDir, File toDir, String[] files, String[] dirs) { assertTrue("No mapper", mapperElement == null); super.scan(fromDir, toDir, files, dirs); for (int i = 0; i < files.length; ++i) { nonOrphans.add(files[i]); } for (int i = 0; i < dirs.length; ++i) { nonOrphans.add(dirs[i]); } } /** * @see Copy#scan(Resource[], File) */ /** {@inheritDoc} */ protected Map scan(Resource[] resources, File toDir) { assertTrue("No mapper", mapperElement == null); Map m = super.scan(resources, toDir); Iterator iter = m.keySet().iterator(); while (iter.hasNext()) { nonOrphans.add(((Resource) iter.next()).getName()); } return m; } /** * Get the destination directory. * @return the destination directory */ public File getToDir() { return destDir; } /** * Get the includeEmptyDirs attribute. * @return true if emptyDirs are to be included */ public boolean getIncludeEmptyDirs() { return includeEmpty; } /** * Yes, we can. * @return true always. * @since Ant 1.7 */ protected boolean supportsNonFileResources() { return true; } } /** * Inner class used to hold exclude patterns and selectors to save * stuff that happens to live in the target directory but should * not get removed. * * @since Ant 1.7 */ public static class SyncTarget extends AbstractFileSet { /** * Constructor for SyncTarget. * This just changes the default value of "defaultexcludes" from * true to false. */ public SyncTarget() { super(); } /** * Override AbstractFileSet#setDir(File) to disallow * setting the directory. * @param dir ignored * @throws BuildException always */ public void setDir(File dir) throws BuildException { throw new BuildException("preserveintarget doesn't support the dir " + "attribute"); } } /** * Pseudo-assert method. */ private static void assertTrue(String message, boolean condition) { if (!condition) { throw new BuildException("Assertion Error: " + message); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -