You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by todd runstein <to...@gmail.com> on 2007/11/20 21:45:13 UTC

How do I set a classpath for my custom task?

I'm writing an ant task that calls a Java class named Engine.  Engine
is in a jar file that is included in my taskdef using the classpath
tag.  This all works great.

The problem I'm having is that one of Engine's methods calls
"Class.forName()", trying to dynamically add a class file that is not
in it's jar file.  Here I'm getting a ClassNotFoundException.  I've
tried a few things to manually set the classpath that Ant uses, but
I'm just hacking aimlessly and have had no luck.  Could someone give
me some direction to manually adding to the classpath that Engine will
ultimately work with?

Thanks in advance!

Todd

-- 


----------------------------------------------------------------------
The only "dumb question" is the one you were too afraid to ask.
________________________________________
Check out RouteRuler - Free software for runners, cyclists, walkers, etc.
http://routeruler.sourceforge.net
________________________________________

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Re: How do I set a classpath for my custom task?

Posted by Gilles Scokart <gs...@gmail.com>.
You don't need to create a separate VM.  You just need to create a
classloader.

Basically, if you want your code to be independant of ant, it will be
something like that :

URLClassLoader userClassLoader = new URLClassLoader(userPath,
TheInterface.class.getClassLoader());
Class userClass = Class.forName(userClassName,true,userClassLoader);
TheInterface userObject = userClass.newInstance()

But you maybe should take into account a loaderref to allow multiple
execution of your task without having to recreate every time a new
ClassLoader (this is something rather heavy)

Gilles

