You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by Dominique Devienne <DD...@lgc.com> on 2002/05/23 18:50:28 UTC

RE: [PATCH] Definer to allow types/tasks from the same jar to wor k

+1 to the concept for ant 1.5. How do achieve it (this code or that one), I
don't really care ;-) --DD

-----Original Message-----
From: costinm@covalent.net [mailto:costinm@covalent.net] 
Sent: Thursday, May 23, 2002 11:43 AM
To: Ant Developers List
Subject: [PATCH] Definer to allow types/tasks from the same jar to work

This patch is essential for me ( in jakarta-tomcat-connectors project, I 
suspect other jakarta projects are affected as well ). I'm willing to make 
changes - but I need this functionality in ant1.5 in a form or another.
For a use-case, look at the cpp-tasks on sourceforge or any other 
user-tasks that uses both types and tasks.

In ant1.4, this only works if the code is in the CLASSPATH ( or 
ant/lib ). It is possible to load multiple tasks that will work togheter,
or multiple types - but it is not possible to have the tasks ant types.
This is because different class loaders are used. A <taskdef> can load
multiple tasks with the same loader if file or resource attribute is 
used. 

With this fix, we allow the user to specify that he wants different
<taskdefs> and <typedefs> to use the same classloader instance, so the
tasks/types can use each other. It is backward and forward compatible - 
unless the user explicitely specify he wants the new behavior, the
old behavior will be used with absolutely no change.

It is possible to use the same build.xml with both ant1.4 and ant1.5,
as long as in 1.4 you put the .jar in ant/lib ( that's the
only solution in 1.4 to solve this problem ). Asking for loader reuse
can also be done with an explicit attribute.

Costin

Index: taskdefs/Definer.java
===================================================================
RCS file:
/home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Definer.java,v
retrieving revision 1.15
diff -u -r1.15 Definer.java
--- taskdefs/Definer.java	18 Apr 2002 06:54:54 -0000	1.15
+++ taskdefs/Definer.java	23 May 2002 16:36:33 -0000
@@ -84,7 +84,9 @@
     private File file;
     private String resource;
     private boolean reverseLoader = false;
-
+    private String reuseLoader=null;
+    private String reference;
+    
     public void setReverseLoader(boolean reverseLoader) {
         this.reverseLoader = reverseLoader;
         log("The reverseloader attribute is DEPRECATED. It will be
removed", 
@@ -112,9 +114,18 @@
     }
 
     public void setClasspathRef(Reference r) {
+        reference=r.getRefId();
         createClasspath().setRefid(r);
     }
 
+    /** Allow multiple taskdef/typedef to use the same class loader,
+     *  so they can be used togheter. This eliminate the need to
+     *  put them in the CLASSPATH.
+     */
+    public void setReuseLoader( String s ) {
+        reuseLoader=s;
+    }
+    
     public void execute() throws BuildException {
         AntClassLoader al = createLoader();
 
@@ -205,8 +216,27 @@
         }
     }
 
-
+    static final String REUSE_LOADER_REF="ant.reuse.loader";
+    
     private AntClassLoader createLoader() {
+        if( project.getProperty( REUSE_LOADER_REF ) != null ) {
+            // Generate the 'reuse' name automatically from the reference.
+            // This allows <taskdefs> that work on both ant1.4 and ant1.5.
+            // ( in 1.4 it'll require the task/type to be in classpath if
they
+            //   are used togheter ).
+            if( reference!=null ) {
+                reuseLoader="ant.loader." + reference;
+            }
+        }
+        if( reuseLoader != null ) {
+            // We could use a local hashtable - but the references are
cleaner.
+            Object reusedLoader=project.getReference( reuseLoader );
+            if( reusedLoader != null &&
+                reusedLoader instanceof AntClassLoader ) {
+                return (AntClassLoader)reusedLoader;
+            }
+        } 
+       
         AntClassLoader al = null;
         if (classpath != null) {
             al = new AntClassLoader(project, classpath, !reverseLoader);
@@ -218,6 +248,13 @@
         // task we want to define will never be a Task but always
         // be wrapped into a TaskAdapter.
         al.addSystemPackageRoot("org.apache.tools.ant");
+
+
+        if( reuseLoader != null ) {
+            if( project.getReference( reuseLoader ) == null ) 
+                project.addReference( reuseLoader, al );
+        }
+
         return al;
     }
 




--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>