This week's book giveaway is in the Cloud forum.
We're giving away four copies of Terraform in Action and have Scott Winkler on-line!
See this thread for details.
Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Ease of Extending Ant?

 
Ranch Hand
Posts: 1902
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know that extending Ant is possible, but the docs and everything I've seen so far really hasn't said how easy/difficult it is.
Could anyone here give some insight as to how easy or hard it is to create custom tasks? One of the things I'd like to try here with my team (when we start using Ant in the near future) is integrating our internal build-tracking system with Ant and having it generate a report on each build, and it would be nice to be able to write a task to accomplish that... but I need some insight as to how much trouble I'm going to be in for.
 
Author
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Theodore,
One way that you can see how to extend ANT (on top of buying a copy of our book ) is to look at the source for an existing task.
Isn't open source great!
 
Theodore Casser
Ranch Hand
Posts: 1902
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The 'how' isn't so much the question as the difficulty level. As my program manager constantly says, "Oh, everything looks easy when you're not the one coding it."
 
Author
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Theodore Casser:
The 'how' isn't so much the question as the difficulty level. As my program manager constantly says, "Oh, everything looks easy when you're not the one coding it."


Ant is amazingly easy to extend. There happens to be a chapter on this in our book...
As for how easy, check this out. This is a simple task that will accept a single FileSet and traverse it printing out something interesting about each file. It could easily be extended to accept multiple filesets. Also note that it supports the concept of a "failureProperty": in this way you can say that instead of the task failing and aborting the build, it will set a certain property if it fails, that you can check later. The actual setting of this property is commented out, but you get the idea.
I tried to enter the code here, but kept getting BBoard errors, so I put it at http://www.joeygibson.com/jr.txt
The setXXX methods equate to xxx="" attributes in your build file. The addFileSet method accepts a nested FileSet and the execute method is where the work gets done.
The buildfile snippet is also at http://www.joeygibson.com/jr.txt
Note the taskdef to bring the code in and assign a name and then we call it with the foo tag, giving it a fileset that will find everything that doesn't end in .java in the project base directory.
It doesn't get much simpler.
 
Ranch Hand
Posts: 282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Joey,
Thanks for the example. Using it as my guide, I was able to create my first custom Ant task. However, for those of us that use WSAD v4.0.3 (and, probably Eclipse 1.0), there seems to be a little more to it.
I was unable to define a task using <taskdef> in WSAD. Instead, WSAD seems to want all extensions (including Ant extensions) to the (Eclipse) software to reside in a plug-in folder.
To get your example to work, I created a folder under the WSAD plugins folder named com.ant.test (I know that's an improper name but it has the advantage of being the first folder displayed in the plugins folder .)
Then, I jarred the class file for the MyTask task into antTasks.jar and placed it in the com.ant.test plugin folder and created the following plug-in file descriptor:
<?xml version="1.0" encoding="UTF-8"?>
<plugin
id="com.ant.test"
name="%pluginName"
version="1.0.0">
<requires>
<import plugin="org.eclipse.ant.core"/>
<import plugin="org.eclipse.core.resources"/>
</requires>
<runtime>
<library name="anttasks.jar">
<export name = "*"/>
</library>
</runtime>
<extension-point id="antTasks" name="%tasksName"/>
<extension
point="org.eclipse.ant.core.antTasks">
<antTask
name="foo"
class="com.lilly.ant.MyTask">
</antTask>
</extension>
</plugin>
This enables your MyTask example to run inside WSAD.
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am in the process of writing my first task, so i can tell you that it's a fairly easy thing to do. my task extends MatchingTask, so it's even easier (MatchingTask handles setting FileSets for you).
otherwise, all sorts of conversion things are handled for you (e.g. you can create a boolean attribute and write a setter for it that takes a boolean input, and the ant supertask you extend - if you're wise - will convert xml attribute values like "yes" or "true" to a boolean for you and call your setter.
the only problems i had were classpath issues when i tried to start up a Java task internally (turns out i have to fork the Java sub-task).
 
Kirk Pepperdine
Author
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One thing to be aware of, ANT treats setxx differently than addxxx. If it matters at all, setXXX is much cheaper to use then addXXX.
 
john guthrie
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
er, how does addXxx() work? i know setXxx()...
 
Author
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
setXXX is for attributes, and datatypes are extremely flexible here.
for elements, things get a bit more complex. there are three signatures that are possible: public void addXXX(XXX), public void addConfiguredXXX(XXX), and public XXX createXXX(). the adders are when you have a no-arg constructor and you are happy letting Ant create the objects for you automatically (addConfigured populates that elements attributes, etc before handing it to you, addXXX does not). the creator is when you need to create the object yourself.

Originally posted by john guthrie:
er, how does addXxx() work? i know setXxx()...

 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This article I wrote last year includes an example of writing your own Ant task:
http://www.javaranch.com/newsletter/Mar2002/newslettermar2002.jsp#ant
There is really nothing to it.
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Writing custom ant task is easy.
Take a look at http://asia.cnet.com/itmanager/tech/0,39006407,39044695,00.htm
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic