On Fri, 9 Nov 2001, Holger Engels wrote:
> On Fri, 9 Nov 2001, Jose Alberto Fernandez wrote:
>
> > From: "Stefan Bodewig" <[EMAIL PROTECTED]>
> >
> > > On Thu, 8 Nov 2001, Holger Engels <[EMAIL PROTECTED]> wrote:
> > >
> > > > Did you ever think about making the ant task a directory based task?
> > >
> > > Yes, we called it <anton> 8-)
> > >
> > > It's on the list of things to do.
> > >
> >
> > The point that I think is relevant is that it may be no need to add a new
> > task
> > we could just add a <antfileset> element to <ant> that can be used instead.
> >
> > This would not imply any backward compatibility issue or any other problem
> > as far as I can see and keep the number of different tasks to maintain low.
> >
>
I just implemented ant as a matching task with support for nested
filesets. Assuming, that the current directory contains sub1.xml and
sub2.xml, the following three blocks have the same effect:
<ant target="main">
<include name="sub*"/>
</ant>
<ant target="main">
<fileset dir=".">
<include name="sub1.xml"/>
</fileset>
</ant>
<ant antfile="sub1.xml" target="main"/>
<ant antfile="sub2.xml" target="main"/>
'dir' acts as the basedir for the matching task's fileset, if
(in|ex)cludes(file)? has been provided. 'antfile' or it's default
"build.xml" are added to the matching tasks includes if no other includes
/ excludes have been specified and no nested filesets do exist.
The task should be fully backwards compatible .. can you confirm that?
Please check the recurring initialization of 'newProject'. Do I
reinitialize to much / less?
Cheers,
Holger
Index: src/main/org/apache/tools/ant/taskdefs/Ant.java
===================================================================
RCS file:
/home/cvspublic/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Ant.java,v
retrieving revision 1.32
diff -u -r1.32 Ant.java
--- src/main/org/apache/tools/ant/taskdefs/Ant.java 2001/11/02 12:07:34
1.32
+++ src/main/org/apache/tools/ant/taskdefs/Ant.java 2001/11/09 10:26:48
@@ -60,12 +60,17 @@
import org.apache.tools.ant.DefaultLogger;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.ProjectHelper;
+import org.apache.tools.ant.DirectoryScanner;
+import org.apache.tools.ant.types.*;
import org.apache.tools.ant.util.FileUtils;
import java.io.File;
import java.io.PrintStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Vector;
+import java.util.Iterator;
+import java.util.List;
+import java.util.LinkedList;
import java.util.Hashtable;
import java.util.Enumeration;
@@ -88,7 +93,7 @@
*
* @author [EMAIL PROTECTED]
*/
-public class Ant extends Task {
+public class Ant extends MatchingTask {
/** the basedir where is executed the build file */
private File dir = null;
@@ -112,6 +117,40 @@
private Project newProject;
/**
+ * Vector to hold source file sets.
+ */
+ private Vector filesets = new Vector();
+
+ private boolean cludes = false;
+
+ public PatternSet.NameEntry createInclude() {
+ cludes = true;
+ return super.createInclude();
+ }
+
+ public PatternSet.NameEntry createIncludesFile() {
+ cludes = true;
+ return super.createIncludesFile();
+ }
+
+ public PatternSet.NameEntry createExclude() {
+ cludes = true;
+ return super.createExclude();
+ }
+
+ public PatternSet.NameEntry createExcludesFile() {
+ cludes = true;
+ return super.createExcludesFile();
+ }
+
+ /**
+ * Adds a set of files (nested fileset attribute).
+ */
+ public void addFileset(FileSet set) {
+ filesets.addElement(set);
+ }
+
+ /**
* If true, inherit all properties from parent Project
* If false, inherit only userProperties and those defined
* inside the ant call itself
@@ -239,59 +278,88 @@
*/
public void execute() throws BuildException {
try {
- if (newProject == null) {
- reinit();
- }
-
if ( (dir == null) && (inheritAll == true) ) {
dir = project.getBaseDir();
}
-
- initializeProject();
-
- if (dir != null) {
- newProject.setBaseDir(dir);
- newProject.setUserProperty("basedir" , dir.getAbsolutePath());
- } else {
- dir = project.getBaseDir();
- }
-
- // Override with local-defined properties
- Enumeration e = properties.elements();
- while (e.hasMoreElements()) {
- Property p=(Property) e.nextElement();
- p.setProject(newProject);
- p.execute();
- }
- if (antFile == null) {
- antFile = "build.xml";
- }
-
- File file = FileUtils.newFileUtils().resolveFile(dir, antFile);
- antFile = file.getAbsolutePath();
-
- newProject.setUserProperty( "ant.file" , antFile );
- ProjectHelper.configureProject(newProject, new File(antFile));
-
if (target == null) {
target = newProject.getDefaultTarget();
}
- // Are we trying to call the target in which we are defined?
- if (newProject.getBaseDir().equals(project.getBaseDir()) &&
-
newProject.getProperty("ant.file").equals(project.getProperty("ant.file")) &&
- getOwningTarget() != null &&
- target.equals(this.getOwningTarget().getName())) {
-
- throw new BuildException("ant task calling its own parent
target");
- }
+ if (getDirectoryScanners().size() == 1 && !cludes) {
+ if (antFile == null) {
+ antFile = "build.xml";
+ }
+ log("single file: " + antFile, Project.MSG_VERBOSE);
+ PatternSet.NameEntry entry = createInclude();
+ entry.setName(antFile);
+ }
+
+ Iterator scanners = getDirectoryScanners().iterator();
+ while (scanners.hasNext()) {
+ DirectoryScanner scanner = (DirectoryScanner)scanners.next();
+ log("basedir: " + scanner.getBasedir(), Project.MSG_VERBOSE);
+ String[] srcFiles = scanner.getIncludedFiles();
+ for (int j = 0; j < srcFiles.length; j++) {
+ File file =
FileUtils.newFileUtils().resolveFile(scanner.getBasedir(), srcFiles[j]);
+ log("file: " + file, Project.MSG_VERBOSE);
+ call(scanner.getBasedir(), file.getAbsolutePath());
+ }
+ }
+ }
+ finally {
+ // help the gc
+ newProject = null;
+ cludes = false;
+ filesets.clear();
+ }
+ }
- newProject.executeTarget(target);
- } finally {
- // help the gc
- newProject = null;
- }
+ protected void call(File dir, String antFile) {
+ reinit();
+ initializeProject();
+
+ if (dir != null) {
+ newProject.setBaseDir(dir);
+ newProject.setUserProperty("basedir" , dir.getAbsolutePath());
+ } else {
+ dir = project.getBaseDir();
+ }
+
+ // Override with local-defined properties
+ Enumeration e = properties.elements();
+ while (e.hasMoreElements()) {
+ Property p=(Property) e.nextElement();
+ p.setProject(newProject);
+ p.execute();
+ }
+
+ log("calling " + target + " in " + antFile, Project.MSG_VERBOSE);
+
+ newProject.setUserProperty("ant.file" , antFile);
+ ProjectHelper.configureProject(newProject, new File(antFile));
+
+ // Are we trying to call the target in which we are defined?
+ if (newProject.getBaseDir().equals(project.getBaseDir()) &&
+
newProject.getProperty("ant.file").equals(project.getProperty("ant.file")) &&
+ getOwningTarget() != null &&
+ target.equals(this.getOwningTarget().getName())) {
+
+ throw new BuildException("ant task calling its own parent target");
+ }
+ newProject.executeTarget(target);
+ }
+
+ public List getDirectoryScanners() {
+ List list = new LinkedList();
+ list.add(getDirectoryScanner(dir));
+
+ for (int i = 0; i < filesets.size(); i++) {
+ FileSet fs = (FileSet) filesets.elementAt(i);
+ DirectoryScanner ds = fs.getDirectoryScanner(project);
+ list.add(ds);
+ }
+ return list;
}
/**
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>