使用ANT从Hibernate注释类创建DDL脚本

时间:2012-06-03 02:24:41

标签: sql hibernate java-ee orm ant

我正在尝试使用ANT 从我的Hibernate注释bean生成SQL DDL,根据hibernate工具文档,我创建了以下Ant脚本:

<?xml version="1.0" encoding="UTF-8"?>
<project name="yourmarketnet" default="all" basedir=".">
<property name="build.dir" value="C:/Users/naim/Documents/NetBeansProjects/yourmarketnet/build" />
<target name="ddl_generation">
<!-- paths to required jars  -->
<path location="web/WEB-INF/lib/hibernate-annotations.jar" />
<path location="web/WEB-INF/lib/ejb3-persistence.jar" />
<path location="web/WEB-INF/lib/hibernate-entitymanager.jar" />
<path location="web/WEB-INF/lib/javaassist.jar" />
<path location="web/WEB-INF/lib/hibernate-tools.jar"/>
<path location="web/WEB-INF/lib/hibernate-entitymanager.jar" />
<path location="web/WEB-INF/lib/jboss-archive-browsing.jar" />
<path location="web/WEB-INF/lib/javaassist.jar" />

<taskdef name="hibernatetool" classname="org.hibernate.tool.ant.HibernateToolTask">
    </taskdef>
    <classpath>
    <!-- path of annotation beans -->
<path location="${build.dir}/web/WEB-INF/classes/com/yourmarketnet/beans" />
</classpath>
<!-- output destination -->
<hibernatetool destdir="${build.dir}">
<!-- were the annotation beans files are located-->
<!-- list exporters here -->
<hbm2ddl
export="false"
update="false"
drop="true"
create="true"
outputfilename="myApps.ddl"
delimiter=";"
format="false"
haltonerror="true"/>
</hibernatetool>
</target>
</project>

但是我收到以下错误:

taskdef类无法找到org.hibernate.tool.ant.HibernateToolTask  使用类加载器AntClassLoader []

我检查了我/ lib&amp;文件夹和类路径hibernate-tools存在。

1 个答案:

答案 0 :(得分:1)

在我的ant脚本中,我通常使用lib文件夹的绝对路径定义一个名为lib.dir的属性,并使用标记加载我的所有jar,因为出于某种原因的相对路径一直是个问题。我。也可能值得你一试。

<property name="lib.dir" value="C:\path\to\project\WebContent\WEB-INF\lib"/>
<property name="jdk.home" value="C:\path\to\jdk" />
<path id="project.classpath">
    <fileset dir="${lib.dir}">
        <include name="*.jar"/>
    </fileset>
    <pathelement location="${jdk.home}/jre/javaws/javaws.jar"/>
    <pathelement location="${jdk.home}/jre/lib/charsets.jar"/>
    <pathelement location="${jdk.home}/jre/lib/ext/dnsns.jar"/>
    <pathelement location="${jdk.home}/jre/lib/ext/ldapsec.jar"/>
    <pathelement location="${jdk.home}/jre/lib/ext/localedata.jar"/>
    <pathelement location="${jdk.home}/jre/lib/ext/sunjce_provider.jar"/>
    <pathelement location="${jdk.home}/jre/lib/im/indicim.jar"/>
    <pathelement location="${jdk.home}/jre/lib/im/thaiim.jar"/>
    <pathelement location="${jdk.home}/jre/lib/jce.jar"/>
    <pathelement location="${jdk.home}/jre/lib/jsse.jar"/>
    <pathelement location="${jdk.home}/jre/lib/plugin.jar"/>
    <pathelement location="${jdk.home}/jre/lib/rt.jar"/>
    <pathelement location="${jdk.home}/jre/lib/sunrsasign.jar"/>
    <pathelement location="${jdk.home}/lib/dt.jar"/>
    <pathelement location="${jdk.home}/lib/htmlconverter.jar"/>
    <pathelement location="${jdk.home}/lib/tools.jar"/>
</path>
...
// Rest of your build file
...