I'd love reusable Ant tasks
[ vmassol ] 16:29, Thursday, 7 April 2005

Where is Ant heading in the future? I would be very interested to learn more about this. I've been using Ant for several years now and I've always been a happy user. However these days, I'm no longer using much the XML scripting side of Ant but I'm using heavily the Ant Java API; what I'm interested in are the Java Ant tasks.

I think this is really where the value of Ant is. All those years of implementing the base building block for a portable OS Java API have created a very useful Task set. I think every Java application that needs to do copying, deleting a directory, spawning a Java application, etc should use these tasks. There's no point in reinventing the wheel!

For example, you may think that deleting a directory is simple. But it's not so easy. Have a look at the Delete Ant task source code. You'll find portion of code like this one:

/**
 * Accommodate Windows bug encountered in both Sun and IBM JDKs.
 * Others possible. If the delete does not work, call System.gc(),
 * wait a little and try again.
 */
private boolean delete(File f) {
     if (!f.delete()) {
          if (Os.isFamily("windows")) {
               System.gc();
           }
          try {
               Thread.sleep(DELETE_RETRY_SLEEP_MILLIS);
           } catch (InterruptedException ex) {
               // Ignore Exception
           }
          if (!f.delete()) {
               if (deleteOnExit) {
                    int level = quiet ? Project.MSG_VERBOSE : Project.MSG_INFO;
                    log("Failed to delete " + f + ", calling deleteOnExit."
                        + " This attempts to delete the file when the ant jvm"
                        + " has exited and might not succeed."
                        , level);
                    f.deleteOnExit();
                    return true;
                }
               return false;
           }
      }
     return true;
}

Would you have thought about this? Probably not and you would have been right not to as this only happens in some rare occasions. But when one of your users reports it, it's going to be darn difficult to identify and fix. Personally I'd rather depend on a stable and well tested library rather than recode it myself.

The problem is that the Ant tasks are a bit too much linked to the execution engine (the XML scripting engine). For example reusing an Ant tasks requires you to create a Project object. This in turn drags loggers, the Ant classloader (in some cases) and possibly other objects. I know it's possible to use Ant from Java (I've been doing it for a long time now) but I'd love it be even easier to do so.

Instead of writing:

Project project = new Project();
Expander expander = project.createTask("unzip"); 
expander.setSrc(new File(zipfile)); 
expander.setDest(new File(destdir)); 
expander.execute();

I'd like to be able to write:

Expand expand = new Expand();
expand.setSrc(new File(zipfile));
expand.setDest(new File(destdir));
expand.setLogger(myLogger);
expand.execute();

I don't want to see the get/setLocation, get/setTaskName(), get/setDescription() and in general all methods from Task.java.

What I'd love to see is Ant moving in the direction of providing completely reusable Tasks that have 0% dependencies on the Ant engine. This means that loggers, classloaders would be passed to the Ant task by the program who uses it.

I'd like to see Ant provide 2 distributable jars: one containing the XML scripting engine only and one containing all the pure java beans Ant tasks that can be reused in any Java application.

I'd like to see Ant separate into 2 subprojects: one for the XML scripting engine (let's call it engine) and one for the Ant tasks (let's call it tasks). The reason for the 2 projects is to ensure there's no dependency in the direction tasks->engine.

I'd like to see Maven2 use those completely reusable Ant tasks instead of recreating them (this is a wish I'm addressing to both projects, not just Ant! :-)).

I'd like to see those Ant tasks being a JSR and incorporated in a future version of the JDK, thus providing a higher level API that the best classes from the JDK.

Is that where Ant is heading today?

TrackBack
Comments

See some comments at http://www.jroller.com/page/eu/20050407#on_reusable_ant_tasks

--eu, April 7, 2005 09:22 PM

It's a nice view, and in fact Ant is not *that* far from it, since it can already run MOJOs AFAIK.

Maybe proposing patches to Ant for this could move something?

--Nicola Ken Barozzi, April 8, 2005 08:34 AM

A fair bit of these ideas have gone into commons-io. (although we don't have the above code for delete).

Perhaps ant could be more formally refactored to commons.

--Stephen Colebourne, April 8, 2005 01:39 PM

Refactoring to Commons has already come up, but it has been put down, because Ant has to be the first to be built because it builds all other projects (Gump).

Instead, a refactoring of the task methods as reusable libraries that get built before the tasks themselves and that still reside in the Ant project has recieved non-negative feedback.

IOW IIRC they don't need it, but would be ok for them to apply patches as long as the compatibility of the tasks is perserved.

Becasue of this, I see a possible positive path. IMHO Maven would greatly benefit from having Ant keep the maintainence of tasks, so that it can focus on the POM (Maven 2 is a clear step forward in this respect).

--Nicola Ken Barozzi, April 10, 2005 02:31 PM

It's about bootstrapping Ant, not so much about Gump.

The Ant community doesn't want to depend on anything but the JDK to bootstrap Ant, that's pretty much it. So a dependency on any external library is very unlikely to be acceptable.

There is no real resistance against separating reusable pieces of code from Ant, as long as it remains part of the Ant code base, that I was aware of.

--Stefan Bodewig, April 11, 2005 08:22 AM
Post a comment









Remember personal info?