2007/11/21, todd runstein <to...@gmail.com>:
>
> Gilles,
>
> The user is providing implementations of an interface I provide.  Then
> my code (which is called from my ant task) calls interface methods.
> So I'm not using reflection to call the method, just to get an
> instance of the users class.
>
> Can you point me to any code that shows how to "create a classloader
> that is a child of the classloader in which the interface class is
> loaded"?  I'm assuming I need to actually launch my application in a
> new VM using the classloader I create - any pointers to code that does
> demonstrates that would be appreciated as well!
>
> Todd
>
> On Nov 21, 2007 12:00 PM, Gilles Scokart <gs...@gmail.com> wrote:
> > That depends on how you want to invoke the code of the user.
> > If you are using only reflection to creates the object, but also to
> invoke
> > the method.  The, it is easier.  You only have to create a classloader
> > defined with the user classpath.
> > If you are only using reflection to create an instance o the user
> object,
> > then you are using interface implemented by the user object.  Then it is
> a
> > little bit more complex.  You need to create a classloader that is a
> child
> > of the classloader in which the interface class is loaded.  And your own
> > code must also be loaded in a classloader that is the child of the
> > classloader in which the interface class is loaded (or the same).
> >
> > Gilles
> >
> > 2007/11/21, todd runstein <to...@gmail.com>:
> >
> > >
> > > Ramu,
> > >
> > > Thanks for the suggestions.  I've looked at both of these options, but
> > > since this is a custom task that I'd like to include with an open
> > > source project, neither of those options are ideal.  In the "taskdef",
> > > I'd like the user of my task to only have to specify where my jar file
> > > is.  However, because my task uses classes written by the user, I'd
> > > like them to define that classpath in the task itself.
> > >
> > > To be honest, I may not completely understand the entire problem yet.
> > > Let me explain what I *think* the problem is, and hopefully someone
> > > can tell me how to fix it.
> > >
> > > Here is the taskdef that I would ask users to include in their build
> > > script:
> > >
> > > <!-- classpathref needs to include migrate4j.jar so ant can find
> AntTask
> > > -->
> > > <taskdef name="migrate" classname="com.eroi.migrate.migration.AntTask"
> > > classpathref="lib.classpath" />
> > >
> > > Then, here's the actual task in use:
> > >
> > > <!-- The migrate task needs to know where user supplied classes are
> -->
> > > <target name="run" depends="compile" >
> > >         <migrate
> > >                 version="1"
> > >                 url="jdbc:h2:~/testH2"
> > >                 driver="org.h2.Driver"
> > >                 username="sa"
> > >                 password=""
> > >                 packagename="db.migrations"
> > >                 classpathref="user.classpath"
> > >         />
> > > </target>
> > >
> > > Not to beat a dead horse, but the taskdef needs to find my code and my
> > > code needs to find users code.  I think that the classpath in the
> > > taskdef is being used to find my task (since the task loads properly),
> > > but when code in my task runs, it is too late to change the classpath
> > > so my code can't find the users classes.  I don't want the user to do
> > > anything they wouldn't do with core ant tasks (except for the taskdef
> > > entry).  I've started looking at the Java, Javac and JUnit tasks to
> > > see how they do this, but it's not immediately clear how to replicate
> > > their behavior.
> > >
> > > I'm now looking at the Commandline object to see how that might work
> > > with Execute.  If anyone has experience using these classes, I welcome
> > > any help!  I may be about to run into a brick wall, so if I'm
> > > obviously going down the wrong path, please let me know.
> > >
> > > Todd
> > >
> > >
> > > On Nov 20, 2007 8:43 PM, Ramu Sethu <sr...@yahoo.co.in> wrote:
> > > > HI todd
> > > >
> > > > Have you tried the following option ??
> > > > 1. -lib option
> > > > 2. classpath attribute in taskdef
> > > >
> > > >
> > > >
> > > > On Nov 21, 2007 2:15 AM, todd runstein <to...@gmail.com> wrote:
> > > >
> > > > > I'm writing an ant task that calls a Java class named
> Engine.  Engine
> > > > > is in a jar file that is included in my taskdef using the
> classpath
> > > > > tag.  This all works great.
> > > > >
> > > > > The problem I'm having is that one of Engine's methods calls
> > > > > "Class.forName()", trying to dynamically add a class file that is
> not
> > > > > in it's jar file.  Here I'm getting a
> ClassNotFoundException.  I've
> > > > > tried a few things to manually set the classpath that Ant uses,
> but
> > > > > I'm just hacking aimlessly and have had no luck.  Could someone
> give
> > > > > me some direction to manually adding to the classpath that Engine
> will
> > > > > ultimately work with?
> > > > >
> > > > > Thanks in advance!
> > > > >
> > > > > Todd
> > > > >
> > > > > --
> > > > >
> > > > >
> > > > >
> ----------------------------------------------------------------------
> > > > > The only "dumb question" is the one you were too afraid to ask.
> > > > > ________________________________________
> > > > > Check out RouteRuler - Free software for runners, cyclists,
> walkers,
> > > etc.
> > > > > http://routeruler.sourceforge.net
> > > > > ________________________________________
> > > > >
> > > > >
> ---------------------------------------------------------------------
> > > > > To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> > > > > For additional commands, e-mail: user-help@ant.apache.org
> > > > >
> > > > >
> > > >
> > > >
> > > > --
> > > > Thank you
> > > > Ramu S
> > > >
> > > >  If A is success in life, then A equals x plus y plus z. Work is x;
> y is
> > > > play; and z is keeping your mouth shut.
> > > > - Albert Einstein
> > > >
> > >
> > >
> > >
> > > --
> > >
> > >
> > > ----------------------------------------------------------------------
> > > The only "dumb question" is the one you were too afraid to ask.
> > > ________________________________________
> > > Check out RouteRuler - Free software for runners, cyclists, walkers,
> etc.
> > > http://routeruler.sourceforge.net
> > > ________________________________________
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> > > For additional commands, e-mail: user-help@ant.apache.org
> > >
> > >
> >
> >
> > --
> > Gilles Scokart
> >
>
>
>
> --
>
>
> ----------------------------------------------------------------------
> The only "dumb question" is the one you were too afraid to ask.
> ________________________________________
> Check out RouteRuler - Free software for runners, cyclists, walkers, etc.
> http://routeruler.sourceforge.net
> ________________________________________
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org
>
>


-- 
Gilles SCOKART

AW: How do I set a classpath for my custom task?

Posted by Rainer Noack <ra...@noacks.net>.
Hi Todd,

the problem is your call to Class.forName(String) in ...Engine.
This uses Engine's Classloader.

There are the following ways to add user-defined classes:

