Ant中的自定义任务:addTYPE(TYPE x) vs add(TYPE x)(后者不起作用)

问题描述 投票:3回答:3

我正在编写一个自定义的Ant任务,需要接受一个自定义的嵌套类型。

根据 蚂蚁手册我应该能够使用addConfigured(TYPE x)而不是addConfiguredTYPE(TYPE x)。另外,根据 本文 (Ant 1.6中的新反射规则、多态性部分)在Ant 1.6中增加了对addConfigured(TYPE x)的支持。

<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>

这个任务是在Java中实现的

public class CustomTask extends Task
{
    ...

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

    ....

    public static class CustomType
    {...}
}

当我尝试运行构建脚本时,我得到以下异常。

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

然而,当我改变

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

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

一切都按预期工作。

有什么原因导致通用的addConfigured( TYPE x )技术在我的情况下似乎不工作?

其他的人 此处此处 都有同样的问题。

PS:Ant版本1.7.0

java ant
3个回答
1
投票

你是否尝试过改变定义顺序,即先做 typedef?不知道这是否重要,但值得一试。

另外,你是否尝试过将其打包在antlib中?在我的公司,我们有许多自定义任务,我知道普通的 addaddConfigured 方法工作。我们使用antlibs,总是先在antlib.xml中定义类型


1
投票

考虑到 蚂蚁手册 确实说明了这一点。

添加的名称(addConfigured)方法必须以add(addConfigured),后面是元素名。

事实上,你把你的""重命名为""。addConfigured"的方法来"addConfiguredCustomType"才是在这里成功的真正关键。


0
投票

蚂蚁手册部分 关于写自己的任务写得不好,但它确实说了你的方法既可以调用 addCustomTypeaddConfiguredCustomType不仅仅是 addConfigured. 各种申报方式都有细微的差别,所以一定要仔细阅读,找对方法。

© www.soinside.com 2019 - 2024. All rights reserved.