Ant 之 生成 ant.dtd 文件 (转载)

Is there a DTD that I can use to validate my build files?php

An incomplete DTD can be created by the <antstructure> task - but this one has a few problems:html

  • 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;apache

方法大体以下:app

1.创建一个build.xmlui

2.输入内容:this

<?xml version="1.0" encoding="UTF-8"?>
       <project>
              <target name="makedtd">
                   <antstructure output="project.dtd" />
              </target>
       </project>
spa

3.到build.xml所在目录下执行ant_home%/bin/ant makedtdcode

4.BUILD FAILED!发生一个NoClassDefFoundError错误,大体是说org/apache/commons/net/ftp/FTPClientConfig没找到orm

可是project.dtd仍是生成了,我这里这个ANT1.7生成的115KB。xml

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就具有了通常的语法提示功能了。

相关文章
相关标签/搜索