May 1, 2010 Ant, Continuous Integration

Demanding of Ant: 1 scriptdef

I hate Ant. I like NAnt better, and I hate NAnt too.  The intent of this series is to share how I have demanded more out of Ant.

Did you know that you can mess with the Java objects of Ant in Ant script?  You can create tasks on the fly. You can even create a logger on the fly.  I have used the following example to insure that a log file is written to without having to count on a user supplying the needed command line arguments.

If you are not familiar with the scriptdef task in Ant you should read the help for the task here.  It is part of the optional Ant tasks, so you will need to have gotten your Ant install setup correctly for the optional tasks.  The scriptdef task supports several languages, here I will be using JavaScript.

<?xml version=1.0 encoding=UTF-8?>

<project name=scratch default=play basedir=. >

  <target name=play>



  <scriptdef name=say.hello language=javascript>






Executing this script will output:


[say.hello] hello

Total time: 0 seconds

You can import Java classes into the JavaScript with the importClass method: importClass(Package.[Java Full Class Name]);.  When you combined this with your access back into the Ant object graph through self you can do some interesting things.  To explore what is available to me I used this trick to iterate over what self provides:

<scriptdef name=explore.self language=javascript>


  for (member in self)






[explore.self] project
[explore.self] getText
[explore.self] createDynamicElement
[explore.self] toString
[explore.self] getTaskType
[explore.self] wait
[explore.self] bindToOwner
[explore.self] log
[explore.self] getClass
[explore.self] getLocation
[explore.self] taskType
[explore.self] hashCode
[explore.self] fail
[explore.self] reconfigure
[explore.self] class
[explore.self] notify
[explore.self] description
[explore.self] maybeConfigure
[explore.self] location
[explore.self] setTaskType
[explore.self] dynamicAttribute
[explore.self] getTaskName
[explore.self] clone
[explore.self] getOwningTarget
[explore.self] getProject
[explore.self] setLocation
[explore.self] setDescription
[explore.self] getRuntimeConfigurableWrapper
[explore.self] taskName
[explore.self] text
[explore.self] equals
[explore.self] setRuntimeConfigurableWrapper
[explore.self] setTaskName
[explore.self] setDynamicAttribute
[explore.self] owningTarget
[explore.self] getDescription
[explore.self] execute
[explore.self] setOwningTarget
[explore.self] perform
[explore.self] notifyAll
[explore.self] init
[explore.self] addText
[explore.self] setProject
[explore.self] runtimeConfigurableWrapper

Self provides a reference to the Ant object project; pretty much the root of the object graph.  You can use this same trick on other objects you need to get more information on.  Sometimes I need to understand the inner workings so I use a decompiler or read the Ant source.  The last critical piece of information you need to unlock the door to productivity is the method project.createTask(String taskName).  The argument taskName is the friendly name not the class name of the task to be created. For example project.createTask(“mkdir”) will create an instance of the mkdir task.  Armed with this knowledge you can experiment and learn to apply it in many helpful and time saving ways.  Here is an example that will use the default logger to output to the console and to a dynamically named log file.

<?xml version=1.0 encoding=UTF-8?>

<project name=scratch default=play basedir=. >

  <taskdef resource=net/sf/antcontrib/ />

  <target name=play>

    <var name=Log.Directory.Path value=C:\Temp\Play/>


    <echo>T minus</echo>




    <echo>blast off…</echo>


  <scriptdef name=start.logger language=javascript>








        mkdir = project.createTask("mkdir");

        mkdir.setDir(new File(project.getProperty("Log.Directory.Path")));


        tstamp = project.createTask("tstamp");

        format = tstamp.createFormat();




        var logger = new DefaultLogger();

        var out = new PrintStream(new FileOutputStream(project.getProperty("Log.Directory.Path") + "/" + project.getProperty("") + "-" + project.getProperty("now") + ".log"));





        logger.buildStarted(new BuildEvent(project));

        logger.targetStarted(new BuildEvent(self.getOwningTarget()));

        logger.taskStarted(new BuildEvent(self))




This is the console output:

c:\>ant -f play

[echo] T minus
[echo] 3
[echo] 2
[echo] 1
[echo] blast off…

Total time: 0 seconds

And here is the output to the logfile:


[echo] T minus
[echo] 3
[echo] 2
[echo] 1
[echo] blast off…

Total time: 0 seconds

62,954 Total Views

