ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Rob Seegel <>
Subject Re: Recycle an Ant task
Date Tue, 02 Oct 2012 10:49:54 GMT
Thanks for sharing this Jose. I had a fairly complex build that called 
other project builds and I needed something like this as well at one 
time. I think it's worth reviewing it now and seeing if something like 
one of these two options might be the better way to go. The way I chose 
is pretty embarrassing in retrospect, but it did work.


On 9/28/12 9:50 AM, Jose Rojas wrote:
> Stefan, thank you for two excellent solutions.
> I actually implemented the reference solution yesterday.  What I discovered
> would be interesting for others looking for a solution like this:
> * Project.addReference() is what I used to create and set a new reference
> at the highest Project level.
> * You must ensure that inheritRefs="true" is set for any nested
> antcall,ant,subant,runtarget tasks in the entire task call chain that will
> invoke the task that you wish to cache data for.
> * You must look out for 'cloning' of the references along the task chain.
>   The Ant code implicitly calls 'clone' via reflection, which can block your
> attempts to retrieve the cached data at the original project scope if the
> data structure used to hold the cached information creates new instances of
> itself (for example when using a HashMap to cache data, I had to subclass
> it and overwrite 'clone()' to return 'this').
> That being said, I think recycling the class loader is a more concise and
> and accurate solution in my case.  Since my build file re-executes the
> taskdefs, I would need to use the loaderRef as Stefan mentioned to prevent
> the class from being reloaded on a subsequent execution.
> Thanks again!
> On Thu, Sep 27, 2012 at 9:08 PM, Stefan Bodewig<>  wrote:
>> On 2012-09-26, Jose Rojas wrote:
>>> I would like to create a custom ant task that caches some results from an
>>> operation and saves it for use in future executions.  My simple method of
>>> doing this is using a 'static' member.  This would work great for me as
>> the
>>> operations would be appropriate to cache at an application wide level.
>> A better approach might be to store the data as a reference in the
>> Project instance (or several of them).
>>> However what I'm noticing is that for each new execution of Ant (I'm
>> using
>>> the Ant taskdef to execute ant), the static data is lost, quite possibly
>>> caused by Ant creating a new class loader upon every execution, thus not
>>> reusing my ant task class instance from the previous execution.
>> This would happen if you use multiple taskdefs - i.e. repeat it several
>> times.  You can pass a name of a class loader to taskdef (the loaderRef
>> attribute) and Ant will create a class loader on the first execution,
>> cache it and reuse it for all subsequent task/typedefs that use the same
>> name.
>> Stefan
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail:
>> For additional commands, e-mail:

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message