The i-Technology Media!
Register | Log in
   
 
.NET  ·  AJAX  ·  CLOUD  ·  ECLIPSE  ·  FLEX  ·  OPEN WEB  ·  iPHONE  ·  JAVA  ·  LINUX  ·  OPEN SOURCE  ·  ORACLE  ·  PBDJ  ·  SEARCH  ·  SILVERLIGHT  ·  SOA  ·  VIRTUALIZATION  ·  WEB 2.0  ·  WIRELESS  ·  XML
Comments
Drool, Britannia? Is the UK Failing the Cloud?
By Roger Strukhoff
Richard Davies wrote: The UK has a good crop of technology pioneers in cloud computing - for example ElasticHosts, FlexiScale, Flexiant, OnApp - and also some strong government initiatives such as G-Cloud. We will have to see whether this kind of technical leadership converts into swift mass-market adoption or not.
Jan. 8, 2012 11:38 AM EST
read more & respond »
Cloud Expo on Google News
Did you read today's front page stories & breaking news?

Cloud Expo & Virtualization 2011 West
Keynotes
Oracle
Opening Keynote | An Enterprise Cloud for Business-Critical Applications
Abiquo
Day 2 Keynote | The Enterprise Cloud Tightrope - Balancing for Success
Akamai
Day 3 Keynote | The DNA of an Enterprise Cloud
DIAMOND SPONSOR:
Oracle
Many Clouds, Many Choices'Cloud
PLATINUM PLUS SPONSORS:
Abiquo
Enterprise Cloud Best Practices - Town Hall - Join the discussion…
PLATINUM SPONSORS:
Intel
Progressing Toward the Federated, Automated and Client-Aware Cloud
New Relic
How to build an app with Twitter-like throughput
Rackspace
Computing in the Cloud Era
GOLD SPONSORS:
Gale Technologies
Practical Cloud Migration
IBM
Re-think IT. Re-inventing Business.
Intel/McAfee
Identity Driven Security in the Cloud
PerspecSys
Hackers Hackers Everywhere, Is My Public Cloud That Safe?
Red Hat
Unlock the Value of the Cloud
SHI
Mission Critical Applications and the Cloud - Myth or Reality?
SoftLayer
Not Your Grandpa's Cloud
Terremark
Integrating Enterprise Clouds
VMware
Upgrade to a vCloud
POWER PANELS:
Cloud Expo Silicon Valley: CTO Power Panel
Cloud Expo Silicon Valley: CEO Power Panel
Cloud Expo Silicon Valley: Cloud SuperStars Panel
Cloud Expo Silicon Valley: CloudNOW Panel
Click For 2010 West
Event Webcasts
Cloud Expo & Virtualization 2011 East
DIAMOND SPONSOR:
Dell
Dell & VMware Deliver the Enterprise Hybrid Cloud
PLATINUM PLUS SPONSORS:
Abiquo
Are Financial Services Organizations Risking Security by Avoiding Cloud Computing?
Oracle
From Consolidation to Enterprise Private PaaS
PLATINUM SPONSORS:
Intel
Driving the Transformation to Next Generation Cloud Data Centers
Rackspace
The Inevitability of an Open Cloud
GOLD SPONSORS:
CA Technologies
Follow YOUR path to Cloud Computing
Interxion
Who Keeps the Cloud in the Air?
Microsoft
Patterns for Cloud Computing
PerspecSys
War in the Clouds: Are you ready?
ServiceMesh
The Big Win: Stop Playing Small-Ball with Your Cloud Strategy
Terremark
Evaluating Enterprise Clouds
Xiotech
Cloud Storage: Myths and Realities
POWER PANELS:
Cloud Expo New York: CTO Power Panel
Cloud Expo New York: CEO Power Panel
Cloud Expo New York: CMO Power Panel
Cloud Expo New York: Wrap-Up Power Panel
Click For 2010 West
Event Webcasts
Live Google News by SYS-CON!
Top Three Links You Must Click On


Java SE 6
Customizing Ant
Customizing Ant

By: Kirk Pepperdine
Sep. 1, 2003 12:00 AM

You have a task that your Ant build process needs to perform and none of the built-in or dozens of optional tasks fits the bill. If at this point you're thinking that Ant won't work for you, then the authors of Ant have some wonderful news. The framework they use to run built-in tasks is also available for your own task.

If that piques your interest, you'll be happy to know that in the next few paragraphs, I promise you'll have all the information you need to use this framework. If you haven't used Ant yet, read the excellent article by Joey Gibson, "A (Brief) Introduction to Ant" (JDJ, Vol. 7, issue 11), before venturing on this journey.

