This stupid delete or deltree extends MatchingTask thing is the ONLY reason I'm on this list. I posted the patch a couple months ago (to Deltree) and have seen it posted 2 more times by different people since then ... after which I reposted it again (and now again).
What does that say about a project, when you have several people re-posting months old patches? Anyway, I am under the (probably incorrect) assumption that Arnout is working on standardizing delete, copy, rename, move and whatever else to be MatchingTask items, as Arnout was leading those discussions. -j > -----Original Message----- > From: Jakob Ramskov [mailto:[EMAIL PROTECTED] > Sent: Wednesday, June 14, 2000 1:00 PM > To: [EMAIL PROTECTED] > Subject: Re: delete question > > > Ofcourse it should be added to the core, it's a major limitation. > > Can't committers just contribute it? > > Do remember to add it to the documentation.. features that aren't > documented > are no good. > > ----- Original Message ----- > From: "Kevin Regan" <[EMAIL PROTECTED]> > To: <[EMAIL PROTECTED]> > Sent: Wednesday, June 14, 2000 9:50 PM > Subject: RE: delete question > > > > Thanks alot! I'll try this out. Is there a plan to > > add this to the Ant core? > > > > Thanks again, > > Kevin >
Index: src/main/org/apache/tools/ant/taskdefs/Deltree.java =================================================================== RCS file: /home/cvspublic/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Deltree.java,v retrieving revision 1.2 diff -u -r1.2 Deltree.java --- src/main/org/apache/tools/ant/taskdefs/Deltree.java 2000/02/24 01:34:45 1.2 +++ src/main/org/apache/tools/ant/taskdefs/Deltree.java 2000/05/04 21:16:53 @@ -1,7 +1,7 @@ /* * The Apache Software License, Version 1.1 * - * Copyright (c) 1999 The Apache Software Foundation. All rights + * Copyright (c) 1999 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without @@ -9,7 +9,7 @@ * are met: * * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. + * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in @@ -17,15 +17,15 @@ * distribution. * * 3. The end-user documentation included with the redistribution, if - * any, must include the following acknowlegement: - * "This product includes software developed by the + * any, must include the following acknowlegement: + * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowlegement may appear in the software itself, * if and wherever such third-party acknowlegements normally appear. * * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software * Foundation" must not be used to endorse or promote products derived - * from this software without prior written permission. For written + * from this software without prior written permission. For written * permission, please contact [EMAIL PROTECTED] * * 5. Products derived from this software may not be called "Apache" @@ -58,45 +58,77 @@ import java.io.*; /** + * A task to delete a directory and its contents recursively, or to delete + * files matching the regular includes/excludes patterns recursively. + * <p>The <code>dir</code> attribute is required. If there are no includes + * or excludes specified, Deltree will remove the specified directory and all + * its contents if possible. If includes and/or excludes are present, then only + * files matching the given pattern will be deleted. The delete can be recursive + * if the pattern specifies such.</p> + * <p>For example, to remove all Java .class files from the src.dir directory + * recursively: </p> + * <pre> + * <deltree dir="${src.dir}" includes="**/*.class" /> + * </pre> + * <p>To remove the entire directory build.dir, use the following:</p> + * <pre> + * <deltree dir="${build.dir}" /> + * </pre> * - * * @author [EMAIL PROTECTED] */ - -public class Deltree extends Task { +public class Deltree extends MatchingTask { private File dir; public void setDir(String dirName) { dir = project.resolveFile(dirName); } - - public void execute() throws BuildException { - project.log("Deleting: " + dir.getAbsolutePath()); - if (dir.exists()) { - if (!dir.isDirectory()) { - dir.delete(); - return; - // String msg = "Given dir: " + dir.getAbsolutePath() + - // " is not a dir"; - // throw new BuildException(msg); + public void execute() throws BuildException { + if (dir == null) { + throw new BuildException("dir attribute must be set!"); + } + + // If both includeList and exludeList are empty, delete the whole directory. + if (includeList.size () == 0 && excludeList.size () == 0) { + project.log("Deleting: " + dir.getAbsolutePath()); + + if (dir.exists()) { + if (!dir.isDirectory()) { + dir.delete(); + return; + // String msg = "Given dir: " + dir.getAbsolutePath() + + // " is not a dir"; + // throw new BuildException(msg); + } + try { + removeDir(dir); + } catch (IOException ioe) { + String msg = "Unable to delete " + dir.getAbsolutePath(); + throw new BuildException(msg); + } } - try { - removeDir(dir); - } catch (IOException ioe) { - String msg = "Unable to delete " + dir.getAbsolutePath(); - throw new BuildException(msg); - } - } + } else { + // Else we have a pattern, delete the matching files with dir as base path. + DirectoryScanner ds = this.getDirectoryScanner (dir); + String[] files = ds.getIncludedFiles(); + project.log ("Deleting: " + files.length + " files."); + for (int i = 0; i < files.length; i++) { + File f = new File (files[i]); + if (!f.delete ()) { + throw new BuildException ("Unable to delete " + f.getAbsolutePath ()); + } + } + } } - + private void removeDir(File dir) throws IOException { // check to make sure that the given dir isn't a symlink // the comparison of absolute path and canonical path // catches this - + // if (dir.getCanonicalPath().equals(dir.getAbsolutePath())) { // (costin) It will not work if /home/costin is symlink to /da0/home/costin ( taz // for example )
