我正在尝试在自定义 Ant 标记中添加嵌套类型,如http://ant.apache.org/manual/develop.html#nested-elements中所述,但我不断收到异常outer doesn't support the nested "innerone" element.
我整个早上都在谷歌上搜索,但我找不到任何示例、教程或任何使用这种模式来解决这个问题的帮助,我开始怀疑它是否已经实现了!
我的构建文件:
<?xml version="1.0"?>
<project name="test" default="tester" basedir="../../..">
<taskdef name="outer" classname="tester.Outer" classpath="target/classes"/>
<typedef name="innerone" classname="tester.Outer$InnerOne" classpath="target/classes"/>
<typedef name="innertwo" classname="tester.Outer$InnerTwo" classpath="target/classes"/>
<target name="tester">
<innerone/>
<innertwo/>
<outer>
<innerone/>
<innertwo/>
</outer>
</target>
</project>
Java代码:
public class Outer extends Task
{
private List<Inner> inners = new ArrayList<Inner>();
public void add(Inner inner) {
inners.add(inner);
System.out.println("Add: ");
}
public void addConfigured(Inner inner) {
inners.add(inner);
System.out.println("AddConfigured: ");
}
public void execute() {
for (Inner inner : inners) {
System.out.println("Inner: " + inner.getMsg());
}
}
public interface Inner {
String getMsg();
}
public static class InnerOne implements Inner {
public String getMsg() {
return "One";
}
}
public static class InnerTwo implements Inner {
public String getMsg() {
return "Two";
}
}
}
使用这些方法addInner(Inner inner)
并且addConfiguredInner(Inner inner)
两者都有效,但我希望能够添加实现 Inner 接口的内部标记,而不必在 Outer 类中为它们显式创建方法。