ant-dev mailing list archives

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

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

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

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

      <taskdef name="startserver"
         <loader ref="mytaskloader"/>

      <taskdef name="runservertests"
         <loader ref="mytaskloader"/>

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).


> -----Original Message-----
> From: Vincent Massol []
> Sent: Tuesday, 6 March 2001 4:42 AM
> To:
> 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:
> For additional commands, e-mail:

View raw message