ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Antoine Lévy-Lambert <anto...@antbuild.com>
Subject Re: How to share an object between Tasks?
Date Tue, 06 Jan 2004 12:48:55 GMT
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


Mime
View raw message