[Patch] adding 'inheritAll' property to gant's ant task.

View: New views
1 Messages — Rating Filter:   Alert me  

[Patch] adding 'inheritAll' property to gant's ant task.

by Eric Van Dewoestine :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello all,

I've just recently started working with gant and converting my
existing ant scripts to it.  One of my goals in the conversion is to
minimize the impact on my users, so I've opted to use the gant ant
task so that my users could continue to use ant to build my project
and my ant build.xml would simply delegate to an underlying
build.gant.

One issue that I quickly ran into though was there didn't appear to be
a way to make the properties defined in the build.xml (or supplied via
-D args to ant) visible in the build.gant file (short of adding a
bunch of definition elements which in some cases could introduce a
different set of issues).  So, I created the attached patch to remedy
this by defining the 'inheritAll' property which can be used like so:

<gant file="src/ant/build.gant" target="${target}" inheritAll="true"/>

I'm not sure if there is an existing way to accomplish this in gant
(please let me know if there is), but I figured other people may find
this functionality useful.

--
eric

--- ./src/main/groovy/org/codehaus/gant/ant/Gant.java.orig 2009-11-04 11:09:24.820960977 -0800
+++ ./src/main/groovy/org/codehaus/gant/ant/Gant.java 2009-11-04 11:17:39.647569322 -0800
@@ -17,6 +17,8 @@
 import java.io.File ;
 
 import java.util.ArrayList ;
+import java.util.Enumeration;
+import java.util.Hashtable;
 import java.util.HashMap ;
 import java.util.List ;
 import java.util.Map ;
@@ -24,6 +26,7 @@
 import org.apache.tools.ant.AntClassLoader ;
 import org.apache.tools.ant.BuildException ;
 import org.apache.tools.ant.BuildListener ;
+import org.apache.tools.ant.MagicNames ;
 import org.apache.tools.ant.Project ;
 import org.apache.tools.ant.Task ;
 
@@ -61,6 +64,8 @@
    *  if the basedir is not set.
    */
   private String file = "build.gant" ;
+  /** should we inherit properties from the parent ? */
+  private boolean inheritAll ;
   /**
    *  A class representing a nested definition tag.
    */
@@ -96,6 +101,12 @@
    */
   public void setFile ( final String f ) { file = f ; }
   /**
+   * If true, pass all properties to the new Ant project.
+   * Defaults to true.
+   * @param value if true pass all properties to the new Ant project.
+   */
+  public void setInheritAll ( boolean value ) { inheritAll = value ; }
+  /**
    *  Set the target to be achieved.
    *
    *  @param t The target to achieve.
@@ -156,6 +167,9 @@
     //  Deal with GANT-50 by getting the base directory from the Ant instance Project object and use it for
     //  the new Project object.  GANT-93 leads to change in the way the Gant file is extracted.
     newProject.setBaseDir ( antProject.getBaseDir ( ) ) ;
+    if (inheritAll){
+      addAlmostAll ( newProject, antProject ) ;
+    }
     final File gantFile = newProject.resolveFile( file ) ;
     if ( ! gantFile.exists ( ) ) { throw new BuildException ( "Gantfile does not exist." , getLocation ( ) ) ; }
     final GantBuilder ant = new GantBuilder ( newProject ) ;
@@ -177,4 +191,30 @@
     final int returnCode =  gant.processTargets ( targetsAsStrings ) ;
     if ( returnCode != 0 ) { throw new BuildException ( "Gant execution failed with return code " + returnCode + '.' , getLocation ( ) ) ; }
   }
+  /**
+   * Almost a direct copy from org.apache.tools.ant.taskdefs.Ant source.
+   * Copies all properties from the given table to the new project -
+   * omitting those that have already been set in the new project as
+   * well as properties named basedir or ant.file.
+   * @param props properties <code>Hashtable</code> to copy to the
+   * new project.
+   */
+  private void addAlmostAll(Project newProject, Project antProject) {
+    Hashtable props = antProject.getProperties();
+    Enumeration e = props.keys();
+    while (e.hasMoreElements()) {
+      String key = e.nextElement().toString();
+      if (MagicNames.PROJECT_BASEDIR.equals(key) || MagicNames.ANT_FILE.equals(key)) {
+        // basedir and ant.file get special treatment in execute()
+        continue;
+      }
+
+      String value = props.get(key).toString();
+      // don't re-set user properties, avoid the warning message
+      if (newProject.getProperty(key) == null) {
+        // no user property
+        newProject.setNewProperty(key, value);
+      }
+    }
+  }
 }


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email