You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by Antony paul <an...@hotmail.com> on 2004/01/26 06:42:24 UTC

Telling DirectoryScanner to scan each directory separately.

Hi all,
   I am writing a custom task which needs to get the list of included files 
for each directory but not any subdirectory and files inside it. First I 
have to get all included directories which is set in the build file. Then I 
will scan each individual directory to get the included files. While 
scanning a directory I dont want to get any subdirectory name or files in 
subdirectory. How to do this.
  I am using 1.5.4. Custome task is extending MatchingTask. Also a minor 
problem. I am using the same build file to compile and run the custom task. 
First compile, make it a jar, copy the jar file to ANT_HOME\lib directory 
then call the target to run the task. If I run the task in one step it will 
run the old class file. I have to launch ant a second time to run the newly 
copied task. Is it possible to compile and run it in one step ?.

rgds
Antony Paul

_________________________________________________________________
Let the new MSN Premium Internet Software make the most of your high-speed 
experience. http://join.msn.com/?pgmarket=en-us&page=byoa/prem&ST=1


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Re: Telling DirectoryScanner to scan each directory separately.

Posted by Antoine Lévy-Lambert <an...@antbuild.com>.
Antony paul wrote:

> Hi all,
>   I am writing a custom task which needs to get the list of included 
> files for each directory but not any subdirectory and files inside it. 
> First I have to get all included directories which is set in the build 
> file. Then I will scan each individual directory to get the included 
> files. While scanning a directory I dont want to get any subdirectory 
> name or files in subdirectory. How to do this.
>
>
>
This snippet is really for ant 1.6.0, because the type selector has been 
introduced in 1.6.0

If you do not want to include subdirectories in a fileset, do something 
like :

<fileset dir="somedir" id="myfileset">
    <!-- this include pattern includes only files and dirs under somedir -->
    <include name="*"/>
    <!-- exclude directories -->
    <not>
        <type type="dir"/>
    </not>
</fileset>


if you have put such a snippet in a build file, for which you have 
created a Project object,
you can get the reference back to your fileset with

Reference r = getProject().getReference("myfileset");
FileSet myFileset = (FileSet) r.getReferencedObject();


 > I am using 1.5.4. Custome task is extending MatchingTask. Also a 
minor problem. I am using the same build file to compile and run the 
custom task. First compile, make it a jar, copy the jar file to 
ANT_HOME\lib directory >then call the target to run the task. If I run 
the task in one step it will run the old class file. I have to launch 
ant a second time to run the newly copied task. Is it possible to 
compile and run it in one step ?.

You might want to wrap your build process in a script or .bat file and 
separate the compilation/jarring of the custom task, and the use of the 
custom task. (Just my feeling).


Antoine

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


RE: Telling DirectoryScanner to scan each directory separately.

Posted by Jim Fuller <ji...@ruminate.co.uk>.

> -----Original Message-----
> From: Antony paul [mailto:antonypaul24@hotmail.com] 
> Sent: 26 January 2004 05:42
> Subject: Telling DirectoryScanner to scan each directory separately.

>    I am writing a custom task which needs to get the list of 
> included files 
> for each directory but not any subdirectory and files inside 
> it. First I 
> have to get all included directories which is set in the 
> build file. Then I 
> will scan each individual directory to get the included files. While 
> scanning a directory I dont want to get any subdirectory name 
> or files in 
> subdirectory. How to do this.

There was a question last week on this, I will repeat my answer....avoid
using matchingtask.

Here is an example that takes a fileset;


import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Vector;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.types.FileSet;


public class AntFileSetTest extends Task{
	private Vector filesets = new Vector();

    public void addFileset(FileSet fileset) {
        filesets.add(fileset);
    }

    public void execute(){

   log("executing Xindice Ant Task");



int fileCount = 0;
int successCount = 0;

Enumeration enum = filesets.elements();

while(enum.hasMoreElements()){

	FileSet fileset = (FileSet) enum.nextElement();
	DirectoryScanner ds = fileset.getDirectoryScanner(getProject());
	String[] files = ds.getIncludedFiles();

        for(int i=0; i<files.length; i++) {

			fileCount++;

			File f = new File(fileset.getDir( getProject()
),files[i]);

			if (process(f)){
				successCount++;
			}
		}

}

System.out.println("task working properly");
}

protected boolean process(File file){
        System.out.println("file processed");
        return true;
}


}

The process method will do something on the individual file, whereas the
enum snippet does the hard work in the execute method.

>   I am using 1.5.4. Custome task is extending MatchingTask. 
> Also a minor 
> problem. I am using the same build file to compile and run 
> the custom task. 
> First compile, make it a jar, copy the jar file to 
> ANT_HOME\lib directory 
> then call the target to run the task. If I run the task in 
> one step it will 
> run the old class file. I have to launch ant a second time to 
> run the newly 
> copied task. Is it possible to compile and run it in one step ?.

Without seeing your build file, I can only assume that you are not
explicitly defining the classpath. 

Gl, jim fuller



> 
> rgds
> Antony Paul


> 
> _________________________________________________________________
> Let the new MSN Premium Internet Software make the most of 
> your high-speed 
> experience. http://join.msn.com/?pgmarket=en-us&page=byoa/prem&ST=1
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org