You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by Bertrand Fontaine <fo...@hotmail.com> on 2004/01/06 13:28:25 UTC

How to share an object between Tasks?

I have a set of Tasks all requiring access to the very same class instance 
(a db connection in practice). How can I implement that with ANT?

So far, I have tried to implement this behaviour via a TaskContainer but I 
can't get back the TaskContainer instance from a Task, and hence the 
reference to the object I would like to share via the TaskContainer, as far 
as the TaskContainer and the Tasks are loaded in separate class loaders.

Thank you,

Bertrand Fontaine

_________________________________________________________________



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


Re: How to share an object between Tasks?

Posted by Antoine Lévy-Lambert <an...@antbuild.com>.
Bertrand Fontaine wrote:

> I have a set of Tasks all requiring access to the very same class 
> instance (a db connection in practice). How can I implement that with 
> ANT?
>
> So far, I have tried to implement this behaviour via a TaskContainer 
> but I can't get back the TaskContainer instance from a Task, and hence 
> the reference to the object I would like to share via the 
> TaskContainer, as far as the TaskContainer and the Tasks are loaded in 
> separate class loaders.
>
> Thank you,
>
> Bertrand Fontaine
>
>
>
Hi Bertrand,

what I would do would be the following :
- create a custom ant datatype encapsulating the object(s) that you want 
to share between tasks,
- use <typedef/> to declare your datatype (or put all your typedefs and 
taskdefs into an antlib descriptor)
-  then you can declare one or several instance(s) of your custom 
datatype(s) with the id attribute set; in your custom tasks, add a an 
attribute which would be a reference to your custom datatype

something like this :

<typedef name="mydata" class="com.foo.mydata"/>
<taskdef name="mytask" class="com.foo.mytask"/>
<taskdef name="mytask2" class="com.foo.mytask2"/>
<mydata property1="some.value" property2="some.othervalue" id="myid"/>
<mytask dataref="myid"/>
<mytask2 dataref="myid"/>

in the source code of ant, this type of mechanisms is used a lot; for 
instance the javac task can accept a reference to the path datatype in 
the attribute classpathref, ...
I have the impression that there are no explanations about custom 
datatypes in the ant manual, this should exist.
Most of what has been written about custom tasks also is true of custom 
datatypes, the only difference is that an execute() method is not needed 
for a datatype.
Your custom datatypes may extend org.apache.tools.ant.types.DataType 
which probably makes coding more easy. It is probably also not an 
absolute requirement.

Hopes this helps,

Antoine



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