1. let the user add them to the classpath of the task's classloader or a one
of it's parents by
1.1. specify them in <taskdef>'s classpath-attribute of the migrate-task or
1.2. add them via something like the <classloader> task (see
http://enitsys.sourceforge.net/ant-classloadertask/)

or (IMHO better)

2. refactor the Engine class:
All methods that use Class.forName(String) should get an parameter
"ClassLoader classloader" and you should
use Class.forName(String,boolean,ClassLoader) instead.
The methods with the "old" signature should be redirected to the new methods
with classloader=Engine.class.getClassLoader().
In your task you have to create a new Classloader:
if cp is your resolved classpathref, use new
AntClassLoader(getClass().getClassLoader(),getProject(),cp) to construct the
new classpath.

cheers

Rainer Noack


-----Ursprüngliche Nachricht-----
Von: todd runstein [mailto:toddrun@gmail.com] 
Gesendet: Mittwoch, 21. November 2007 23:26
An: Ant Users List
Betreff: Re: How do I set a classpath for my custom task?

The project is non platform specific, so it can run in a container, in
a swing app, or from the command line.  The project applies schema
changes to a database.  The user creates a class that implements a
single interface, adding code that the project uses to apply the
changes.  I've currently got this working from the command line and in
a servlet (in it's init method).  I really want to create a simple to
use ant task so schema changes can be applied prior to deployment,
test runs, or whatever, but I can't get this classloader thing figured
out.

Not sure if it's helpful, but if you need more specifics about what
I'm trying to do, the project is on sourceforge under the name
migrate4j.  If there's more info that I can provide, just let me know.

Todd

On Nov 21, 2007 1:40 PM, Martin Gainty <mg...@hotmail.com> wrote:
>
> Hi Todd-Could you provide a bit more contextWill you be implementing this
(web)application under J2EE server -or- perhaps a container such as Catalina
or Jetty?Generally these environments support their own classloader loading
algorithms depending on the capability and security characteristics of the
(web)application you will be implementing and which J2EE server and
container will be the implementor-Martin


-- 


----------------------------------------------------------------------
The only "dumb question" is the one you were too afraid to ask.
________________________________________
Check out RouteRuler - Free software for runners, cyclists, walkers, etc.
http://routeruler.sourceforge.net
________________________________________

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Re: How do I set a classpath for my custom task?

Posted by todd runstein <to...@gmail.com>.
The project is non platform specific, so it can run in a container, in
a swing app, or from the command line.  The project applies schema
changes to a database.  The user creates a class that implements a
single interface, adding code that the project uses to apply the
changes.  I've currently got this working from the command line and in
a servlet (in it's init method).  I really want to create a simple to
use ant task so schema changes can be applied prior to deployment,
test runs, or whatever, but I can't get this classloader thing figured
out.

Not sure if it's helpful, but if you need more specifics about what
I'm trying to do, the project is on sourceforge under the name
migrate4j.  If there's more info that I can provide, just let me know.

Todd

On Nov 21, 2007 1:40 PM, Martin Gainty <mg...@hotmail.com> wrote:
>
> Hi Todd-Could you provide a bit more contextWill you be implementing this (web)application under J2EE server -or- perhaps a container such as Catalina or Jetty?Generally these environments support their own classloader loading algorithms depending on the capability and security characteristics of the (web)application you will be implementing and which J2EE server and container will be the implementor-Martin


-- 


----------------------------------------------------------------------
The only "dumb question" is the one you were too afraid to ask.
________________________________________
Check out RouteRuler - Free software for runners, cyclists, walkers, etc.
http://routeruler.sourceforge.net
________________________________________

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


RE: How do I set a classpath for my custom task?

Posted by Martin Gainty <mg...@hotmail.com>.
Hi Todd-Could you provide a bit more contextWill you be implementing this (web)application under J2EE server -or- perhaps a container such as Catalina or Jetty?Generally these environments support their own classloader loading algorithms depending on the capability and security characteristics of the (web)application you will be implementing and which J2EE server and container will be the implementor-Martin ______________________________________________Disclaimer and confidentiality noteEverything in this e-mail and any attachments relates to the official business of Sender. This transmission is of a confidential nature and Sender does not endorse distribution to any party other than intended recipient. Sender does not necessarily endorse content contained within this transmission.> Date: Wed, 21 Nov 2007 12:33:46 -0800> From: toddrun@gmail.com> To: user@ant.apache.org> Subject: Re: How do I set a classpath for my custom task?> > Gilles,> > The user is providing implementations of an interface I provide. Then> my code (which is called from my ant task) calls interface methods.> So I'm not using reflection to call the method, just to get an> instance of the users class.> > Can you point me to any code that shows how to "create a classloader> that is a child of the classloader in which the interface class is> loaded"? I'm assuming I need to actually launch my application in a> new VM using the classloader I create - any pointers to code that does> demonstrates that would be appreciated as well!> > Todd> > On Nov 21, 2007 12:00 PM, Gilles Scokart <gs...@gmail.com> wrote:> > That depends on how you want to invoke the code of the user.> > If you are using only reflection to creates the object, but also to invoke> > the method. The, it is easier. You only have to create a classloader> > defined with the user classpath.> > If you are only using reflection to create an instance o the user object,> > then you are using interface implemented by the user object. Then it is a> > little bit more complex. You need to create a classloader that is a child> > of the classloader in which the interface class is loaded. And your own> > code must also be loaded in a classloader that is the child of the> > classloader in which the interface class is loaded (or the same).> >> > Gilles> >> > 2007/11/21, todd runstein <to...@gmail.com>:> >> > >> > > Ramu,> > >> > > Thanks for the suggestions. I've looked at both of these options, but> > > since this is a custom task that I'd like to include with an open> > > source project, neither of those options are ideal. In the "taskdef",> > > I'd like the user of my task to only have to specify where my jar file> > > is. However, because my task uses classes written by the user, I'd> > > like them to define that classpath in the task itself.> > >> > > To be honest, I may not completely understand the entire problem yet.> > > Let me explain what I *think* the problem is, and hopefully someone> > > can tell me how to fix it.> > >> > > Here is the taskdef that I would ask users to include in their build> > > script:> > >> > > <!-- classpathref needs to include migrate4j.jar so ant can find AntTask> > > -->> > > <taskdef name="migrate" classname="com.eroi.migrate.migration.AntTask"> > > classpathref="lib.classpath" />> > >> > > Then, here's the actual task in use:> > >> > > <!-- The migrate task needs to know where user supplied classes are -->> > > <target name="run" depends="compile" >> > > <migrate> > > version="1"> > > url="jdbc:h2:~/testH2"> > > driver="org.h2.Driver"> > > username="sa"> > > password=""> > > packagename="db.migrations"> > > classpathref="user.classpath"> > > />> > > </target>> > >> > > Not to beat a dead horse, but the taskdef needs to find my code and my> > > code needs to find users code. I think that the classpath in the> > > taskdef is being used to find my task (since the task loads properly),> > > but when code in my task runs, it is too late to change the classpath> > > so my code can't find the users classes. I don't want the user to do> > > anything they wouldn't do with core ant tasks (except for the taskdef> > > entry). I've started looking at the Java, Javac and JUnit tasks to> > > see how they do this, but it's not immediately clear how to replicate> > > their behavior.> > >> > > I'm now looking at the Commandline object to see how that might work> > > with Execute. If anyone has experience using these classes, I welcome> > > any help! I may be about to run into a brick wall, so if I'm> > > obviously going down the wrong path, please let me know.> > >> > > Todd> > >> > >> > > On Nov 20, 2007 8:43 PM, Ramu Sethu <sr...@yahoo.co.in> wrote:> > > > HI todd> > > >> > > > Have you tried the following option ??> > > > 1. -lib option> > > > 2. classpath attribute in taskdef> > > >> > > >> > > >> > > > On Nov 21, 2007 2:15 AM, todd runstein <to...@gmail.com> wrote:> > > >> > > > > I'm writing an ant task that calls a Java class named Engine. Engine> > > > > is in a jar file that is included in my taskdef using the classpath> > > > > tag. This all works great.> > > > >> > > > > The problem I'm having is that one of Engine's methods calls> > > > > "Class.forName()", trying to dynamically add a class file that is not> > > > > in it's jar file. Here I'm getting a ClassNotFoundException. I've> > > > > tried a few things to manually set the classpath that Ant uses, but> > > > > I'm just hacking aimlessly and have had no luck. Could someone give> > > > > me some direction to manually adding to the classpath that Engine will> > > > > ultimately work with?> > > > >> > > > > Thanks in advance!> > > > >> > > > > Todd> > > > >> > > > > --> > > > >> > > > >> > > > > ----------------------------------------------------------------------> > > > > The only "dumb question" is the one you were too afraid to ask.> > > > > ________________________________________> > > > > Check out RouteRuler - Free software for runners, cyclists, walkers,> > > etc.> > > > > http://routeruler.sourceforge.net> > > > > ________________________________________> > > > >> > > > > ---------------------------------------------------------------------> > > > > To unsubscribe, e-mail: user-unsubscribe@ant.apache.org> > > > > For additional commands, e-mail: user-help@ant.apache.org> > > > >> > > > >> > > >> > > >> > > > --> > > > Thank you> > > > Ramu S> > > >> > > > If A is success in life, then A equals x plus y plus z. Work is x; y is> > > > play; and z is keeping your mouth shut.> > > > - Albert Einstein> > > >> > >> > >> > >> > > --> > >> > >> > > ----------------------------------------------------------------------> > > The only "dumb question" is the one you were too afraid to ask.> > > ________________________________________> > > Check out RouteRuler - Free software for runners, cyclists, walkers, etc.> > > http://routeruler.sourceforge.net> > > ________________________________________> > >> > > ---------------------------------------------------------------------> > > To unsubscribe, e-mail: user-unsubscribe@ant.apache.org> > > For additional commands, e-mail: user-help@ant.apache.org> > >> > >> >> >> > --> > Gilles Scokart> >> > > > -- > > > ----------------------------------------------------------------------> The only "dumb question" is the one you were too afraid to ask.> ________________________________________> Check out RouteRuler - Free software for runners, cyclists, walkers, etc.> http://routeruler.sourceforge.net> ________________________________________> > ---------------------------------------------------------------------> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org> For additional commands, e-mail: user-help@ant.apache.org> 
_________________________________________________________________
Put your friends on the big screen with Windows Vista® + Windows Live™.
http://www.microsoft.com/windows/shop/specialoffers.mspx?ocid=TXT_TAGLM_CPC_MediaCtr_bigscreen_102007

Re: How do I set a classpath for my custom task?

Posted by todd runstein <to...@gmail.com>.
Gilles,

The user is providing implementations of an interface I provide.  Then
my code (which is called from my ant task) calls interface methods.
So I'm not using reflection to call the method, just to get an
instance of the users class.

Can you point me to any code that shows how to "create a classloader
that is a child of the classloader in which the interface class is
loaded"?  I'm assuming I need to actually launch my application in a
new VM using the classloader I create - any pointers to code that does
demonstrates that would be appreciated as well!

Todd

On Nov 21, 2007 12:00 PM, Gilles Scokart <gs...@gmail.com> wrote:
> That depends on how you want to invoke the code of the user.
> If you are using only reflection to creates the object, but also to invoke
> the method.  The, it is easier.  You only have to create a classloader
> defined with the user classpath.
> If you are only using reflection to create an instance o the user object,
> then you are using interface implemented by the user object.  Then it is a
> little bit more complex.  You need to create a classloader that is a child
> of the classloader in which the interface class is loaded.  And your own
> code must also be loaded in a classloader that is the child of the
> classloader in which the interface class is loaded (or the same).
>
> Gilles
>
> 2007/11/21, todd runstein <to...@gmail.com>:
>
> >
> > Ramu,
> >
> > Thanks for the suggestions.  I've looked at both of these options, but
> > since this is a custom task that I'd like to include with an open
> > source project, neither of those options are ideal.  In the "taskdef",
> > I'd like the user of my task to only have to specify where my jar file
> > is.  However, because my task uses classes written by the user, I'd
> > like them to define that classpath in the task itself.
> >
> > To be honest, I may not completely understand the entire problem yet.
> > Let me explain what I *think* the problem is, and hopefully someone
> > can tell me how to fix it.
> >
> > Here is the taskdef that I would ask users to include in their build
> > script:
> >
> > <!-- classpathref needs to include migrate4j.jar so ant can find AntTask
> > -->
> > <taskdef name="migrate" classname="com.eroi.migrate.migration.AntTask"
> > classpathref="lib.classpath" />
> >
> > Then, here's the actual task in use:
> >
> > <!-- The migrate task needs to know where user supplied classes are -->
> > <target name="run" depends="compile" >
> >         <migrate
> >                 version="1"
> >                 url="jdbc:h2:~/testH2"
> >                 driver="org.h2.Driver"
> >                 username="sa"
> >                 password=""
> >                 packagename="db.migrations"
> >                 classpathref="user.classpath"
> >         />
> > </target>
> >
> > Not to beat a dead horse, but the taskdef needs to find my code and my
> > code needs to find users code.  I think that the classpath in the
> > taskdef is being used to find my task (since the task loads properly),
> > but when code in my task runs, it is too late to change the classpath
> > so my code can't find the users classes.  I don't want the user to do
> > anything they wouldn't do with core ant tasks (except for the taskdef
> > entry).  I've started looking at the Java, Javac and JUnit tasks to
> > see how they do this, but it's not immediately clear how to replicate
> > their behavior.
> >
> > I'm now looking at the Commandline object to see how that might work
> > with Execute.  If anyone has experience using these classes, I welcome
> > any help!  I may be about to run into a brick wall, so if I'm
> > obviously going down the wrong path, please let me know.
> >
> > Todd
> >
> >
> > On Nov 20, 2007 8:43 PM, Ramu Sethu <sr...@yahoo.co.in> wrote:
> > > HI todd
> > >
> > > Have you tried the following option ??
> > > 1. -lib option
> > > 2. classpath attribute in taskdef
> > >
> > >
> > >
> > > On Nov 21, 2007 2:15 AM, todd runstein <to...@gmail.com> wrote:
> > >
> > > > I'm writing an ant task that calls a Java class named Engine.  Engine
> > > > is in a jar file that is included in my taskdef using the classpath
> > > > tag.  This all works great.
> > > >
> > > > The problem I'm having is that one of Engine's methods calls
> > > > "Class.forName()", trying to dynamically add a class file that is not
> > > > in it's jar file.  Here I'm getting a ClassNotFoundException.  I've
> > > > tried a few things to manually set the classpath that Ant uses, but
> > > > I'm just hacking aimlessly and have had no luck.  Could someone give
> > > > me some direction to manually adding to the classpath that Engine will
> > > > ultimately work with?
> > > >
> > > > Thanks in advance!
> > > >
> > > > Todd
> > > >
> > > > --
> > > >
> > > >
> > > > ----------------------------------------------------------------------
> > > > The only "dumb question" is the one you were too afraid to ask.
> > > > ________________________________________
> > > > Check out RouteRuler - Free software for runners, cyclists, walkers,
> > etc.
> > > > http://routeruler.sourceforge.net
> > > > ________________________________________
> > > >
> > > > ---------------------------------------------------------------------
> > > > To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> > > > For additional commands, e-mail: user-help@ant.apache.org
> > > >
> > > >
> > >
> > >
> > > --
> > > Thank you
> > > Ramu S
> > >
> > >  If A is success in life, then A equals x plus y plus z. Work is x; y is
> > > play; and z is keeping your mouth shut.
> > > - Albert Einstein
> > >
> >
> >
> >
> > --
> >
> >
> > ----------------------------------------------------------------------
> > The only "dumb question" is the one you were too afraid to ask.
> > ________________________________________
> > Check out RouteRuler - Free software for runners, cyclists, walkers, etc.
> > http://routeruler.sourceforge.net
> > ________________________________________
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> > For additional commands, e-mail: user-help@ant.apache.org
> >
> >
>
>
> --
> Gilles Scokart
>



-- 


----------------------------------------------------------------------
The only "dumb question" is the one you were too afraid to ask.
________________________________________
Check out RouteRuler - Free software for runners, cyclists, walkers, etc.
http://routeruler.sourceforge.net
________________________________________

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Re: How do I set a classpath for my custom task?

Posted by Gilles Scokart <gs...@gmail.com>.
That depends on how you want to invoke the code of the user.
If you are using only reflection to creates the object, but also to invoke
the method.  The, it is easier.  You only have to create a classloader
defined with the user classpath.
If you are only using reflection to create an instance o the user object,
then you are using interface implemented by the user object.  Then it is a
little bit more complex.  You need to create a classloader that is a child
of the classloader in which the interface class is loaded.  And your own
code must also be loaded in a classloader that is the child of the
classloader in which the interface class is loaded (or the same).

Gilles

2007/11/21, todd runstein <to...@gmail.com>:
>
> Ramu,
>
> Thanks for the suggestions.  I've looked at both of these options, but
> since this is a custom task that I'd like to include with an open
> source project, neither of those options are ideal.  In the "taskdef",
> I'd like the user of my task to only have to specify where my jar file
> is.  However, because my task uses classes written by the user, I'd
> like them to define that classpath in the task itself.
>
> To be honest, I may not completely understand the entire problem yet.
> Let me explain what I *think* the problem is, and hopefully someone
> can tell me how to fix it.
>
> Here is the taskdef that I would ask users to include in their build
> script:
>
> <!-- classpathref needs to include migrate4j.jar so ant can find AntTask
> -->
> <taskdef name="migrate" classname="com.eroi.migrate.migration.AntTask"
> classpathref="lib.classpath" />
>
> Then, here's the actual task in use:
>
> <!-- The migrate task needs to know where user supplied classes are -->
> <target name="run" depends="compile" >
>         <migrate
>                 version="1"
>                 url="jdbc:h2:~/testH2"
>                 driver="org.h2.Driver"
>                 username="sa"
>                 password=""
>                 packagename="db.migrations"
>                 classpathref="user.classpath"
>         />
> </target>
>
> Not to beat a dead horse, but the taskdef needs to find my code and my
> code needs to find users code.  I think that the classpath in the
> taskdef is being used to find my task (since the task loads properly),
> but when code in my task runs, it is too late to change the classpath
> so my code can't find the users classes.  I don't want the user to do
> anything they wouldn't do with core ant tasks (except for the taskdef
> entry).  I've started looking at the Java, Javac and JUnit tasks to
> see how they do this, but it's not immediately clear how to replicate
> their behavior.
>
> I'm now looking at the Commandline object to see how that might work
> with Execute.  If anyone has experience using these classes, I welcome
> any help!  I may be about to run into a brick wall, so if I'm
> obviously going down the wrong path, please let me know.
>
> Todd
>
>
> On Nov 20, 2007 8:43 PM, Ramu Sethu <sr...@yahoo.co.in> wrote:
> > HI todd
> >
> > Have you tried the following option ??
> > 1. -lib option
> > 2. classpath attribute in taskdef
> >
> >
> >
> > On Nov 21, 2007 2:15 AM, todd runstein <to...@gmail.com> wrote:
> >
> > > I'm writing an ant task that calls a Java class named Engine.  Engine
> > > is in a jar file that is included in my taskdef using the classpath
> > > tag.  This all works great.
> > >
> > > The problem I'm having is that one of Engine's methods calls
> > > "Class.forName()", trying to dynamically add a class file that is not
> > > in it's jar file.  Here I'm getting a ClassNotFoundException.  I've
> > > tried a few things to manually set the classpath that Ant uses, but
> > > I'm just hacking aimlessly and have had no luck.  Could someone give
> > > me some direction to manually adding to the classpath that Engine will
> > > ultimately work with?
> > >
> > > Thanks in advance!
> > >
> > > Todd
> > >
> > > --
> > >
> > >
> > > ----------------------------------------------------------------------
> > > The only "dumb question" is the one you were too afraid to ask.
> > > ________________________________________
> > > Check out RouteRuler - Free software for runners, cyclists, walkers,
> etc.
> > > http://routeruler.sourceforge.net
> > > ________________________________________
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> > > For additional commands, e-mail: user-help@ant.apache.org
> > >
> > >
> >
> >
> > --
> > Thank you
> > Ramu S
> >
> >  If A is success in life, then A equals x plus y plus z. Work is x; y is
> > play; and z is keeping your mouth shut.
> > - Albert Einstein
> >
>
>
>
> --
>
>
> ----------------------------------------------------------------------
> The only "dumb question" is the one you were too afraid to ask.
> ________________________________________
> Check out RouteRuler - Free software for runners, cyclists, walkers, etc.
> http://routeruler.sourceforge.net
> ________________________________________
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org
>
>


-- 
Gilles Scokart

Re: How do I set a classpath for my custom task?

Posted by todd runstein <to...@gmail.com>.
Ramu,

Thanks for the suggestions.  I've looked at both of these options, but
since this is a custom task that I'd like to include with an open
source project, neither of those options are ideal.  In the "taskdef",
I'd like the user of my task to only have to specify where my jar file
is.  However, because my task uses classes written by the user, I'd
like them to define that classpath in the task itself.

To be honest, I may not completely understand the entire problem yet.
Let me explain what I *think* the problem is, and hopefully someone
can tell me how to fix it.

Here is the taskdef that I would ask users to include in their build script:

<!-- classpathref needs to include migrate4j.jar so ant can find AntTask -->
<taskdef name="migrate" classname="com.eroi.migrate.migration.AntTask"
classpathref="lib.classpath" />

Then, here's the actual task in use:

<!-- The migrate task needs to know where user supplied classes are -->
<target name="run" depends="compile" >
        <migrate
        	version="1"
        	url="jdbc:h2:~/testH2"
        	driver="org.h2.Driver"
        	username="sa"
    		password=""
        	packagename="db.migrations"
                classpathref="user.classpath"
        />
</target>

Not to beat a dead horse, but the taskdef needs to find my code and my
code needs to find users code.  I think that the classpath in the
taskdef is being used to find my task (since the task loads properly),
but when code in my task runs, it is too late to change the classpath
so my code can't find the users classes.  I don't want the user to do
anything they wouldn't do with core ant tasks (except for the taskdef
entry).  I've started looking at the Java, Javac and JUnit tasks to
see how they do this, but it's not immediately clear how to replicate
their behavior.

