CS636 XML and Ant Examples

Generic XML ID/IDREF example  

Here the id attribute of person is of type ID, while the mother and father attributes of person and the ownerref attribute of dog are of type IDREF, as specified in the DTD (not shown).  Note that the ID does not have to be named "id", but that's common.  The IDREF has a name based on its actual use or "refid", etc.

   <person id="p01" name="Sue"> The Mom </person>
   <person id="p02" name="Jack"> The Dad </person>
   <person id="p03" name="Dave" mother="p01" father="po2"> The first kid </person>
   <person id="p04" name="Sally" mother="p01" father="po2"> The second kid </person>

<dog name="Spot" ownerref="p01"> The dog </dog>

Ant example 1, simplified (not using jar) from Apache's Hello World tutorial

<?xml version="1.0"?>                       <--XML document should start with this

    <target name="clean">

        <delete dir="build"/>                 ß recursive directory deletion, portable


    <target name="compile">
        <mkdir dir="build/classes"/>           ß ant maps / to \ for Windows
        <javac srcdir="src" destdir="build/classes"/>       
    <target name="run">
        <java fork="true" classpath="build/classes" classname="oata.HelloWorld" />

Now you can compile, package and run the application via

ant compile

ant run

Or shorter with

ant compile run


Ant example 2, uses properties, dependencies: This is changed somewhat from Apache's Hello World tutorial—uses project.classpath too

<project name="HelloWorld" basedir="." default="main">
    <property name="src.dir"     value="src"/>
    <property name="build.dir"   value="build"/>
    <property name="classes.dir" value="${build.dir}/classes"/>
    <property name="main-class"  value="oata.HelloWorld"/>
    <!-- set up classpath using path element having id for target of refid -->
    <path id="project.classpath">
         <pathelement location="${classes.dir}"/>   <!--classpath def-->
    <target name="clean">
        <delete dir="${build.dir}"/>
    <target name="compile">
        <mkdir dir="${classes.dir}"/>
        <javac srcdir="${src.dir}" destdir="${classes.dir}"/>
    <target name="run" fork=”true” depends="compile">
        <java classname="${main-class}" fork="true">
              <classpath refid="project.classpath"/>   <!--classpath ref-->
    <target name="clean-build" depends="clean,compile"/>
    <target name="main" depends="clean,run"/>

Now it's easier, just do “ant” and you will get execution of targets clean, compile, and run.

Part of build.xml from pizza1/database: getting the drivers into the classpath. With this,
“ant start-hsqldb” starts up HSQLDB. Much easier!

   <path id="project.classpath">
      <pathelement location="../lib/ojdbc6.jar" />
location="../lib/hsqldb.jar" />
location="../lib/mysql-connector-java-5.1.43-bin.jar" />

or using a wildcard expression for any jar file in ../lib:

    <path id="project.classpath">
        <fileset dir="../lib">
            <include name="**/*.jar" />