Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What can you do in the ant Task.init() method?

Tags:

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?)

like image 844
skiphoppy Avatar asked Feb 17 '10 16:02

skiphoppy


People also ask

What is an Ant task?

Ant tasks are the units of your Ant build script that actually execute the build operations for your project. Ant tasks are usually embedded inside Ant targets. Thus, when you tell Ant to run a specific target it runs all Ant tasks nested inside that target.

What does * stands for in * Test Java in Ant?

**\*.sql means "in the given directory and inside all of its subdirectories, all the files that end with .sql"

Which of the following task is used for downloading file from web in Ant?

The usetimestamp option enables you to control downloads so that the remote file is only fetched if newer than the local copy. If there is no local copy, the download always takes place. When a file is downloaded, the timestamp of the downloaded file is set to the remote timestamp.


2 Answers

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():

  1. Pre-initialization of delegate objects. For example, the Sync task delegates most of its work to an underlying object, passing through some of the setXYZ() to this delegate. The instantiation and pre-configuration of this delegate has to happen before any properties are set on the task.
  2. Configuration of defaults for property values, which are based on general project settings. For example, the SSH task initializes its knownHosts default by looking at a System property. Also, the Project object has been injected into the task by the init() is called, so the task can look at that.
  3. Adding of optional 3rd-party dependencies to the classpath. For example, the JUnit task adds various Junit-related dependencies. These can only be loaded when the task is used, since they're optional.

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 inside execute() itself.

like image 172
skaffman Avatar answered Oct 11 '22 12:10

skaffman


The init() method is called on each Task upon parsing of the build file and also after the Task has been bound to its owning project. So the Task can do work that involves a Project instance.

like image 27
cherouvim Avatar answered Oct 11 '22 13:10

cherouvim