I'm now looking at the Commandline object to see how that might work
with Execute.  If anyone has experience using these classes, I welcome
any help!  I may be about to run into a brick wall, so if I'm
obviously going down the wrong path, please let me know.

Todd


On Nov 20, 2007 8:43 PM, Ramu Sethu <sr...@yahoo.co.in> wrote:
> HI todd
>
> Have you tried the following option ??
> 1. -lib option
> 2. classpath attribute in taskdef
>
>
>
> On Nov 21, 2007 2:15 AM, todd runstein <to...@gmail.com> wrote:
>
> > I'm writing an ant task that calls a Java class named Engine.  Engine
> > is in a jar file that is included in my taskdef using the classpath
> > tag.  This all works great.
> >
> > The problem I'm having is that one of Engine's methods calls
> > "Class.forName()", trying to dynamically add a class file that is not
> > in it's jar file.  Here I'm getting a ClassNotFoundException.  I've
> > tried a few things to manually set the classpath that Ant uses, but
> > I'm just hacking aimlessly and have had no luck.  Could someone give
> > me some direction to manually adding to the classpath that Engine will
> > ultimately work with?
> >
> > Thanks in advance!
> >
> > Todd
> >
> > --
> >
> >
> > ----------------------------------------------------------------------
> > The only "dumb question" is the one you were too afraid to ask.
> > ________________________________________
> > Check out RouteRuler - Free software for runners, cyclists, walkers, etc.
> > http://routeruler.sourceforge.net
> > ________________________________________
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> > For additional commands, e-mail: user-help@ant.apache.org
> >
> >
>
>
> --
> Thank you
> Ramu S
>
>  If A is success in life, then A equals x plus y plus z. Work is x; y is
> play; and z is keeping your mouth shut.
> - Albert Einstein
>