The framework that supports Ant loosely defines a contract that describes the responsibilities of the task provider. Briefly stated, the task provider provides a class that implements the desired behavior and introduces this task and its supporting class to Ant, exploiting one of the means available for that purpose. The framework is responsible for discovering the task's supporting class and then managing that class's life cycle. Although a task is fronted with a single class, other classes (which may or may not be known to Ant) may be used to help support the completion of the task. Let's take an in-depth look at the custom task contract defined by Ant.

For a class to be considered a custom task it must implement the method, public void execute(). That's it! If you're wondering what the catch is, it's pretty much the same as with everything else. The more information you provide, the less work the framework has to do in order to work with your task. Providing additional information allows the framework to do more work for you. With this in mind, let's describe the portion of the contract we're concerned with here. The description of the providers' responsibilities are as follows:

  • Named task must be supported with a Java class.
  • The class must implement public void execute().
  • The class may extend import org.apache.tools.ant.Task.
  • The class may implement public void init().
  • The class must implement a set method for each attribute of the task.
  • The class should implement the default constructor.
  • The name task must be registered with either the taskdef task or via the addition of an entry into the task.properties file.
  • The execute method should throw a org.apache.tools.ant. BuildException. Complementary to the providers' responsibilities are the frameworks' responsibilities:
  • Call the default constructor for the provider's supporting class.
  • Call the public void init() method once.
  • Call the appropriate set method for every task attribute.
  • Call the public void execute() throws BuildException method once for every occurrence of the corresponding task found in the build file.

In addition to those assuming these responsibilities, the provider must define a set of XML tags to be used when exploiting the custom task. From here, we could proceed to a fairly dry explanation of how all this fits together, but let's see how it all works in practice instead. I'll kick start the process with the following story.

Most applications built today contain a number of "off-the-shelf" components that complement code developed in-house. These components can be supplied by a combination of vendors and infrastructure teams that work alongside the application development team. Given that each group (especially third-party suppliers) will have its own delivery schedule over which you most likely won't have any influence, it would be unheard of not to have to deal with version management. Lack of a concise plan to manage versions often results in confusion among configuration management staff that can result in them having to spend a considerable amount of time sorting out which versions of which components (a.k.a. JARs) are to be used for each build. A tactical solution to this problem is to use a version map to describe the build. Let's first build a sample map and then a custom task to use this map to drive a build process.

To keep things somewhat simplified so we aren't too distracted from our main goal - learning how to build a custom task - we'll purposely limit the functionality of the version map to pulling artifacts from CVS. The test application will be a toy chat application that consists of four layers (see Figure 1).

The JMS bus was acquired from a third-party vendor (SwiftMQ in this instance). Our internal infrastructure group is responsible for the channel component. Finally, the application development team has built chat client and topic server. During the development process, they've also identified a common layer shared by the clients and the server. In accordance with our earlier constraints, the JMS implementation can be found in the file system. The JAR file for the channel, as well as the source for commons, the chat client, and the topic server will be pulled from CVS. Let's use XML to build the version map.



dest="./com mon"/>
dest="./serv er"/>


The map contains all the information needed to identify all the attributes that are necessary to locate a component. Note: This mechanism does rely on a stable policy of how code artifacts are to be stored.

As mentioned earlier, we're required to register the custom task. This can be completed using one of two techniques. The taskdef task maps an alias for a task to its implementation. The second technique inserts the definition into the task.properties file found in the Ant distribution. A quick examination of the file will reveal how to do this. I'm going to avoid the discussion as to which approach is best, but there are implications from using either mechanism to register your custom task that go beyond technical issues. Let's look at the build file fragment found in the following code.




This is telling Ant that we'd like to define the custom task aliased as versionmap and implemented in the class com.jpt.ant.task.VersionMap. The task versionmap takes two attributes, the name of the file containing the map and a destination directory to work in.

Again referring back to the contract, we see that we should create a class that implements our custom behavior. Though it's not necessary, it makes life a lot easier to have our class extend org.apache.tools.ant.Task. Extending this class allows us to inherit common task behavior. We'll have to decide on an implement for the methods public void init() and public void execute(). In addition, the XML defines the two attri- butes, file and dest. Fields and supporting access methods must be implemented to support these attributes. Let's look at the custom task class template found in Listing 1.

With this, we've pretty much covered the entire contract. All that's left is to fill in the task's behavior. Let's start with the init method. The init method gets called once and this occurs fairly early on in the build process. Consequently, we should embed behavior that we only want executed once and that can also occur early in the build process. Since our custom task doesn't have any behavior that fits this description, we allow the call to percolate up to the super classes implementation.

A common technique used for coding the execute method is to defer the bulk of the implementation to a helper method called execute0. Doing this allows us to easily handle the inherited attribute, failonerror, in the execute method. The main logic that we need is to read each entry in the version map and use the information to trigger a CVS get (see Listing 2). Note how the functionality of other tasks is being used to help us achieve our goal.

