ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Jim Fuller" <jim.ful...@ruminate.co.uk>
Subject RE: Help creating a MatchingTask
Date Wed, 21 Jan 2004 10:02:03 GMT
Try something like this (


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){

//work done here on a per file basis

        System.out.println("file processed");
        return true;
}


}

With ant snippet ( remember to use taskdef )

	<test>
	   <fileset dir="somedir">
	                <include name="**/*.*"/>
            </fileset>
	</test>

The output would be a the 'file processed' message displaying per
included file.

Gl, Jim Fuller

> -----Original Message-----
> From: Mark McKay [mailto:mark@kitfox.com] 
> Sent: 21 January 2004 09:43
> To: Ant Developers List
> Subject: Re: Help creating a MatchingTask
> 
> 
> Conor MacNeill wrote:
> 
> >Mark,
> >
> >Peter and Jan look to have figured out your compile issue. I would 
> >recommend,
> >however, that you reconsider using MatchingTask. It was the 
> way early Ant 
> >tasks were implements but it is not ideal. A better approach 
> would be to use 
> >an addFileset(FileSet fs) method and process that fileset 
> (or multiple 
> >filesets) in your execute method.
> >
> >Your task then has an explicit fileset
> >
> ><task>
> >  <fileset .../>
> ></task>
> >
> >It's a cleaner approach overall.
> >
> >Conor
> >  
> >
> 
> Thanks for all the help offered so far.  I've modified my code and 
> getting better results, but am still error prone. 
> 
> I can build my ant task now; however, when I try to use it I get:
> 
>     build.xml [18] The <fileset> data type doesn't support the nested 
> "includes" element.
>     BUILD FAILED
> 
> I'm assuming this means that 'fileset' is seen as an ordinary nested 
> element, and not the special FileSet facility provided by Ant.
> 
> I've included my task and code.  Any further help would be great!
> 
> Mark McKay
> 
> =====================
> 
> 
> 
>     <target name="use" description="Use the Task" depends="jar">
>         <taskdef name="walkTree"
>                  classname="com.kitfox.anttask.MyAntTask"
>                  classpath="${ant.project.name}.jar"/>
>         <walkTree>
>             <fileset>
>                 <includes name="**/*.class"/>>
>             </fileset>
>         </walkTree>
>     </target>
> 
> 
> public class MyAntTask extends MatchingTask {
>    
>     FileSet sourceFiles;
>    
>     /** Creates a new instance of MyAntTask */
>     public MyAntTask() {
>     }
> 
>     public void addFileset(FileSet fs)
>     {
>         sourceFiles = fs;
>     }
> 
>     public void execute() throws BuildException
>     {
>         log("Walking tree");
>    
>         DirectoryScanner scanner = 
> sourceFiles.getDirectoryScanner(getProject());
>        
>         scanner.scan();
>        
>         String[] files = scanner.getIncludedFiles();
>         for (int i = 0; i < files.length; i++) {
>             //System.out.println(files[i]);
>             log(files[i]);
>         }
>    
>     }
>    
> }
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
> For additional commands, e-mail: dev-help@ant.apache.org
> 


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


Mime
View raw message