You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by Jason Sando <js...@think1up.com> on 2000/05/04 23:18:09 UTC
[PATCH] - Enhancement to Deltree task to recursively delete files matching includes/excludes patterns.
Committers,
My company's current project requires the class files in the same path as
the sources, so in order to do a 'clean' I needed to remove the .class files
from the source tree. We also have generated .java files, same thing.
Let me know if there is already a way to do this.
Thanks,
-j
Index: src/main/org/apache/tools/ant/taskdefs/Deltree.java
===================================================================
RCS file:
/home/cvspublic/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Deltree.j
ava,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 apache@apache.org.
*
* 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 duncan@x180.com
*/
-
-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 )