29

Could please anybody explain to me why is that? I've been always using Maven and absence of xml definition for Ant build script is something that makes me searching through internet and documentation instead of one look at the xsd or DTD.

For instance it is almost impossible to find out whether target definition can have own property declared or not.

peterh
  • 11,875
  • 18
  • 85
  • 108
lisak
  • 21,611
  • 40
  • 152
  • 243
  • Why - just because the authors of ant don't want it. You should dive into the docs to find what you want. – khachik Dec 11 '10 at 15:48

3 Answers3

17

There is a FAQ for this:

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 Apache 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.
gnat
  • 6,213
  • 108
  • 53
  • 73
  • 1
    I noticed, but it is absolutely useless answer and the link in there doesn't exist. If DTD can't handle it. XSD is able to for sure. – lisak Dec 11 '10 at 15:58
  • 1
    I don't think the answer there is useless. It pretty well explains why you cannot really validate an Ant script –  Dec 11 '10 at 16:24
9

You can also use the ant target:

  <target name="dtdgen">
    <antstructure output="ant.dtd"/>
  </target>

to generate an "ant.dtd" file for your build.xml directly. Just add the target to your script and execute "ant dtdgen".

(*Chris*)

TheChrisPratt
  • 457
  • 3
  • 13
3

I read somewhere that it is impossible to create a schema for ANT because ANT is not a sufficiently regular grammar. You can test it for conformance with vanilla XML grammar if you use Stylus Studio.

You might also feed some of your working scripts into Stylus Studio and ask it to generate an XSD. Then polish the XSD manually. That would not be full ANT, but it might help catch errors.

My way of handling the problem is not to write ANT XML directly, but to generate it with a stomper program. That way it is guaranteed to be correct once I get the program correct. It is then perfectly consistent across all projects.

see https://wush.net/websvn/mindprod/filedetails.php?repname=mindprod&path=%2Fcom%2Fmindprod%2Fzzz%2FMkAnt.java

for my own stomper code.

user1332994
  • 100
  • 1
  • 1