You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by gl...@ca.ibm.com on 2000/07/15 23:08:18 UTC

[PATCH] Inheriting properties from a parent project (Project.java , ProjectHelper.java, Ant.java)




Hey all.

I have a _very_ large master project that needs to call literally hundreds
of subbuilds (and please don't ask why :-)  Every time the "ant" task is
called, it creates a new project and copies all of the "parent" project's
properties into it.  When I tried it I ran out of memory due to this
horrendous duplication of property values.

This patch introduces the concept of a parent project, and the use of
java.util.Properties to hold the property lists, rather than a Hashtable.
The use of the parent values should be extended furthur, but I only had
time to implement it for properties today.  The amount of time required to
set up subbuilds is drastically reduced, as is the memory requirements.
All the current rules for setting property values are still enforced - no
changes there.

Comments?

Index: jakarta-ant/src/main/org/apache/tools/ant/Project.java
===================================================================
RCS file:
/home/cvspublic/jakarta-ant/src/main/org/apache/tools/ant/Project.java,v
retrieving revision 1.31
diff -u -r1.31 Project.java
--- jakarta-ant/src/main/org/apache/tools/ant/Project.java
2000/07/12 11:51:29        1.31
+++ jakarta-ant/src/main/org/apache/tools/ant/Project.java
2000/07/15 20:54:28
@@ -94,8 +94,8 @@

     private String name;

-    private Hashtable properties = new Hashtable();
-    private Hashtable userProperties = new Hashtable();
+    private Properties properties;
+    private Properties userProperties;
     private Hashtable references = new Hashtable();
     private String defaultTarget;
     private Hashtable taskClassDefinitions = new Hashtable();
@@ -104,9 +104,19 @@
     private File baseDir;

     private Vector listeners = new Vector();
+
+    private Project parent = null;

     public Project() {
+        this.properties = new Properties();
+        this.userProperties = new Properties();
     }
+
+    public Project(Project parent) {
+        this.parent = parent;
+        this.properties = new Properties(parent.getProperties());
+        this.userProperties = new Properties(parent.getUserProperties());
+    }

     /**
      * Initialise the project.
@@ -115,39 +125,41 @@
      * system properties.
      */
     public void init() throws BuildException {
-        detectJavaVersion();
-
-        String defs =
"/org/apache/tools/ant/taskdefs/defaults.properties";
-
-        try {
-            Properties props = new Properties();
-            InputStream in = this.getClass().getResourceAsStream(defs);
-            props.load(in);
-            in.close();
-
-            Enumeration enum = props.propertyNames();
-            while (enum.hasMoreElements()) {
-                String key = (String) enum.nextElement();
-                String value = props.getProperty(key);
-                try {
-                    Class taskClass = Class.forName(value);
-                    addTaskDefinition(key, taskClass);
-                } catch (NoClassDefFoundError ncdfe) {
-                    // ignore...
-                } catch (ClassNotFoundException cnfe) {
-                    // ignore...
+        if (parent == null) {
+            detectJavaVersion();
+
+            String defs =
"/org/apache/tools/ant/taskdefs/defaults.properties";
+
+            try {
+                Properties props = new Properties();
+                InputStream in =
this.getClass().getResourceAsStream(defs);
+                props.load(in);
+                in.close();
+
+                Enumeration enum = props.propertyNames();
+                while (enum.hasMoreElements()) {
+                    String key = (String) enum.nextElement();
+                    String value = props.getProperty(key);
+                    try {
+                        Class taskClass = Class.forName(value);
+                        addTaskDefinition(key, taskClass);
+                    } catch (NoClassDefFoundError ncdfe) {
+                        // ignore...
+                    } catch (ClassNotFoundException cnfe) {
+                        // ignore...
+                    }
                 }
-            }

-            Properties systemP = System.getProperties();
-            Enumeration e = systemP.keys();
-            while (e.hasMoreElements()) {
-                String name = (String) e.nextElement();
-                String value = (String) systemP.get(name);
-                this.setProperty(name, value);
+                Properties systemP = System.getProperties();
+                Enumeration e = systemP.keys();
+                while (e.hasMoreElements()) {
+                    String name = (String) e.nextElement();
+                    String value = (String) systemP.get(name);
+                    this.setProperty(name, value);
+                }
+            } catch (IOException ioe) {
+                throw new BuildException("Can't load default task list");
             }
-        } catch (IOException ioe) {
-            throw new BuildException("Can't load default task list");
         }
     }

@@ -197,21 +209,21 @@

     public String getProperty(String name) {
         if (name == null) return null;
-        String property = (String) properties.get(name);
+        String property = properties.getProperty(name);
         return property;
     }

     public String getUserProperty(String name) {(name);;
         if (name == null) return null;
-        String property = (String) userProperties.get(name);
+        String property = userProperties.getProperty(name);
         return property;
     }

-    public Hashtable getProperties() {
+    public Properties getProperties() {
         return properties;
     }

-    public Hashtable getUserProperties() {
+    public Properties getUserProperties() {
         return userProperties;
     }

Index: jakarta-ant/src/main/org/apache/tools/ant/ProjectHelper.java
===================================================================
RCS file:
/home/cvspublic/jakarta-ant/src/main/org/apache/tools/ant/ProjectHelper.java,v

retrieving revision 1.20
diff -u -r1.20 ProjectHelper.java
--- jakarta-ant/src/main/org/apache/tools/ant/ProjectHelper.java
2000/07/13 15:23:03        1.20
+++ jakarta-ant/src/main/org/apache/tools/ant/ProjectHelper.java
2000/07/15 20:54:28
@@ -451,7 +451,7 @@

     /** Replace ${NAME} with the property value
      */
-    public static String replaceProperties( String value, Hashtable keys )
+    public static String replaceProperties( String value, Properties keys
)
         throws BuildException
     {
         // XXX use Map instead of proj, it's too heavy
@@ -480,8 +480,10 @@
                                              value );
                 }
                 String n=value.substring( pos+2, endName );
-                String v= (keys.containsKey(n)) ? (String) keys.get( n )
-                    : "${"+n+"}";
+                String v= keys.getProperty(n);
+                if (v == null) {
+                    v = "${" + n + "}";
+                }
                 //System.out.println("N: " + n + " " + " V:" + v);
                 sb.append( v );
                 prev=endName+1;

Index: jakarta-ant/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.10
diff -u -r1.10 Ant.java
--- jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Ant.java
2000/07/12 06:36:11        1.10
+++ jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Ant.java
2000/07/15 20:54:28
@@ -88,7 +88,7 @@
     Project p1;

     public void init() {
-        p1 = new Project();
+        p1 = new Project(project);
         Vector listeners = project.getBuildListeners();
         for (int i = 0; i < listeners.size(); i++) {();nt.java
             p1.addBuildListener((BuildListener)listeners.elementAt(i));
@@ -114,14 +114,6 @@
             p1.addTaskDefinition(taskName, taskClass);
         }

-        // set user-define properties
-        Hashtable prop1 = project.getProperties();
-        Enumeration e = prop1.keys();
-        while (e.hasMoreElements()) {
-            String arg = (String) e.nextElement();
-            String value = (String) prop1.get(arg);
-            p1.setProperty(arg, value);
-        }
     }

     /**

Glenn McAllister
TID - Software Developer - VisualAge for Java
IBM Toronto Lab, (416) 448-3805
"An approximate answer to the right question is better than the
right answer to the wrong question." - John W. Tukey



Re: [PATCH] Inheriting properties from a parent project (Project.java , ProjectHelper.java, Ant.java)

Posted by Stefan Bodewig <bo...@bost.de>.
>>>>> "g" == glennm  <gl...@ca.ibm.com> writes:

 g> This patch introduces the concept of a parent project, and the use
 g> of java.util.Properties to hold the property lists, rather than a
 g> Hashtable.

While I understand the need for the first I don't see why you've
introduced the second - especially since we want to move away from
String based properties.

Stefan