ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Conor MacNeill" <co...@cortexebusiness.com.au>
Subject RE: Custom Task and classloader problem ?
Date Tue, 06 Mar 2001 01:32:41 GMT
Vincent,

Indeed, this is a classloader problem, however it should not be that
unexpected. Since you are specifying a classpath in the taskdef, Ant must
use a classloader to load the class. Ant will use a separate classloader for
each taskdef, as it must since each taskdef can have a separate classpath
specified. Since your class StartServerTask is being loaded by two distinct
classloaders, it will be treated as a separate class despite being the same
code. This leads to your ClassCastException.

If you look at your code,
         Object o = project.createTask("startserver");
         System.out.println("class = " + o.getClass().getName());

         task = (StartServerTask)o;

createTask is not guaranteed to return anything except an instance of Task.
You cannot assume it is a particular class. If you want a particular class,
create that class and initialise it appropriately.

As I have said in a separate thread, the use of createTask by other tasks is
somewhat problematic. It is better, IMHO, to create common utility classes
that provides the service you want (I have an outstanding reply on this
thread).

If you did want this to work, we could extend the core syntax to capture
what you want

	<loader id="mytaskloader">
             <classpath>
                 <pathelement location="lib/j2eeunit-22-0.8.jar"/>
                 <pathelement path="${java.class.path}"/>
             </classpath>
	</loader>

      <taskdef name="startserver"
               classname="j2eeunit.ant.StartServerTask">
         <loader ref="mytaskloader"/>
      </taskdef>

      <taskdef name="runservertests"
               classname="j2eeunit.ant.RunServerTestsTask">
         <loader ref="mytaskloader"/>
      </taskdef>

Alternatively, we could try to infer that information by sharing loaders in
the taskdefs, based on common classpaths. That would be workable but the
above change allows the user to show their real intent (i.e. the loader
should be shared across the taskdefs).

Conor


> -----Original Message-----
> From: Vincent Massol [mailto:vmassol@octo.fr]
> Sent: Tuesday, 6 March 2001 4:42 AM
> To: ant-dev@jakarta.apache.org
> Subject: Custom Task and classloader problem ?
>
>
> Hi,
>
> I have 2 custom tasks defined in my own jar file (package name :
> j2eeunit.ant). I try to call one of my task from the other task.
>
> Here is my task definition in my build.xml
>
>         <taskdef name="startserver"
> classname="j2eeunit.ant.StartServerTask">
>             <classpath>
>                 <pathelement location="lib/j2eeunit-22-0.8.jar"/>
>                 <pathelement path="${java.class.path}"/>
>             </classpath>
>         </taskdef>
>
>         <taskdef name="runservertests"
> classname="j2eeunit.ant.RunServerTestsTask">
>             <classpath>
>                 <pathelement location="lib/j2eeunit-22-0.8.jar"/>
>                 <pathelement path="${java.class.path}"/>
>             </classpath>
>         </taskdef>
>
> Here is the portion of code that I use to call the other task :
>
>         StartServerTask task;
>
>         Object o = project.createTask("startserver");
>         System.out.println("class = " + o.getClass().getName());
>
>         task = (StartServerTask)o;
>
> It fails with a class cast exception .... !
>
> Now, if I bundle my tasks in ant.jar and I removed the taskdefs,
> everything
> works fine. It seems it is a classloader problem ...
> Is there a way to solve this ? It seems that the Taskdef task uses the
> AntClassLoader class to load tasks. Is that the problem ? How can I remedy
> it ?
>
> Thanks.
> Vincent Massol.
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: ant-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: ant-dev-help@jakarta.apache.org
>
>


Mime
View raw message