In the execute0() method, the XML file is converted into a map. An iterator runs through each entry in the map and calls its execute method using the task as a parameter. The execute method of the task is then free to make a call back on the getSourceFromCVS method. It's this method that creates an instance of a CVS (from the optional tasks list) task and then configures it. The executeTask method sets the technical parameters on the task before executing it. Our simplified custom task is now complete.

Where to Go from Here
Nested attributes can add a lot of flexibility to your custom task. Each Ant data type object is supported by a class and, consequently, the development path is fairly similar to one that's followed by custom tasks. In addition to tasks and data types, you can also build specialized build listeners. Though their development path is slightly different than that of data types and tasks, it's still not all that difficult. I've always found that it's best to look into Ant's code when I'm looking for examples or techniques to help me develop custom tasks, data types, and listeners. Happy AntingS

References

* Ant 1.5.1 source code: www.apache.org
* Williamson, A., et al. (2003). Ant Developer's Handbook. Sams.

Published Sep. 1, 2003— Reads 24,900 — Feedback 2
Copyright © 2003 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
Related Links
▪ Figure 1
▪ Source Code
About Kirk Pepperdine
Kirk Pepperdine has more than 10 years of experience in OO technologies. In edition to his work in the area of performance tuning, Kirk has focused on building middleware for distributed applications.

Add Your Feedback

In order to post a comment you need to be registered and logged in.

Register | Sign-in

Reader Feedback: Page 1 of 1

#2
Phil Willemann commented on 23 Sep 2003

I am a beginner at custom ant tasks.

Shouldn't execute0() include a call to getSourceFromCVS?
Where is the code for processMapEntry?
Does processMapEntry call getSourceFromCVS?

#1
Phil Willemann commented on 23 Sep 2003


Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
Click to Add our RSS Feeds to the Service of Your Choice:
Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
Publish Your Article! Please send it to editorial(at)sys-con.com!

Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021

SYS-CON Featured Whitepapers

ADS BY GOOGLE

Breaking Java News
FRO - Q4 2011 Presentation
GSMA Statement on Planned Barcelona Public Transport Strike
Smart Phones Being Shipped to Soldiers in Afghanistan
Superstar-Style Holidays - at a Fraction of the Price!
Smart Phones Being Shipped to Soldiers in Afghanistan
Superstar-Style Holidays - at a Fraction of the Price!
GSMA Statement on Planned Barcelona Public Transport Strike
The Conference Board Leading Economic Index® (LEI) for Spain Increases
Learn How to Spread Bet With Free Online Tools

ADVERTISE   |   MAGAZINE SUBSCRIPTIONS   |   FREE BREAKING-NEWSLETTERS!   |   SYS-CON.TV   |   BLOG-N-PLAY!   |   WEBCAST   |   EDUCATION   |   RESEARCH

.NET Developer's Journal - .NETDJ   |   ColdFusion Developer's Journal - CFDJ   |   Eclipse Developer's Journal - EDJ   |   Enterprise Open Source Magazine - EOS
Open Web Developer's Journal - OPENWEB   |   iPhone Developer's Journal - iPHONE   |   Virtualization - Virtualization   |   Java Developer's Journal - JDJ   |   Linux.SYS-CON.com
PowerBuilder Developer's Journal - PBDJ   |   SEO / SEM Journal - SJ   |   SOAWorld Magazine - SOAWM   |   IT Solutions Guide - ITSG   |   Symbian Developer's Journal - SDJ
WebLogic Developer's Journal - WLDJ   |   WebSphere Journal - WJ   |   Wireless Business & Technology - WBT   |   XML-Journal - XMLJ   |   Internet Video - iTV
Flex Developer's Journal - Flex   |   AJAXWorld Magazine - AWM   |   Silverlight Developer's Journal - SLDJ   |   PHP.SYS-CON.com   |   Web 2.0 Journal - WEB2
Apache   |   CMS   |   CRM   |   HP   |   Oracle Journal   |   Perl   |   Python   |   Red Hat   |   Ruby on Rails   |   SAP   |   SaaS

SYS-CON MEDIA:   ABOUT US   |   CONTACT US   |   COMPANY NEWS   |   CAREERS   |   SITE MAP
SYS-CON EVENTS:   |  AJAXWorld Conference & Expo  |  iPhone Developer Summit  |  Cloud Computing Conference & Expo  |  SOA World Conference & Expo  |  Virtualization Conference & Expo
INTERNATIONAL SITES:   India  |  U.K.  |  Canada  |  Germany  |  France  |  Australia  |  Italy  |  Spain  |  Netherlands  |  Brazil  |  Belgium
 Terms of Use & Our Privacy Statement     About Newsfeeds / Video Feeds
Copyright ©1994-2008 SYS-CON Publications, Inc. All Rights Reserved. All marks are trademarks of SYS-CON Media.
Reproduction in whole or in part in any form or medium without express written permission of SYS-CON Publications, Inc. is prohibited.
 
close this window