Then have a look at what I did in the past two days to do something similar
;-) I created an <antreturn> task that piggybacks on <ant>, and allows
returning properties and/or references from the called build file back into
the caller's context (Project).

That would take care of that use case ;-) --DD

> -----Original Message-----
> From: Conor MacNeill [mailto:[EMAIL PROTECTED]
> Sent: Thursday, July 24, 2003 10:39 AM
> To: Ant Developers List
> Subject: Re: ant 1.5.4 : Import
> 
> On Fri, 25 Jul 2003 01:23 am, Dominique Devienne wrote:
> >
> > I (strongly again ;) believe that imported build files should be
> designed
> > to be imported, and never used without being imported.
> 
> I disagree (strongly :-). I think augmenting/overriding an existing build
> file
> is a valid use for import. I recently changed the checkstyle build I have
> been using (check.xml) to import Ant's build.xml to pick up property defs
> for
> various locations. Have a look.
> 
> Conor


<?xml version="1.0"?>

<project name="AntReturnTest" default="test-returnProperty">

  <!-- Our custom tasks we are testing -->
  <taskdef resource="com/lgc/buildmagic/tasks.properties" />
  <typedef resource="com/lgc/buildmagic/types.properties" />

  <target name="test-returnProperty">
    <antreturn antfile="AntReturnNestedTest.xml" target="set-properties">
      <return>
        <property name="propA" />
        <property name="propC" />
      </return>
    </antreturn>
  </target>

  <target name="test-returnReference">
    <antreturn antfile="AntReturnNestedTest.xml" target="set-references">
      <return>
        <reference refid="pathRef" />
        <reference refid="filesetRef" />
      </return>
    </antreturn>
  </target>

</project>

<?xml version="1.0"?>

<project name="AntReturnNestedTest" default="set-properties">

  <target name="set-properties">
    <property name="propA" value="a" />
    <property name="propB" value="bb" />
    <property name="propC" value="ccc" />
    <property name="propD" value="dddd" />
  </target>

  <target name="set-references">
    <path id="pathRef" location="${basedir}" />
    <fileset id="filesetRef" dir="${basedir}">
      <include name="AntReturn*Test.xml" />
    </fileset>
    <patternset id="patternsetRef">
      <include name="dummy" />
    </patternset>
  </target>

</project>

// vim:ts=2:sw=2
package com.lgc.buildmagic.test;

import org.apache.tools.ant.ProjectComponent;
import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.types.FileSet;

import com.lgc.buildmagic.util.BuildFileTestCase;

/**
 * Tests &lt;antreturn&gt;.
 *
 * @author <a href="mailto:[EMAIL PROTECTED]">Dominique Devienne</a>
 * @version Jul 2003 - Copyright (c) 2003, Landmark Graphics Corp.
 */
public class AntReturnTest
             extends BuildFileTestCase { 
 
  public void testReturnProperty() {
    executeTarget("test-returnProperty");

    assertProperty("propA", "a");
    assertProperty("propB", null);
    assertProperty("propC", "ccc");
    assertProperty("propD", null);
  }

  private void assertProperty(String name, String expectedValue) {
    String value = getProject().getProperty(name);
    assertEquals(name + " property", value, expectedValue);
  }

  public void testReturnReference() {
    executeTarget("test-returnReference");

    assertReference("pathRef", Path.class);
    assertReference("filesetRef", FileSet.class);
    assertReference("patternsetRef", null);
  }

  private void assertReference(String refid, Class expectedClass) {
    Object value = getProject().getReference(refid);
    if (expectedClass != null) {
      assertNotNull(refid + " reference", value);
      assertEquals(refid + " reference type", expectedClass,
value.getClass());
      assertSame(refid + " reference project", getProject(),
                 ((ProjectComponent)value).getProject());
    }
    else {
      assertEquals(refid + " reference found", false, value != null);
    }
  }

} // END class AntReturnTest 

// vim:ts=2:sw=2
package com.lgc.buildmagic;

import java.util.List;
import java.util.Iterator;
import java.util.ArrayList;
import java.util.Collections;

import org.apache.tools.ant.Project;
import org.apache.tools.ant.ProjectComponent;
import org.apache.tools.ant.BuildException;

import org.apache.tools.ant.types.Reference;

import org.apache.tools.ant.taskdefs.Ant;
import org.apache.tools.ant.taskdefs.Property;

/**
 * ...
 *
 * @author <a href="mailto:[EMAIL PROTECTED]">Dominique Devienne</a>
 * @version Jul 2003 - Copyright (c) 2003, Landmark Graphics Corp.
 *
 * @ant.task category="control"
 */
public class AntReturn
             extends Ant {

  private Project _newProject;
  private ReturnedElements _returns;

  public void init() {
    super.init();
    setInheritAll(false);
    setInheritRefs(false);
  }

  public void execute()
              throws BuildException {
    int returnCount = 0;
    if (_returns != null) {
      returnCount = _returns._references.size()
                  + _returns._properties.size();
    }

    if (returnCount > 0 && _newProject == null) {
      // Create dummy property to get at nested project
      Property p = createProperty();
      p.setName("sdliugyhspuygh.asdjhriawu120983472$376asdbfandbfa7347");
      p.setValue("");

      if (_newProject == null) {
        throw new BuildException("Cannot access nested Project BEFORE!!!");
      }
    }

    super.execute();

    if (returnCount < 1) {
      return; // Nothing to return
    }

    // Set the returned properties in the outer project
    for (Iterator i = _returns._properties.iterator(); i.hasNext();) {
      ReturnProperty retProperty = (ReturnProperty)i.next();
      String name = retProperty.getName();
      String value = _newProject.getProperty(name);
      if (value == null) {
        throw new BuildException("Cannot find property '" + name + "'");
      }
      getProject().setNewProperty(retProperty.getToName(), value);
    }

    // Set the returned properties in the outer project
    for (Iterator i = _returns._references.iterator(); i.hasNext();) {
      ReturnReference retReference = (ReturnReference)i.next();
      String refid = retReference.getRefid();
      Object value = _newProject.getReference(refid);
      if (value == null) {
        throw new BuildException("Cannot find reference '" + refid + "'");
      }
      if (value instanceof ProjectComponent) {
        ((ProjectComponent)value).setProject(getProject());
      }
      getProject().addReference(retReference.getToRefid(), value);
    }
  }

  /** Intercept property creation to catch the new project. Wicked! */
  public Property createProperty() {
    Property p = super.createProperty();
    _newProject = p.getProject();
    return p;
  }

  /** Adds the set of properties/references to return. */
  public void addReturn(ReturnedElements returns) {
    if (_returns != null) {
      throw new BuildException("Can have only one nested <return>");
    }
    _returns = returns;
  }

  /** Programatic convenience to select a property to return. */
  public void selectProperty(String name, String toName) {
    if (_returns == null) {
      addReturn(new ReturnedElements());
    }
    ReturnProperty p = new ReturnProperty();
    p.setName(name);
    if (toName != null) {
      p.setToName(toName);
    }
    _returns.addProperty(p);
  }

  /** Programatic convenience to select a reference to return. */
  public void selectReference(String refid, String toRefid) {
    if (_returns == null) {
      addReturn(new ReturnedElements());
    }
    ReturnReference r = new ReturnReference();
    r.setRefid(refid);
    if (toRefid != null) {
      r.setToRefid(toRefid);
    }
    _returns.addReference(r);
  }

  /** A property name to return from the called project. */
  public static class ReturnProperty {
    private String _name;
    private String _toName;

    public void setName(String name) {
      _name = TaskUtils.assertNotEmpty(name, "name", true);
    }
    public void setToName(String toName) {
      _toName = TaskUtils.assertNotEmpty(toName, "toName", true);
    }
    public String getName() {
      TaskUtils.assertAttributeSet(_name, "name");
      return _name;
    }
    public String getToName() {
      return (_toName == null)? getName(): _toName;
    }
  } // END class AntReturn.ReturnProperty

  /** A reference name to return from the called project. */
  public static class ReturnReference {
    private String _refid;
    private String _toRefid;

    public void setRefid(String refid) {
      _refid = TaskUtils.assertNotEmpty(refid, "refid", true);
    }
    public void setToRefid(String toRefid) {
      _toRefid = TaskUtils.assertNotEmpty(toRefid, "toRefid", true);
    }
    public String getRefid() {
      TaskUtils.assertAttributeSet(_refid, "refid");
      return _refid;
    }
    public String getToRefid() {
      return (_toRefid == null)? getRefid(): _toRefid;
    }
  } // END class AntReturn.ReturnReference

  /** List of properties and references to return from the called project.
*/
  public static class ReturnedElements {
    private List _properties = Collections.EMPTY_LIST;
    private List _references = Collections.EMPTY_LIST;

    public void addProperty(ReturnProperty retProperty) {
      if (_properties == Collections.EMPTY_LIST) {
        _properties = new ArrayList(4);
      }
      _properties.add(retProperty);
    }

    public void addReference(ReturnReference retReference) {
      if (_references == Collections.EMPTY_LIST) {
        _references = new ArrayList(4);
      }
      _references.add(retReference);
    }
  } // END class AntReturn.ReturnedElements

} // END class AntReturn


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to