Monday, May 16, 2011

Ant custom task in application to the real purpose. Part One.

I found that a lot of ant tasks examples face the difficulties when they are got handled by the newbies. So, I decided to provide the detailed instruction on how to implement your own task and the example will be based on the real requirement that I had to create the solution for few days ago.
The problem we faced in our project can be described in the following way:
1. We have some set of files in different folders and of different formats (.properties, .xml, .somethingelse)
2. We need to customize some  values there
3. We use ANT to process those files
4. ANT script is divided in several targets
5. Once some target gets failed the previous ones which have been completed cause changes in some sub-set initial set
6. We require the simple way to re-run the scenario so we won't have the "incremental customization". I mean if we added the string to the file and some further customization failed, we should not have two identical strings after the second run after something went wrong

So the way I have chosen is to determine which ANT standard (or well-known packages) tasks are used  in our customization scripts. The actual set appeared to be not so huge. Like: replace, copy, xmltask. I prototyped the solution for the replace task. Some extended class called SafeReplaceTask was created. It implemented the following functionality:

1. To determine the set of files to be handled in the task
2. To back-up the files
3. To save the mapping of original files - back-up files (the most simple way is to serialize HashMap object)
4. To call the original execute() method of the Class we're extending
5. On fail to restore all the back-ups using the information from de-serialized object

That was the problem I had to solve, and the technical details of this solution you will find in further posts.

Thank you!

-- PART II --

No comments:

Post a Comment