Tuesday, June 07, 2011

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

Hi!

Here here I started the set of posts about how to apply the customized tasks in Ant to address some real requirement.

So there is a lot of information about how to create custom task in ant. But we're talking about some production requirements. As you may discover in previous post we're trying to create the framework allowing to roll back the changes which have been made anywhere in the ant script, and it doesn't matter whether they were made within one target or the changes were distributed among the several ones. So, to achieve that I decided to extend the native ant tasks performing file operations and I started from Replace task. However I still needed the infrastructure to support this functionality. So the whole workflow looks now like the following:

- We take care of reseting the storage holding the information about which file is mapped to which backup.
- Then we perform safe operations with our files
- Finally we take care of cleaning up the backups being sure all has finished successfully

The particular ANT script will look like this
<project name="Safe replace" default="do-all" basedir=".">

 <taskdef name="safereplace" classname="com.enkata.ant.tasks.SafeReplaceTask">
  <classpath>
   <pathelement path="C:/temp/safereplace.jar" />
  </classpath>
 </taskdef>

 <taskdef name="initstorage" classname="com.enkata.ant.tasks.InitializeStorageTask">  <classpath>
   <pathelement path="C:/temp/safereplace.jar" />
  </classpath>
 </taskdef>

 <taskdef name="cleartemporary" classname="com.enkata.ant.tasks.ClearTempFilesTask">  <classpath>
   <pathelement path="C:/temp/safereplace.jar" />
  </classpath>
 </taskdef>

 <target name="do-all">
  <initstorage/>
  <antcall target="valid"/>
  <antcall target="notvalid"/>
  <cleartemporary/>
 </target>

 <target name="valid">
  <safereplace dir="c:/test_safereplace" token="The" value="HRENAR">
   <include name="**/*.txt" />
  </safereplace>
 </target>

 <target name="notvalid">
  <safereplace file="abc" token="0" value="0">
  </safereplace>
 </target>

</project>

So here are several important points we should pay attention for. First of all it is seen that we use three custom tasks here. Each task has the path to the jar it is represented in. Also we have the general target where we take care of the items I have specified above and call the some valid target (not producing any errors) and some not valid one (that should always fail)

As most of us already know to create the custom task we have to extend ANT native class Task or any class that is the extension of it. The simple custom task that I use is the following:

public class InitializeStorageTask extends Task {

 @Override
 public void execute() throws BuildException {
  File storage = new File (SafeTasksUtils.BACKUP_STORAGE_FILE_NAME);
  if (storage.exists()){
   FileUtils.delete(storage);
  }
 }

}

I'm not including the default overridden methods not to waste the space. Anyway the IDE will remind you. So we should to implement our own execute() method to make it work.

In the third part I will describe the core of the example means how the extended Replace task works and then in the fourth part the step-by-step implementation workflow will be described using Eclipse IDE.

Thank you!