tags:

views:

244

answers:

3

I'm writing a custom Ant task that needs to accept a custom nested type.

According to the Ant manual, I should be able to use addConfigured(TYPE x) rather than addConfiguredTYPE(TYPE x). Also, according to this article (section New Reflection rules, Polymorphism in Ant 1.6) support for addConfigured(TYPE x) was added in Ant 1.6.

<taskdef name="custom-task" classname="com.acme.CustomTask">
    <classpath refid="task.classpath" />
</taskdef>

<typedef name="custom-type" classname="com.acme.CustomTask$CustomType">
    <classpath refid="task.classpath" />
</typedef>

...

<custom-task>
    <custom-type/>
</custom-task>

The task is implemented in Java

public class CustomTask extends Task
{
    ...

    public void addConfigured( CustomType t )
    {...}

    ....

    public static class CustomType
    {...}
}

When I try to run the build script, I get the following exception:

Build Failed: custom-task doesn't support the nested "custom-type" element.

However, when I change

<typedef name="custom-type" classname="com.acme.CustomTask$CustomType">
...
<custom-task>
    <custom-type/>
</custom-task>
...
public void addConfigured( CustomType t )

to

<typedef name="customtype" classname="com.acme.CustomTask$CustomType">
...
<custom-task>
    <customtype/>
</custom-task>
...
public void addConfiguredCustomType( CustomType t )

everything works as expected.

Is there a reason why the generic addConfigured( TYPE x ) technique does not seem to work in my case?

Other people here and here were having the same problem.

PS: Ant version 1.7.0

+1  A: 

Considering the Ant manual does specify that:

The name of the add (addConfigured) method must begin with add (addConfigured), followed by the element name.

, the fact you renamed your "addConfigured" method to "addConfiguredCustomType" was the real key to make it work here.

VonC
http://ant.apache.org/manual/develop.html#nestedtype Ant specifically allows for addConfigured(TYPE x) and add(TYPE x). According to http://www.oracle.com/technology/pub/articles/bodewig_taskwriters.html, that support was added in Ant 1.6
niktech
A: 

The Ant manual section on writing your own tasks is poorly written, but it does say that your method can either be called "addCustomType" or "addConfiguredCustomType", not just "addConfigured". The various ways of declaring it all have subtle differences, so make sure you read it carefully and get the right one.

skaffman
The section following the one you mentioned does say it's possible to use add(TYPE x) and addConfigured(TYPE x). Also, according to http://www.oracle.com/technology/pub/articles/bodewig_taskwriters.html, in the section called New Reflection Rules > Polymorphism in Ant 1.6 , it's clear that one can use public void addConfigured(X); starting with Ant 1.6
niktech
OK, so now it's even more confusing. Just go with what works.
skaffman
The reason I'm trying to go with addConfigured(X) is because I want to use dashes in type name. Using addConfiguredX(X) does not allow for X to have any dashes.
niktech
+1  A: 

Have you tried altering the definition order, i.e. do the typedef first? Not sure if this matters but it's worth trying.

Also, have you tried packing this in an antlib? At my company we have many custom tasks and I know that the plain add and addConfigured methods work. We use antlibs and always define the types first in antlib.xml

Packaging the task and type defs into antlib.xml did the trick! Seems there is a major bug in Ant that prevents doing the same without antlib.xml.
niktech