看到很多人,包括一些老外,在网上问ant的dtd或者xsd在哪里。
其实在ant.apache.org的FAQ里有一条写的很清楚:
(http://ant.apache.org/faq.html#dtd)
Is there a DTD that I can use to validate my build files?
An incomplete DTD can be created by the <antstructure> task - but this one has a few problems:
It doesn't know about required attributes. Only manual tweaking of this file can help here.
It is not complete - if you add new tasks via <taskdef> it won't know about it. See this page by Michel Casabianca for a solution to this problem. Note that the DTD you can download at this page is based on Ant 0.3.1.
It may even be an invalid DTD. As Ant allows tasks writers to define arbitrary elements, name collisions will happen quite frequently - if your version of Ant contains the optional <test> and <junit> tasks, there are two XML elements named test (the task and the nested child element of <junit>) with different attribute lists. This problem cannot be solved; DTDs don't give a syntax rich enough to support this.
虽然dtd会限制ant的可扩展性但是在语法提示的诱惑下还是生成了这个DTD;
方法大致如下:
1.建立一个build.xml
2.输入内容:
<?xml version="1.0" encoding="UTF-8"?>
<project>
<target name="makedtd">
<antstructure output="project.dtd" />
</target>
</project>
3.到build.xml所在目录下执行ant_home%/bin/ant makedtd
4.BUILD FAILED!发生一个NoClassDefFoundError错误,大致是说org/apache/commons/net/ftp/FTPClientConfig没找到
但是project.dtd还是生成了,我这里这个ANT1.7生成的115KB。
5.为了解决这个问题首先查得FTPClientConfig是apache的commons-net包的,于是下载http://apache.mirror.phpchina.com/commons/net/binaries/commons-net-1.4.1.zip,放到ant_home的lib目录下 ,再次构建 OK ,没有错误提示project.dtd生成202KB。
不过好像给XMLSpy挂上还是没有语法提示 -_-
6.仔细看了一下原来生成的DTD里边有几处问题。
1)第六行<!ELEMENT project (target | %tasks; | %types;)*>
此行末尾的‘*’号应该为‘+’号因为每个build.xml都应该起码有一个<project>元素
2)根据XMLSpy提示,有个定义<!ELEMENT targe EMPTY>。
3) 根据XMLSpy提示<!ELEMENT FTP.........>这块也有问题,由于平时基本不会用到因此删掉。
OK 现在已经是个良好格式(well-formet)的project.dtd文件了。
现在通过XMLSpy根据此project.dtd 建立的build.xml就具备了一般的语法提示功能了。