From 1844a85f5feebe940057df6b041ab2fe095cc43d Mon Sep 17 00:00:00 2001 From: "Arnout J. Kuiper" Date: Sun, 6 Feb 2000 16:16:29 +0000 Subject: [PATCH] Made tasks able to use the new pattern based directory scanning, while retaining backwards compatibility. git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@267585 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/tools/ant/taskdefs/Copydir.java | 128 +++++++++--- .../org/apache/tools/ant/taskdefs/Jar.java | 24 ++- .../org/apache/tools/ant/taskdefs/Javac.java | 110 +++++++--- .../org/apache/tools/ant/taskdefs/Zip.java | 192 ++++++++++++------ 4 files changed, 335 insertions(+), 119 deletions(-) diff --git a/src/main/org/apache/tools/ant/taskdefs/Copydir.java b/src/main/org/apache/tools/ant/taskdefs/Copydir.java index 1318d931f..8e0b7363a 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Copydir.java +++ b/src/main/org/apache/tools/ant/taskdefs/Copydir.java @@ -67,11 +67,12 @@ import java.util.*; public class Copydir extends Task { - public File srcDir; - public File destDir; - + private File srcDir; + private File destDir; + private String[] includes; + private String[] excludes; + private boolean useDefaultExcludes = true; private Hashtable filecopyList = new Hashtable(); - private Vector ignoreList = new Vector(); public void setSrc(String src) { srcDir = project.resolveFile(src); @@ -81,8 +82,85 @@ public class Copydir extends Task { destDir = project.resolveFile(dest); } + /** + * Sets the set of include patterns. Patterns may be separated by a comma + * or a space. + * + * @param includes the string containing the include patterns + */ + public void setIncludes(String includes) { + if (includes != null && includes.length() > 0) { + Vector tmpIncludes = new Vector(); + StringTokenizer tok = new StringTokenizer(includes, ", ", false); + while (tok.hasMoreTokens()) { + String pattern = tok.nextToken().trim(); + if (pattern.length() > 0) { + tmpIncludes.addElement(pattern); + } + } + this.includes = new String[tmpIncludes.size()]; + for (int i = 0; i < tmpIncludes.size(); i++) { + this.includes[i] = (String)tmpIncludes.elementAt(i); + } + } else { + this.includes = null; + } + } + + /** + * Sets the set of exclude patterns. Patterns may be separated by a comma + * or a space. + * + * @param excludes the string containing the exclude patterns + */ + public void setExcludes(String excludes) { + if (excludes != null && excludes.length() > 0) { + Vector tmpExcludes = new Vector(); + StringTokenizer tok = new StringTokenizer(excludes, ", ", false); + while (tok.hasMoreTokens()) { + String pattern = tok.nextToken().trim(); + if (pattern.length() > 0) { + tmpExcludes.addElement(pattern); + } + } + this.excludes = new String[tmpExcludes.size()]; + for (int i = 0; i < tmpExcludes.size(); i++) { + this.excludes[i] = (String)tmpExcludes.elementAt(i); + } + } else { + this.excludes = null; + } + } + + /** + * Sets whether default exclusions should be used or not. + * + * @param useDefaultExcludes "true" or "on" when default exclusions should + * be used, "false" or "off" when they + * shouldn't be used. + */ + public void setDefaultexcludes(String useDefaultExcludes) { + this.useDefaultExcludes = Project.toBoolean(useDefaultExcludes); + } + public void execute() throws BuildException { - scanDir(srcDir, destDir); + if (srcDir == null) { + throw new BuildException("srcdir attribute must be set!"); + } + if (!srcDir.exists()) { + throw new BuildException("srcdir does not exist!"); + } + DirectoryScanner ds = new DirectoryScanner(); + ds.setBasedir(srcDir); + ds.setIncludes(includes); + ds.setExcludes(excludes); + if (useDefaultExcludes) { + ds.addDefaultExcludes(); + } + ds.scan(); + + String[] files = ds.getIncludedFiles(); + scanDir(srcDir, destDir, files); if (filecopyList.size() > 0) { project.log("Copying " + filecopyList.size() + " files to " + destDir.getAbsolutePath()); @@ -115,38 +193,32 @@ public class Copydir extends Task { @author Jon S. Stevens jon@clearink.com */ public void setIgnore(String ignoreString) { - ignoreString = ignoreString; + project.log("The ignore attribute is deprecated. "+ + "Please use the excludes attribute.", + Project.MSG_WARN); if (ignoreString != null && ignoreString.length() > 0) { - StringTokenizer tok = - new StringTokenizer(ignoreString, ", ", false); + Vector tmpExcludes = new Vector(); + StringTokenizer tok = new StringTokenizer(ignoreString, ", ", false); while (tok.hasMoreTokens()) { - ignoreList.addElement ( tok.nextToken().trim() ); + tmpExcludes.addElement("**/"+tok.nextToken().trim()+"/**"); } + this.excludes = new String[tmpExcludes.size()]; + for (int i = 0; i < tmpExcludes.size(); i++) { + this.excludes[i] = (String)tmpExcludes.elementAt(i); + } + } else { + this.excludes = null; } } - private void scanDir(File from, File to) { - String[] list = from.list(new DesirableFilter()); - if (list == null) { - project.log("Source directory " + srcDir.getAbsolutePath() - + " does not exist.", "copydir", Project.MSG_WARN); - return; - } - for (int i = 0; i < list.length; i++) { - String filename = list[i]; + private void scanDir(File from, File to, String[] files) { + for (int i = 0; i < files.length; i++) { + String filename = files[i]; File srcFile = new File(from, filename); File destFile = new File(to, filename); - if ( ! ignoreList.contains(filename) ) { - if (srcFile.isDirectory()) { - scanDir(srcFile, destFile); - } else { - if (srcFile.lastModified() > destFile.lastModified()) { - filecopyList.put(srcFile.getAbsolutePath(), + if (srcFile.lastModified() > destFile.lastModified()) { + filecopyList.put(srcFile.getAbsolutePath(), destFile.getAbsolutePath()); - } - } - } else { - project.log("Copydir Ignored: " + filename, Project.MSG_VERBOSE); } } } diff --git a/src/main/org/apache/tools/ant/taskdefs/Jar.java b/src/main/org/apache/tools/ant/taskdefs/Jar.java index 866cc5e9b..3b9364d44 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Jar.java +++ b/src/main/org/apache/tools/ant/taskdefs/Jar.java @@ -87,7 +87,7 @@ public class Jar extends Zip { if (manifest != null) { ZipEntry ze = new ZipEntry("META-INF/"); zOut.putNextEntry(ze); - zipFile(manifest, zOut, "META-INF/MANIFEST.MF"); + super.zipFile(manifest, zOut, "META-INF/MANIFEST.MF"); } else { ZipEntry ze = new ZipEntry("META-INF/"); zOut.putNextEntry(ze); @@ -100,14 +100,22 @@ public class Jar extends Zip { } protected void zipDir(File dir, ZipOutputStream zOut, String vPath) - throws IOException + throws IOException { - // First add directory to zip entry - if( ! "META-INF/".equals(vPath) ) { - // we already added a META-INF - ZipEntry ze = new ZipEntry(vPath); - zOut.putNextEntry(ze); + // First add directory to zip entry + if(!vPath.equals("META-INF/")) { + // we already added a META-INF + ZipEntry ze = new ZipEntry(vPath); + zOut.putNextEntry(ze); + } + } + + protected void zipFile(File file, ZipOutputStream zOut, String vPath) + throws IOException + { + // We already added a META-INF/MANIFEST.MF + if (!vPath.equals("META-INF/MANIFEST.MF")) { + super.zipFile(file, zOut, vPath); } - super.zipDir(dir, zOut, vPath); } } diff --git a/src/main/org/apache/tools/ant/taskdefs/Javac.java b/src/main/org/apache/tools/ant/taskdefs/Javac.java index b55274972..7c77f7006 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Javac.java +++ b/src/main/org/apache/tools/ant/taskdefs/Javac.java @@ -98,6 +98,9 @@ public class Javac extends Task { private String target; private String bootclasspath; private String extdirs; + private String[] includes; + private String[] excludes; + private boolean useDefaultExcludes = true; private Vector compileList = new Vector(); private Hashtable filecopyList = new Hashtable(); @@ -193,22 +196,91 @@ public class Javac extends Task { } /** - * Executes the task. + * Sets the set of include patterns. Patterns may be separated by a comma + * or a space. + * + * @param includes the string containing the include patterns */ + public void setIncludes(String includes) { + if (includes != null && includes.length() > 0) { + Vector tmpIncludes = new Vector(); + StringTokenizer tok = new StringTokenizer(includes, ", ", false); + while (tok.hasMoreTokens()) { + tmpIncludes.addElement(tok.nextToken().trim()); + } + this.includes = new String[tmpIncludes.size()]; + for (int i = 0; i < tmpIncludes.size(); i++) { + this.includes[i] = (String)tmpIncludes.elementAt(i); + } + } else { + this.includes = null; + } + } - public void execute() throws BuildException { + /** + * Sets the set of exclude patterns. Patterns may be separated by a comma + * or a space. + * + * @param excludes the string containing the exclude patterns + */ + public void setExcludes(String excludes) { + if (excludes != null && excludes.length() > 0) { + Vector tmpExcludes = new Vector(); + StringTokenizer tok = new StringTokenizer(excludes, ", ", false); + while (tok.hasMoreTokens()) { + tmpExcludes.addElement(tok.nextToken().trim()); + } + this.excludes = new String[tmpExcludes.size()]; + for (int i = 0; i < tmpExcludes.size(); i++) { + this.excludes[i] = (String)tmpExcludes.elementAt(i); + } + } else { + this.excludes = null; + } + } + /** + * Sets whether default exclusions should be used or not. + * + * @param useDefaultExcludes "true" or "on" when default exclusions should + * be used, "false" or "off" when they + * shouldn't be used. + */ + public void setDefaultexcludes(String useDefaultExcludes) { + this.useDefaultExcludes = Project.toBoolean(useDefaultExcludes); + } + + /** + * Executes the task. + */ + public void execute() throws BuildException { // first off, make sure that we've got a srcdir and destdir - if (srcDir == null || destDir == null ) { - String msg = "srcDir and destDir attributes must be set!"; - throw new BuildException(msg); + if (srcDir == null) { + throw new BuildException("srcdir attribute must be set!"); + } + if (!srcDir.exists()) { + throw new BuildException("srcdir does not exist!"); + } + if (destDir == null) { + throw new BuildException("destdir attribute must be set!"); } // scan source and dest dirs to build up both copy lists and // compile lists - scanDir(srcDir, destDir); + DirectoryScanner ds = new DirectoryScanner(); + ds.setBasedir(srcDir); + ds.setIncludes(includes); + ds.setExcludes(excludes); + if (useDefaultExcludes) { + ds.addDefaultExcludes(); + } + ds.scan(); + + String[] files = ds.getIncludedFiles(); + + scanDir(srcDir, destDir, files); // compile the source files @@ -262,33 +334,21 @@ public class Javac extends Task { * support files to be copied. */ - private void scanDir(File srcDir, File destDir) { - - String[] list = srcDir.list(new DesirableFilter()); - int len = (list==null ? 0 : list.length); - for (int i = 0; i < len; i++) { - String filename = list[i]; - File srcFile = new File(srcDir, filename); - File destFile = new File(destDir, filename); - if (srcFile.isDirectory()) { - // it's a dir, scan that recursively - scanDir(srcFile, destFile); - } else { - // it's a file, see if we compile it or just copy it - if (filename.endsWith(".java")) { - File classFile = - new File(destDir, - filename.substring(0, - filename.indexOf(".java")) + private void scanDir(File srcDir, File destDir, String files[]) { + for (int i = 0; i < files.length; i++) { + File srcFile = new File(srcDir, files[i]); + if (files[i].endsWith(".java")) { + File classFile = new File(destDir, files[i].substring(0, + files[i].indexOf(".java")) + ".class"); if (srcFile.lastModified() > classFile.lastModified()) { compileList.addElement(srcFile.getAbsolutePath()); } } else { + File destFile = new File(destDir, files[i]); if (srcFile.lastModified() > destFile.lastModified()) { filecopyList.put(srcFile.getAbsolutePath(), destFile.getAbsolutePath()); - } } } } diff --git a/src/main/org/apache/tools/ant/taskdefs/Zip.java b/src/main/org/apache/tools/ant/taskdefs/Zip.java index f2c3a3158..a85121f39 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Zip.java +++ b/src/main/org/apache/tools/ant/taskdefs/Zip.java @@ -74,10 +74,10 @@ public class Zip extends Task { private File zipFile; private File baseDir; - private Vector items = new Vector(); + private String[] includes; + private String[] excludes; + private boolean useDefaultExcludes = true; private File manifest; - private Vector ignoreList = new Vector(); - private boolean allItems = false; protected String archiveType = "zip"; /** @@ -107,15 +107,28 @@ public class Zip extends Task { ignore lists are easier than include lists. ;-) */ public void setItems(String itemString) { - if ( itemString.equals("*") ) { - allItems = true; + project.log("The items attribute is deprecated. "+ + "Please use the includes attribute.", + Project.MSG_WARN); + if (itemString == null || itemString.equals("*")) { + includes = new String[1]; + includes[0] = "**"; } else { - StringTokenizer tok = new StringTokenizer(itemString, ",", false); + Vector tmpIncludes = new Vector(); + StringTokenizer tok = new StringTokenizer(itemString, ", "); while (tok.hasMoreTokens()) { - items.addElement(tok.nextToken().trim()); + String pattern = tok.nextToken().trim(); + if (pattern.length() > 0) { + tmpIncludes.addElement(pattern+"/**"); + } + } + this.includes = new String[tmpIncludes.size()]; + for (int i = 0; i < tmpIncludes.size(); i++) { + this.includes[i] = (String)tmpIncludes.elementAt(i); } } } + /** List of filenames and directory names to not include in the final .jar file. They should be either @@ -130,49 +143,127 @@ public class Zip extends Task { @author Jon S. Stevens jon@clearink.com */ public void setIgnore(String ignoreString) { - ignoreString = ignoreString; - if (ignoreString != null && ignoreString.length() > 0) { - StringTokenizer tok = - new StringTokenizer(ignoreString, ", ", false); + project.log("The ignore attribute is deprecated. "+ + "Please use the excludes attribute.", + Project.MSG_WARN); + if (ignoreString == null) { + this.excludes = null; + } else { + Vector tmpExcludes = new Vector(); + StringTokenizer tok = new StringTokenizer(ignoreString, ", "); while (tok.hasMoreTokens()) { - ignoreList.addElement ( tok.nextToken().trim() ); + String pattern = tok.nextToken().trim(); + if (pattern.length() > 0) { + tmpExcludes.addElement("**/"+pattern+"/**"); + } + } + this.excludes = new String[tmpExcludes.size()]; + for (int i = 0; i < tmpExcludes.size(); i++) { + this.excludes[i] = (String)tmpExcludes.elementAt(i); } } } + /** + * Sets the set of include patterns. Patterns may be separated by a comma + * or a space. + * + * @param includes the string containing the include patterns + */ + public void setIncludes(String includes) { + if (includes == null) { + this.includes = null; + } else { + Vector tmpIncludes = new Vector(); + StringTokenizer tok = new StringTokenizer(includes, ", "); + while (tok.hasMoreTokens()) { + String pattern = tok.nextToken().trim(); + if (pattern.length() > 0) { + tmpIncludes.addElement(pattern); + } + } + this.includes = new String[tmpIncludes.size()]; + for (int i = 0; i < tmpIncludes.size(); i++) { + this.includes[i] = (String)tmpIncludes.elementAt(i); + } + } + } + + /** + * Sets the set of exclude patterns. Patterns may be separated by a comma + * or a space. + * + * @param excludes the string containing the exclude patterns + */ + public void setExcludes(String excludes) { + if (excludes == null) { + this.excludes = null; + } else { + Vector tmpExcludes = new Vector(); + StringTokenizer tok = new StringTokenizer(excludes, ", ", false); + while (tok.hasMoreTokens()) { + String pattern = tok.nextToken().trim(); + if (pattern.length() > 0) { + tmpExcludes.addElement(pattern); + } + } + this.excludes = new String[tmpExcludes.size()]; + for (int i = 0; i < tmpExcludes.size(); i++) { + this.excludes[i] = (String)tmpExcludes.elementAt(i); + } + } + } + + /** + * Sets whether default exclusions should be used or not. + * + * @param useDefaultExcludes "true" or "on" when default exclusions should + * be used, "false" or "off" when they + * shouldn't be used. + */ + public void setDefaultexcludes(String useDefaultExcludes) { + this.useDefaultExcludes = Project.toBoolean(useDefaultExcludes); + } + public void execute() throws BuildException { - project.log("Building " + archiveType + ": " + zipFile.getAbsolutePath()); - + project.log("Building "+ archiveType +": "+ zipFile.getAbsolutePath()); + + if (baseDir == null) { + throw new BuildException("basedir attribute must be set!"); + } + if (!baseDir.exists()) { + throw new BuildException("basedir does not exist!"); + } + + DirectoryScanner ds = new DirectoryScanner(); + ds.setBasedir(baseDir); + ds.setIncludes(includes); + ds.setExcludes(excludes); + if (useDefaultExcludes) { + ds.addDefaultExcludes(); + } + ds.scan(); + + String[] files = ds.getIncludedFiles(); + String[] dirs = ds.getIncludedDirectories(); + try { ZipOutputStream zOut = new ZipOutputStream(new FileOutputStream(zipFile)); - initZipOutputStream(zOut); - - if ( allItems ) { - String[] lst = baseDir.list(); - for (int i=0;i