Hi, in my company we use one large sourcetree for several different end-products. A number of packages are shared between these products and lots are not.
Since I don't want to have a huge jar containing way to much, I want to have several jars named after the products containing only the needed packages for that product. I needed a way to select the package name from a fileset where the fileset was something like: <fileset dir="${src}" id="projectlist"> <and> <filename name="**/.jars" /> <contains text="myproject" /> </and> </fileset> This returns a number of files, all of which are named .jars and contain the the text "myproject". Now I want to find out which classes live in the same dir as that file. I can't find a way in ant to do this, so I added a number of properties to the <dirname> task. To select all classes just type: <dirname basedir="." property="packagefiles" separator="/*.class, "> <fileset refid="projectlist" /> </dirname> <apply executable="ls" dir="${buildzone}"> <fileset dir="${buildzone}" includes="${packagefiles}/*.class" /> </apply> Please consider adding the patch to the main distribution. Thanx! -- Thomas Zander [EMAIL PROTECTED] We are what we pretend to be
--- Dirname.java-old Sun Jul 7 18:09:41 2002 +++ Dirname.java Sun Jul 7 19:20:15 2002 @@ -55,9 +55,12 @@ package org.apache.tools.ant.taskdefs; import java.io.File; +import java.util.Vector; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Task; +import org.apache.tools.ant.DirectoryScanner; +import org.apache.tools.ant.types.FileSet; /** * Task to determine the directory name of the specified file. @@ -73,8 +76,13 @@ * value of the specified file up to, but not including, the last path * element. If file is a file, the directory will be the current * directory. + * When using a nested fileset the dirs will be appended in a path like manner, + * using the separator for the OS. A specific separator can be supplied as well. + * If the basedir property has been supplied the return value will be a relative + * path with the basedir as root. * * @author Diane Holt <a href="mailto:[EMAIL PROTECTED]">[EMAIL PROTECTED]</a> + * @author Thomas Zander * * @version $Revision: 1.2.2.1 $ * @@ -84,8 +92,10 @@ */ public class Dirname extends Task { - private File file; + private File file, basedir=null; private String property; + private Vector filesets = new Vector (); + private String separator = null; /** * Path to take the dirname of. @@ -96,6 +106,14 @@ } /** + * Path to use as the basedir. + * This path will be chopped of off the return values. + */ + public void setBaseDir(File dir) { + this.basedir = dir; + } + + /** * The name of the property to set. * @param property */ @@ -103,18 +121,59 @@ this.property = property; } + /** + * The separator string between each element if a nested fileset is used. + * @param separator + */ + public void setSeparator(String separator) { + this.separator = separator; + } + + /** + * Adds a set of files. + */ + public void addFileset(FileSet set) { + filesets.addElement(set); + } + // The method executing the task public void execute() throws BuildException { if (property == null) { throw new BuildException("property attribute required", location); } - if (file == null) { - throw new BuildException("file attribute required", location); - } else { - String value = file.getParent(); - getProject().setNewProperty(property, value); + + String value; // the return value + + if (file != null) { + value=getDirname(file); + } else if(filesets.size() !=0) { + if(separator == null) separator = File.pathSeparator; + + StringBuffer returnBuffer=new StringBuffer(); + for (int i = 0; i < filesets.size(); i++) { + FileSet fs = (FileSet) filesets.elementAt(i); + DirectoryScanner ds = fs.getDirectoryScanner(getProject()); + String[] s = ds.getIncludedFiles(); + for (int j = 0; j < s.length; j++) { + if(j!=0) returnBuffer.append(separator); + returnBuffer.append( getDirname(new File(s[j])) ); + } + } + value=returnBuffer.toString(); + } else + throw new BuildException("file attribute or fileset child required", +location); + + getProject().setNewProperty(property, value); + } + + protected String getDirname(File file) { + String value = file.getParent(); + if(basedir!=null && value.startsWith(basedir.getAbsolutePath()) ) { + value=value.substring(basedir.getAbsolutePath().length()); + if(value.startsWith(File.separator)) value=value.substring(1); } + return value; } }
msg18089/pgp00000.pgp
Description: PGP signature