You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by Mark McKay <ma...@kitfox.com> on 2004/01/21 09:44:32 UTC

Help creating a MatchingTask

I'm trying to create a custom ant task that I can call from my build 
scripts.  Right now I'm just trying to figure out how to write a task 
that can accept a fileset and iterate through all valid files.  My best 
guess so far is below.

Unfortunately, this is giving me errors that I have not encountered in 
Java before.  What does 'cannot be applied to ()' mean?  Could anyone 
suggest what I could do to fix my program?

Thanks.

Mark McKay


=========================

Error:

com/kitfox/anttask/MyAntTask.java [26:1] 
getDirectoryScanner(org.apache.tools.ant.Project) in 
org.apache.tools.ant.types.AbstractFileSet cannot be applied to ()
        DirectoryScanner scanner = fs.getDirectoryScanner();
                                     ^
1 error
Errors compiling MyAntTask.


=========================


package com.kitfox.anttask;

import org.apache.tools.ant.*;
import org.apache.tools.ant.taskdefs.*;
import org.apache.tools.ant.types.*;

/**
 *
 * @author  kitfox
 */
public class MyAntTask extends MatchingTask {
   
    /** Creates a new instance of MyAntTask */
    public MyAntTask() {
    }
   
    public void execute() throws BuildException
    {
        FileSet fs = getImplicitFileSet();
        DirectoryScanner scanner = fs.getDirectoryScanner();
       
        scanner.scan();
       
        String[] files = scanner.getIncludedFiles();
        for (int i = 0; i < files.length; i++) {
            System.out.println(files[i]);
        }
    }
   
}



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


RE: Help creating a MatchingTask

Posted by Jim Fuller <ji...@ruminate.co.uk>.
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


Re: Help creating a MatchingTask

Posted by Mark McKay <ma...@kitfox.com>.
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


Re: Help creating a MatchingTask

Posted by Conor MacNeill <co...@cortexebusiness.com.au>.
On Wed, 21 Jan 2004 07:44 pm, Mark McKay wrote:
> I'm trying to create a custom ant task that I can call from my build
> scripts.  Right now I'm just trying to figure out how to write a task
> that can accept a fileset and iterate through all valid files.  My best
> guess so far is below.
>

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



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


Re: Help creating a MatchingTask

Posted by Peter Reilly <pe...@corvil.com>.
Mark McKay wrote:

>
> I'm trying to create a custom ant task that I can call from my build 
> scripts.  Right now I'm just trying to figure out how to write a task 
> that can accept a fileset and iterate through all valid files.  My 
> best guess so far is below.
>
> Unfortunately, this is giving me errors that I have not encountered in 
> Java before.  What does 'cannot be applied to ()' mean?  Could anyone 
> suggest what I could do to fix my program?
>
> Thanks.
>
> Mark McKay
>
>
> =========================
>
> Error:
>
> com/kitfox/anttask/MyAntTask.java [26:1] 
> getDirectoryScanner(org.apache.tools.ant.Project) in 
> org.apache.tools.ant.types.AbstractFileSet cannot be applied to ()
>        DirectoryScanner scanner = fs.getDirectoryScanner();

You need to do:
  DirectoryScanner scanner = fs.getDirectoryScanner(getProject());

Peter

>                                     ^
> 1 error
> Errors compiling MyAntTask.
>
>
> =========================
>
>
> package com.kitfox.anttask;
>
> import org.apache.tools.ant.*;
> import org.apache.tools.ant.taskdefs.*;
> import org.apache.tools.ant.types.*;
>
> /**
> *
> * @author  kitfox
> */
> public class MyAntTask extends MatchingTask {
>      /** Creates a new instance of MyAntTask */
>    public MyAntTask() {
>    }
>      public void execute() throws BuildException
>    {
>        FileSet fs = getImplicitFileSet();
>        DirectoryScanner scanner = fs.getDirectoryScanner();
>              scanner.scan();
>              String[] files = scanner.getIncludedFiles();
>        for (int i = 0; i < files.length; i++) {
>            System.out.println(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