25 to “Demanding of Ant: 1 scriptdef”


  1. [...] Topsy Retweet Button topsyWidgetPreload({ “url”: “″, “title”: “JayFlowers > Demanding of Ant: 1″, “order”: “count,badge,retweet” }) Add Topsy Retweet Button to your Blog or Web Site. WordPress  Web Sites 3 tweets tweet [...]

  1. Twitter Trackbacks for JayFlowers > Demanding of Ant: 1 [] on says...

    [...] Topsy Retweet Button topsyWidgetPreload({ “url”: “″, “title”: “JayFlowers > Demanding of Ant: 1″, “order”: “count,badge,retweet” }) Add Topsy Retweet Button to your Blog or Web Site. WordPress  Web Sites 3 tweets tweet [...]

  2. ugg australia uk says...

    good job,guys.

  3. pins disney world says...

    Having read this I thought it was very enlightening.
    I appreciate you spending some time and effort to put this information together.
    I once again find myself personally spending a significant amount of time both reading and commenting.

    But so what, it was still worth it!

  4. collègue de travail traduction says...

    You’re so interesting! I do not suppose I have read a single thing like this before.
    So wonderful to find another person with some unique thoughts on
    this subject matter. Seriously.. thanks for starting this up.

    This site is one thing that is required on the internet,
    someone with some originality!

  5. payday loans says...

    Link exchange is nothing else however it is just placing the other person’s weblog link on your page
    at suitable place and other person will also do same in favor of you.

  6. zee bee junior pentatonic shred in B lick four says...

    Do you have a spam problem on this website; I also am a blogger,
    and I was wanting to know your situation; many of us have developed
    some nice procedures and we are looking to swap techniques with others, please
    shoot me an email if interested.

  7. campingfrance says...

    Fantastic post however I was wondering if you could write a litte more on this topic?
    I’d be very grateful if you could elaborate a little bit further.
    Bless you!

  8. says...

    In the bedroom, window treatments have double duty.
    In fact, many companies that sell partition systems offer free
    planning and installation. We also got him some finger puppets as
    well as a few soft musical toys.

  9. buy facebook likes cheap india says...

    I simply could not depart your site before suggesting that
    I extremely loved the usual information an individual provide in your visitors?
    Is going to be again regularly to check out new posts

  10. says...

    I’ve been surfing online greater than three hours these days, but I
    never found any interesting article like yours. It’s beautiful value
    enough for me. Personally, if all web owners and bloggers made just right
    content as you did, the net will probably be much more helpful than ever before.

  11. rubinetti per lavelli poco prezzo compra says...

    If some one desires expert view regarding
    blogging and site-building after that i suggest him/her to go to see this weblog, Keep up the pleasant work.

  12. Johnd748 says...

    Enjoyed studying this, very good stuff, regards . A man may learn wisdom even from a foe. by Aristophanes. gdfbgcabdebg

  13. domain says...

    What’s up to every one, the contents existing at this web
    site are in fact amazing for people knowledge, well,
    keep up the nice work fellows.

  14. Iliana Alvin says...

    That is a great tip particularly to those fresh to the blogosphere. Brief but very precise information… Thanks for sharing this one. A must read article!

  15. corporate gift items says...

    Picking the right location can literally mean the difference between being wildly successful and failing miserably.

    In every gathering they will go to, they can put on a quite show that he is
    someone responsible that mean business. Pins come in a variety
    of styles and are considered a nice little corporate gift
    that can be easily customized according to a company’s logo,
    name or design style.

  16. Microsoft Windows 8 64 bit says...

    very useful post! I’m responding on upper comment : “Who wants some upgrade? Be prepared to get new soft for your PC”?.

  17. car games says...

    I really liked your post.Thanks Again. Really Great.

  18. printed physical banner says...

    Excellent goods from you, man. I’ve remember your stuff previous
    to and you’re just extremely wonderful. I really like what you’ve acquired here, really like what you are stating and the best way
    during which you assert it. You’re making it enjoyable and you still care for to
    stay it smart. I can not wait to learn far more
    from you. This is actually a great web site.

  19. says...

    Hi there, yeah this article is in fact nice and I have learned lot of things from it concerning blogging.

  20. MarinBNalley says...

    I am just curious to determine what blog platform you might
    be dealing with? I’m having some minor security issues with my latest site and I would
    personally prefer to find some thing risk-free. Are you experiencing any recommendations?

  21. obral daster pekalongan says...

    Provided that you have the right knowledge and tools, you can bring your
    online business to great heights and you can expect great
    rewards right after. Larger stores or locations that are just getting started can find a premium
    display fixture that features divided bins and includes over 850
    pounds of cheap bulk candy. So if you want to buy them cheap, try focusing on Chinese online store.

  22. Ronny says...

    Organizations use this amazing accessory to promote their products
    in a unique way. In fact, this can easily be a family business because ultimately, your
    whole family will benefit as a result of your getting involved
    in this business. When people find that your website offers a complete array of
    toys of every brand, for any age, and price, people will
    always come back for more.

  23. Lisa Lauder says...

    Mega schön

  24. Elvin Schwager says...

    Hot bath, food and chill before another long day a head of me!|SamBezzina|

Leave a comment