Here's a suggestion for a new feature in Ant.  I would like to see the 
capability for a "default" or dynamic target in Ant.  What this would do is 
catch all target invocations (or target invocations based on a pattern) after 
all explicit targets have been matched.  In other words, any targets which now 
error out for "target not found" would instead be caught and processed by the 
default target.

This is useful for something I'm experimenting with now - dynamic generation of 
Ant scripts.  In my current project, developers are using different sets of app 
servers, each with not only different config values, but with totally different 
config parameters sets.  Each developer has a different setup, so it is 
difficult to write a single Ant file to handle them all, and local changes by 
each developer can't be effectively version controlled.  (Yes, I know the flaw 
in this environment, but I can't control it.)

My experimental solution is to have each developer create a local app server 
definition file, which I then apply a set of XSLT sheets to in order to create 
an Ant script.

Easy enough so far.  The complication comes in when I have a master script to 
do the XSLT transforms and then call the generated script.  The way Ant is 
structured today, the sub-targets will have to be explicitly coded into the 
master script in order to have it call the sub-script.  I can work around this 
by having the developers call the generated script directly with -buildfile, 
but I want to avoid this if possible.

So, my proposed solution goes like this.  Assume we have a build script like 
this:

<target name="target-1">
 ...
</target>

<target pattern="target-*">
 ...
</target>

<default-target>
 ...
</default-target>

Then target invocations would match up like this:

"ant target-1" would invoke target-1, since there is an explicit match.
"ant target-2" would invoke target-*, since there is no explicit match, but 
there is a pattern match.
"ant my-target" would invoke the default target, since there is no explicit or 
pattern match.

(Let me point out here that pattern targets and default targets are related, 
but separate ideas and don't need to be implemented together.  I think default 
targets would be the simpler and more useful to implement first.)

Now I'll show how this relates to my situation.  My master script has a number 
of generic targets - the compilation, jars, etc.  But I also want it to be able 
to pass unknown targets on to the sub-script for processing there.  So, imagine 
that my master script looks like this:

<target name="compile">
  ...do some stuff...
</target>

<default-target>
  <ant antfile="servers.xml" target="${target.name}"/>
</default-target>

where "target.name" is a property set by the default-target class, containing 
the name of the target that was invoked (and fell through to the default 
target).

Now, in my sub-file, I have the following:

<target name="deploy-local">
 ...
</target>

<target name-"deploy-remote">
 ...
</target>

So, now, an invocation of "ant deploy-local" will be unsatisfied in the main 
script and will fall through to the default target.  This will invoke the 
sub-script, which has a matching target.  An invocation of "ant dummy-target" 
will fall through the first script to the default target and flow to the 
sub-script which also has no match, so we get the usual "target not found" 
error.

Without this sort of capability today, I have to resort to something like Perl 
scripts to straighten out where each target should go.  I would like to have 
all of this native to Ant if possible.

For those of you who will answer by saying I shouldn't be generating dynamic 
scripts - I get the point, but some situations call for it, and believe me, 
it's easier than teaching every developer on the project how to write their own 
scripts.  (And better than having them spend expensive time on it...)

Any comments would be appreciated.

Roger Vaughn



Get FREE Email/Voicemail with 15MB at Lycos Communications at 
http://comm.lycos.com

Reply via email to