-- 


----------------------------------------------------------------------
The only "dumb question" is the one you were too afraid to ask.
________________________________________
Check out RouteRuler - Free software for runners, cyclists, walkers, etc.
http://routeruler.sourceforge.net
________________________________________

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Re: How do I set a classpath for my custom task?

Posted by Ramu Sethu <sr...@yahoo.co.in>.
HI todd

Have you tried the following option ??
1. -lib option
2. classpath attribute in taskdef


On Nov 21, 2007 2:15 AM, todd runstein <to...@gmail.com> wrote:

> I'm writing an ant task that calls a Java class named Engine.  Engine
> is in a jar file that is included in my taskdef using the classpath
> tag.  This all works great.
>
> The problem I'm having is that one of Engine's methods calls
> "Class.forName()", trying to dynamically add a class file that is not
> in it's jar file.  Here I'm getting a ClassNotFoundException.  I've
> tried a few things to manually set the classpath that Ant uses, but
> I'm just hacking aimlessly and have had no luck.  Could someone give
> me some direction to manually adding to the classpath that Engine will
> ultimately work with?
>
> Thanks in advance!
>
> Todd
>
> --
>
>
> ----------------------------------------------------------------------
> The only "dumb question" is the one you were too afraid to ask.
> ________________________________________
> Check out RouteRuler - Free software for runners, cyclists, walkers, etc.
> http://routeruler.sourceforge.net
> ________________________________________
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org
>
>


-- 
Thank you
Ramu S

 If A is success in life, then A equals x plus y plus z. Work is x; y is
play; and z is keeping your mouth shut.
- Albert Einstein