I’m developing a few custom ant tasks that all need to initialize the same objects. I wanted to initialize those object’s in a common superclass that extends from Task, in the init() method. But I see from the lifecycle of an ant task that init() gets called before the tasks child elements and attributes are set. So all of the data I need for initializing those objects is unavailable during init(), if I am reading right.
So, why is init() called at this point? What do you even know that you could use in init()? What could it be used for?
(And is there some other method that I can rely on to be called before execute(), but after my data is available?)
The best guide to this is to look at the source code of the tasks that Ant ships with. There seems to be 3 main use cases for
init():Synctask delegates most of its work to an underlying object, passing through some of thesetXYZ()to this delegate. The instantiation and pre-configuration of this delegate has to happen before any properties are set on the task.SSHtask initializes itsknownHostsdefault by looking at a System property. Also, theProjectobject has been injected into the task by theinit()is called, so the task can look at that.All of the above could conceivably be done inside the task constructor, but the use of an explicit
init()method is in keeping with the Ant task design.As for the second part of your question, no, there is no lifecycle hook that gets called between the properties being set, and
execute()being called. You have to do that insideexecute()itself.