Let Ant tasks choose the attribute values by themself

For a long time an idea travelled in my head. But now it arrived.

In my several Ant build files I have constructs like

<javac source=”${javac.source}” debug=”${javac.debug}” target=”${javac.target}” …

and the according properties defined in an external file. Now have a compile run for the source code and the test code and you have doubled this amount of configuration. And I thought that just writing a <javac> and starting with an -autoconf option would be easier.

The idea is: apply the properties directly before the task execution.

So I could implement a method call in oata.Task.perform() direclty before calling the execute() method.
Hhm …. I don’t want to change the Ant core that deeply because so many external tasks exist and I don’t want to (maybe) break their build.

Another idea is using an AOP framework like AspectJ for jumping in: before <? extends Task>.execute() : applyAttributeValues()
(I am not familiar with AspectJ but you get the idea.)
But then I would depend on the AOP library. That’s nothing for the Core. And Ant options should not depend on any further libraries.
I could implement it as a task:

  <project><autoconf/><javac/></project>    

Better. But I have to learn AspectJ … so not for now…

On the Hudson dev-mailinglist I heard from the Hudson Clover Plugin. It gatheres code coverage from Ant jobs WITHOUT configuring the job itself.-It adds a BuildListener which stores the srcdir and destdir values from <javac> tasks.
Nice idea …. use the Listeners taskStarted(event) and taskFinished(event) methods for doing AOP-stuff.

This results in the <autoconf> task, currently in the sandbox and feedback is welcome.

The goal:

  • apply attribute values from properties
  • do not overwrite user specified values
  • switch on/off the behaviour
  • support name prefix for using different values for different targets

While the last two points are easily to implement (add/remove the listener, use a prefix for property search) the first two are difficult.

apply attribute values from properties / which attributes are supported by a given task?

Why is this difficult? Just ask mytask.getClass() for declared setters ….

The problem is that you don’t get the task object. Due lazy instantiation/configuration the only thing you get from the BuildEvent is an UnknownElement. And therefore you cannot just ask getClass() for the class object you need.

I saw three different strategies according to that value:

  • if it is a <macrodef> I could ask it directly for its <attribute>s
  • if it is a normal task I ask the class object
  • if it is a <presetdef> I ask the class object from preset.getTypeClass()

do not overwrite user specified values

I could ask an object for its values, but which are set by the user and which are just implementation defaults? From the Java perspective you cannot distinguish between them…

My strategy is using a “clean” object (I called it template object) and compare its values with the values from the given object.

Explore posts in the same categories: Ant, en

Schlagwörter: , , , ,

You can comment below, or link to this permanent URL from your own site.

Schreibe einen Kommentar

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

WordPress.com-Logo

Du kommentierst mit Deinem WordPress.com-Konto. Abmelden / Ändern )

Twitter-Bild

Du kommentierst mit Deinem Twitter-Konto. Abmelden / Ändern )

Facebook-Foto

Du kommentierst mit Deinem Facebook-Konto. Abmelden / Ändern )

Google+ Foto

Du kommentierst mit Deinem Google+-Konto. Abmelden / Ändern )

Verbinde mit %s


%d Bloggern gefällt das: