From: "Dominique Devienne" <[EMAIL PROTECTED]>
> Please submit here. Even it gets turned down by Ant, I most likely want
your
> patch (and I don't monitor Maven). Thanks, --DD

Here you go. This patch adds exportRefs="true|false" and
exportAll="true|false" to be the opposite of inheritRefs and inheritAll.
This new feature will never overwrite anything, only add new things not
already defined in the parent project.

James
Index: src/main/org/apache/tools/ant/taskdefs/Ant.java
===================================================================
RCS file: 
/home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Ant.java,v
retrieving revision 1.56
diff -u -r1.56 Ant.java
--- src/main/org/apache/tools/ant/taskdefs/Ant.java     22 Apr 2002 13:43:52 
-0000      1.56
+++ src/main/org/apache/tools/ant/taskdefs/Ant.java     23 May 2002 22:45:51 
-0000
@@ -117,6 +117,12 @@
     /** should we inherit references from the parent ? */
     private boolean inheritRefs = false;
 
+    /** should we export all properties to the parent ? */
+    private boolean exportAll = false;
+    
+    /** should we export references to the parent ? */
+    private boolean exportRefs = false;
+        
     /** the properties to pass to the new project */
     private Vector properties = new Vector();
 
@@ -148,6 +154,28 @@
     }
 
     /**
+     * If true, all properties defined in the child Project
+     * are exported to the parent Project so long as they are not
+     * already defined. No properties will be overwritten.
+     * If false, properties are not exported to the parent project
+     */
+    public void setExportAll(boolean value) {
+        exportAll = value;
+    }
+
+    /**
+     * If true, all references will be exported from the child Project
+     * into the parent Project after the child project has been executed
+     * providing they don't already exist in the parent Project.
+     * No references will ever be overwritten.
+     * If false, references are not exported to the parent project
+     */
+    public void setExportRefs(boolean value) {
+        exportRefs = value;
+    }
+
+
+    /**
      * Creates a Project instance for the project to call.
      */
     public void init() {
@@ -355,6 +383,10 @@
             }
 
             newProject.executeTarget(target);
+            
+            
+            exportProperties();
+            exportReferences();
         } finally {
             // help the gc
             newProject = null;
@@ -441,6 +473,10 @@
      */
     private void copyReference(String oldKey, String newKey) {
         Object orig = project.getReference(oldKey);
+        
+        // #### could replace the rest of this method with the next line I 
think
+        // copyReference(orig, newKey, newProject);
+        
         Class c = orig.getClass();
         Object copy = orig;
         try {
@@ -473,6 +509,123 @@
         }
         newProject.addReference(newKey, copy);
     }
+
+    /**
+     * Try to clone the given reference .
+     *
+     * <p>If we cannot clone it, lets reuse the existing 
+     * reference and keep our fingers crossed.</p>
+     * 
+     * @param orig is the reference to be cloned
+     * @param key is the key for the newly created reference
+     * @param aProject is the project to add the new reference to
+     * @return a new cloned reference, ready for adding to a different project
+     */
+    private void copyReference(Object orig, String key, Project aProject)
+    {
+        Object copy = orig;
+        Class c = orig.getClass();
+        try
+        {
+            Method cloneM = c.getMethod("clone", new Class[0]);
+            if (cloneM != null)
+            {
+                copy = cloneM.invoke(orig, new Object[0]);
+            }
+        }
+        catch (Exception e)
+        {
+            // not Clonable
+        }
+
+
+        if (copy instanceof ProjectComponent)
+        {
+            ((ProjectComponent) copy).setProject(aProject);
+        }
+        else
+        {
+            try
+            {
+                Method setProjectM =
+                    c.getMethod("setProject", new Class[] {Project.class});
+                if (setProjectM != null)
+                {
+                    setProjectM.invoke(copy, new Object[] {aProject});
+                }
+            }
+            catch (NoSuchMethodException e)
+            {
+                // ignore this if the class being referenced does not have
+                // a set project method.
+            }
+            catch (Exception e2)
+            {
+                String msg = "Error setting new project instance for "
+                    + "reference with new id " + key;
+                throw new BuildException(msg, e2, location);
+            }
+        }
+        aProject.addReference(key, copy);
+    }
+
+    /**
+     * Export any references defined in the child project to the parent
+     * Project providing that they don't already exist in the parent
+     * project.
+     */
+    private void exportReferences() throws BuildException {
+        if (exportRefs) {
+            Hashtable thisReferences = (Hashtable) 
project.getReferences().clone();
+            Hashtable newReferences = newProject.getReferences();
+            
+            for (Enumeration e = newReferences.keys(); e.hasMoreElements(); )
+            {
+                String refid = (String) e.nextElement();
+                if (refid != null)
+                {
+                    // do not overwrite any existing references
+                    if (!thisReferences.containsKey(refid)) 
+                    {
+                        Object reference = newReferences.get(refid);
+                        
+                        log("exporting reference for id: " + refid, 
Project.MSG_VERBOSE);
+                        
+                        copyReference(reference, refid, project);
+                    }
+                }
+            }
+        }
+    }
+    
+
+    /**
+     * Export any properties defined in the child project to the parent
+     * Project providing that they don't already exist in the parent
+     * project.
+     */
+    private void exportProperties() throws BuildException {
+        if (exportAll) {
+            Hashtable thisProperties = (Hashtable) 
project.getProperties().clone();
+            Hashtable newProperties = newProject.getProperties();
+            
+            for (Enumeration e = newProperties.keys(); e.hasMoreElements(); )
+            {
+                String key = (String) e.nextElement();
+                if (key != null)
+                {
+                    // do not overwrite any existing properties
+                    if (!thisProperties.containsKey(key)) 
+                    {
+                        log("exporting property for key: " + key, 
Project.MSG_VERBOSE);
+                        
+                        project.setProperty(key, newProject.getProperty(key));
+                    }
+                }
+            }
+        }
+    }
+
 
     /**
      * Set the dir attribute.

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

Reply via email to