http://www.blogjava.net/jinfeng_wang/archive/2005/04/01/2688.html
<?xml version="1.0"?>
 <project name="Harnessing Hibernate: The Developer's Notebook"
<project name="Harnessing Hibernate: The Developer's Notebook"
 default="db" basedir=".">
         default="db" basedir=".">

 <!-- Set up properties containing important project directories -->
  <!-- Set up properties containing important project directories -->
 <property name="source.root" value="src"/>
  <property name="source.root" value="src"/>
 <property name="class.root" value="classes"/>
  <property name="class.root" value="classes"/>
 <property name="lib.dir" value="lib"/>
  <property name="lib.dir" value="lib"/>
 <property name="data.dir" value="data"/>
  <property name="data.dir" value="data"/>

 <!-- Set up the class path for compilation and execution -->
  <!-- Set up the class path for compilation and execution -->
 <path id="project.class.path">
  <path id="project.class.path">
 <!-- Include our own classes, of course -->
      <!-- Include our own classes, of course -->
 <pathelement location="${class.root}" />
      <pathelement location="${class.root}" />
 <!-- Include jars in the project library directory -->
      <!-- Include jars in the project library directory -->
 <fileset dir="${lib.dir}">
      <fileset dir="${lib.dir}">
 <include name="*.jar"/>
        <include name="*.jar"/>
 </fileset>
      </fileset>
 </path>
  </path>

 <target name="db" description="Runs HSQLDB database management UI
  <target name="db" description="Runs HSQLDB database management UI
 against the database file--use when application is not running">
