ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Guillaume Binet" <guillau...@previewsystems.com>
Subject [PROPOSAL] AntCallOn : to call repetitively a specified target for each file composing the filesets you give to it
Date Sat, 06 Jan 2001 01:03:33 GMT
Hi everybody,

I got lock this week with Ant trying to execute something on a list of file
..
I found no way actually to decompose a FileSet and I had to go thru an
execon on a batch file that calls ant on the same build.xml (yuuk !).

example: put an ejb.properties in the root of each of your ejb bean, call
antcallon that will seek those files ant execute a the deploy target giving
it the path to the ejb.properties files that it found.

I give you here the documentation and the code I did (or merge from antcall
and execon).

Please tell me what you think about it, and sorry in advance if it is
completely irrelevant because I just joined this mailing list.

Guillaume BINET.


----------------------------------------------------------------------------
----

AntCallOn
Description
Call repetitively a target enumerating one by one the files contained in
filesets.

The files and/or directories of a number of FileSets are passed in a
specified property to the system command. At least one nested <fileset> is
required.

Parameters
      Attribute Description Required
      target the target you want to call Yes
      property the property in which you want to pass the current file Yes
      type One of file, dir or both. If set to file, only the names of plain
files will be sent to the command. If set to dir, only the names of
directories are considered. No, default is file

Parameters specified as nested elements
fileset
You can use any number of nested <fileset> elements to define the files for
this task and refer to <fileset>s defined elsewhere.

Examples
<antcallon target="deploy" property="currPropFile">
  <fileset dir="/sources">
    <patternset>
      <include name="**/ejb.properties" />
    </patternset>
  </fileset>
</execon>
<!-- ... -->
<target name="deploy">  <property file="${currPropFile}"/>  <!-- execute
something according to the given properties set in the current
ejb.properties --></target>

invokes deploy for all files below /sources that are named ebj.properties
adding the absolute filenames and passes it thru the property currPropFile.


----------------------------------------------------------------------------
----


package org.apache.tools.ant.taskdefs;
import org.apache.tools.ant.*;import org.apache.tools.ant.types.*;
import java.util.Vector;import java.io.File;import java.io.IOException;
public class AntCallOn extends Task{
   protected Vector filesets = new Vector();   protected String type =
"file";   private Ant callee;   private String subTarget;   private String
propertyToSet;
   /**    * Adds a set of files (nested fileset attribute).    */   public
void addFileset(FileSet set)   {      filesets.addElement(set);   }
   /**     * Shall the command work only on files, directories or both?
*/   public void setType(FileDirBoth type)   {      this.type =
type.getValue();   }
   // The setter for the "property" attribute   public void
setProperty(String propertyName)   {      this.propertyToSet =
opertyName;   }
   protected void checkConfiguration()   {      if( subTarget == null )
{         throw new BuildException("Attribute target is required.",
location);      }
      if( filesets.size() == 0 )      {         throw new BuildException("no
filesets specified", location);      }      if( subTarget == null )
{         throw new BuildException("Attribute target is required.",
location);      }
   }
   /**    * Enumerated attribute with the values "file", "dir" and "both"
* for the type attribute.      */   public static class FileDirBoth extends
EnumeratedAttribute   {      public String[] getValues()      {
return new String[] {"file", "dir", "both"};      }   }
   public void init()   {         }
   public void execute()   {                  checkConfiguration();
Vector v = new Vector();      for( int i=0; i<filesets.size(); i++ )
{         FileSet fs = (FileSet) filesets.elementAt(i);
DirectoryScanner ds = fs.getDirectoryScanner(project);

         if( !"dir".equals(type) )         {            String[] s =
ds.getIncludedFiles();            for( int j=0; j<s.length; j++ )
{               v.addElement(new File(fs.getDir(project),
s[j]).getAbsolutePath());            }         }

         if( !"file".equals(type) )         {            String[] s =
ds.getIncludedDirectories();            for( int j=0; j<s.length; j++ )
{               v.addElement(new File(fs.getDir(project),
s[j]).getAbsolutePath());            }         }      }
      String[] s = new String[v.size()];      v.copyInto(s);
      for( int i=0; i<s.length; i++ )      {
         project.setProperty(propertyToSet,s[i]);                  callee =
(Ant) project.createTask("ant");         callee.setOwningTarget(target);
callee.setTaskName(getTaskName());         callee.setLocation(location);
callee.init();callee.setDir(project.getBaseDir());
callee.setAntfile(project.getProperty("ant.file"));
callee.setTarget(subTarget);         callee.execute();      }        }
   public void setTarget(String target)   {      subTarget = target;   }}

----------------------------------------------------------------------------
----


Mime
View raw message