ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From arn...@locus.apache.org
Subject cvs commit: jakarta-ant/src/main/org/apache/tools/ant/taskdefs Zip.java Jar.java Copydir.java Javac.java
Date Sun, 06 Feb 2000 16:16:30 GMT
arnout      00/02/06 08:16:30

  Modified:    src/main/org/apache/tools/ant/taskdefs Zip.java Jar.java
                        Copydir.java Javac.java
  Log:
  Made tasks able to use the new pattern based directory scanning, while
  retaining backwards compatibility.
  
  Revision  Changes    Path
  1.2       +134 -58   jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Zip.java
  
  Index: Zip.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Zip.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- Zip.java	2000/01/13 10:41:41	1.1
  +++ Zip.java	2000/02/06 16:16:29	1.2
  @@ -74,10 +74,10 @@
   
       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 @@
           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 @@
           @author Jon S. Stevens <a href="mailto:jon@clearink.com">jon@clearink.com</a>
       */
       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<lst.length;i++) {
  -                    items.addElement(lst[i]);
  -                }
  +            initZipOutputStream(zOut);
  +
  +            for (int i = 0; i < dirs.length; i++) {
  +                File f = new File(baseDir,dirs[i]);
  +                String name = dirs[i].replace(File.separatorChar,'/')+"/";
  +                zipDir(f, zOut, name);
               }
   
  -            // add items
  -            Enumeration e = items.elements();
  -            while (e.hasMoreElements()) {
  -                String s = (String)e.nextElement();
  -                // check to make sure item is not in ignore list
  -                // shouldn't be ignored here, but just want to make sure
  -                if (! ignoreList.contains(s)) {
  -                    File f = new File(baseDir, s);
  -                    if (f.isDirectory()) {
  -                        zipDir(f, zOut, s + "/");
  -                    } else {
  -                        zipFile(f, zOut, s);
  -                    }
  -                } else {
  -                    project.log("Ignoring: " + s, Project.MSG_WARN);
  -                }
  +            for (int i = 0; i < files.length; i++) {
  +                File f = new File(baseDir,files[i]);
  +                String name = files[i].replace(File.separatorChar,'/');
  +                zipFile(f, zOut, name);
               }
  -    
  -            // close up            
  +
  +            // close up
               zOut.close();
           } catch (IOException ioe) {
               String msg = "Problem creating " + archiveType + " " + ioe.getMessage();
  @@ -181,29 +272,14 @@
       }
   
       protected void initZipOutputStream(ZipOutputStream zOut)
  -	throws IOException, BuildException
  +        throws IOException, BuildException
       {
  -	zOut.setMethod(ZipOutputStream.DEFLATED);
  +        zOut.setMethod(ZipOutputStream.DEFLATED);
       }
   
       protected void zipDir(File dir, ZipOutputStream zOut, String vPath)
           throws IOException
       {
  -        String[] list = dir.list();
  -        for (int i = 0; i < list.length; i++) {
  -            String f = list[i];
  -            // check to make sure item is not in ignore list
  -            if (! ignoreList.contains(f)) {
  -                File file = new File(dir, f);
  -                if (file.isDirectory()) {
  -                    zipDir(file, zOut, vPath + f + "/");
  -                } else {
  -                    zipFile(file, zOut, vPath + f);
  -                }
  -            } else {
  -                project.log("Ignoring: " + f, Project.MSG_WARN);
  -            }
  -        }
       }
   
       protected void zipFile(InputStream in, ZipOutputStream zOut, String vPath)
  @@ -211,7 +287,7 @@
       {
           ZipEntry ze = new ZipEntry(vPath);
           zOut.putNextEntry(ze);
  -        
  +
           byte[] buffer = new byte[8 * 1024];
           int count = 0;
           do {
  @@ -219,7 +295,7 @@
               count = in.read(buffer, 0, buffer.length);
           } while (count != -1);
       }
  -    
  +
       protected void zipFile(File file, ZipOutputStream zOut, String vPath)
           throws IOException
       {
  
  
  
  1.2       +16 -8     jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Jar.java
  
  Index: Jar.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Jar.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- Jar.java	2000/01/13 10:41:41	1.1
  +++ Jar.java	2000/02/06 16:16:29	1.2
  @@ -87,7 +87,7 @@
   	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 @@
        }
   
       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);
       }
   }
  
  
  
  1.3       +100 -28   jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Copydir.java
  
  Index: Copydir.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Copydir.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Copydir.java	2000/01/21 04:44:50	1.2
  +++ Copydir.java	2000/02/06 16:16:29	1.3
  @@ -67,11 +67,12 @@
   
   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 @@
   	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 @@
           @author Jon S. Stevens <a href="mailto:jon@clearink.com">jon@clearink.com</a>
       */
       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);
               }
           }
       }
  
  
  
  1.3       +85 -25    jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Javac.java
  
  Index: Javac.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Javac.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Javac.java	2000/01/27 03:54:15	1.2
  +++ Javac.java	2000/02/06 16:16:29	1.3
  @@ -98,6 +98,9 @@
       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 @@
       }
   
       /**
  -     * 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;
  +        }
  +    }
  +
  +    /**
  +     * 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
  +
  +        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);
  +        scanDir(srcDir, destDir, files);
   
           // compile the source files
   
  @@ -262,33 +334,21 @@
        * 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());
  -                    }
                   }
               }
           }
  
  
  

Mime
View raw message