against the database file--use when application is not running">
 <java classname="org.hsqldb.util.DatabaseManager"
      <java classname="org.hsqldb.util.DatabaseManager"
 fork="yes">
            fork="yes">
 <classpath refid="project.class.path"/>
         <classpath refid="project.class.path"/>
 <arg value="-driver"/>
         <arg value="-driver"/>
 <arg value="org.hsqldb.jdbcDriver"/>
         <arg value="org.hsqldb.jdbcDriver"/>
 <arg value="-url"/>
         <arg value="-url"/>
 <arg value="jdbc:hsqldb:${data.dir}/music"/>
         <arg value="jdbc:hsqldb:${data.dir}/music"/>
 <arg value="-user"/>
         <arg value="-user"/>
 <arg value="sa"/>
         <arg value="sa"/>
 </java>
      </java>
 </target>
  </target>

 <!-- Teach Ant how to use Hibernate's code generation tool -->
  <!-- Teach Ant how to use Hibernate's code generation tool -->
 <taskdef name="hbm2java"
  <taskdef name="hbm2java"
 classname="net.sf.hibernate.tool.hbm2java.Hbm2JavaTask"
           classname="net.sf.hibernate.tool.hbm2java.Hbm2JavaTask"
 classpathref="project.class.path"/>
           classpathref="project.class.path"/>

 <!-- Generate the java code for all mapping files in our source tree -->
  <!-- Generate the java code for all mapping files in our source tree -->
 <target name="codegen"
  <target name="codegen"
 description="Generate Java source from the O/R mapping files">
          description="Generate Java source from the O/R mapping files">
 <hbm2java output="${source.root}">
    <hbm2java output="${source.root}">
 <fileset dir="${source.root}">
      <fileset dir="${source.root}">
 <include name="**/*.hbm.xml"/>
        <include name="**/*.hbm.xml"/>
 </fileset>
      </fileset>
 </hbm2java>
    </hbm2java>
 </target>
  </target>

 <!-- Create our runtime subdirectories and copy resources into them -->
  <!-- Create our runtime subdirectories and copy resources into them -->
 <target name="prepare" description="Sets up build structures">
  <target name="prepare" description="Sets up build structures">
 <mkdir dir="${class.root}"/>
    <mkdir dir="${class.root}"/>

 <!-- Copy our property files and O/R mappings for use at runtime -->
    <!-- Copy our property files and O/R mappings for use at runtime -->
 <copy todir="${class.root}" >
    <copy todir="${class.root}" >
 <fileset dir="${source.root}" >
      <fileset dir="${source.root}" >
 <include name="**/*.properties"/>
        <include name="**/*.properties"/>
 <include name="**/*.hbm.xml"/>
        <include name="**/*.hbm.xml"/>
 </fileset>
      </fileset>
 </copy>
    </copy>
 </target>
  </target>

 <!-- Compile the java source of the project -->
  <!-- Compile the java source of the project -->
 <target name="compile" depends="prepare"
  <target name="compile" depends="prepare"
 description="Compiles all Java classes">
          description="Compiles all Java classes">
 <javac srcdir="${source.root}"
    <javac srcdir="${source.root}"
 destdir="${class.root}"
           destdir="${class.root}"
 debug="on"
           debug="on"
 optimize="off"
           optimize="off"
 deprecation="on">
           deprecation="on">
 <classpath refid="project.class.path"/>
      <classpath refid="project.class.path"/>
 </javac>
    </javac>
 </target>
  </target>

 <!-- Generate the schemas for all mapping files in our class tree -->
  <!-- Generate the schemas for all mapping files in our class tree -->
 <target name="schema" depends="compile"
  <target name="schema" depends="compile"
 description="Generate DB schema from the O/R mapping files">
          description="Generate DB schema from the O/R mapping files">

 <!-- Teach Ant how to use Hibernate's schema generation tool -->
    <!-- Teach Ant how to use Hibernate's schema generation tool -->
 <taskdef name="schemaexport"
    <taskdef name="schemaexport"
 classname="net.sf.hibernate.tool.hbm2ddl.SchemaExportTask"
             classname="net.sf.hibernate.tool.hbm2ddl.SchemaExportTask"
 classpathref="project.class.path"/>
             classpathref="project.class.path"/>

 <schemaexport properties="${class.root}/hibernate.properties"
    <schemaexport properties="${class.root}/hibernate.properties"
 quiet="no" text="no" drop="no">
                  quiet="no" text="no" drop="no">
 <fileset dir="${class.root}">
      <fileset dir="${class.root}">
 <include name="**/*.hbm.xml"/>
        <include name="**/*.hbm.xml"/>
 </fileset>
      </fileset>
 </schemaexport>
    </schemaexport>
 </target>
  </target>

 </project>
</project>
 
 
You may be wondering why the taskdef for the schema update tool is inside our schema target, rather than at the top of the build file, next to the one for hbm2java. Well, I wanted it up there too, but I ran into a snag that's worth explaining. I got strange error messages the first time I tried to build the schema target, complaining there was no hibernate.properties on the class path and our compiled Track class couldn't be found. When I ran it again, it worked. Some detective work using ant -verbose revealed that if the classes directory didn't exist when the taskdef was encountered, Ant helpfully removed it from the class path. Since a taskdef can't have its own dependencies, the solution is to move it into the schema target, giving it the benefit of that target's dependencies, ensuring the classes directory exists by the time the taskdef is processed.
也许你会奇怪,为何找了schema更新工具的taskdef会在schema的target中进行定义,而不是在build文件的顶部,紧接着hbm2java进行定义。刚开始我也是那样想的,这里我需要解释一下这里的问题。当第一次构建schema时,我得到了许多莫名奇妙的错误,报错信息是:在classpath中找不到hibernate.properties和Track类。但是再次构建的时候,就可以了。当使用命令“ant -verbose”时,你就可以发现其中的细节之所在。如果在ant解析到taskdef时,build文件中所使用的class目录并不存在,那么ant就会自己将此目录从classpath中移除。但是taskdef又无法定义自己的dependencies,因此这里不得不将其移入到schema target中,这样也就可以使用到target的dependencies,保证在ant处理taskdef的时候,class目录必然已经存在。