功能描述:
1.ant hibernate:由class xdoclet 得到映射.
2.hbm2ddl action
2.0.建立此动作文件
2.1.备份数据库,并重建数据库
2.2.建立hbm2ddl标签并建create table sql 文件
2.3.执行sql文件 basicData.sql ; test.sql
3.junit 集成测试
lib 需要
- commons-collections-2.0.jar
- commons-logging.jar
- dom4j-1.6.1.jar
- hibernate3.jar
- hibernate-tools.jar
- junit.jar
- mysql-connector-java-5.0.8-bin.jar
- xdoclet-1.2.3.jar
- xdoclet-hibernate-module-1.2.3.jar
- xdoclet-xdoclet-module-1.2.3.jar
- xjavadoc-1.1.jar
文件路径
ant---------
+-- basicData.sql 系统数据库建表
|-- build-dist.properties 项目配置
|--test.sql 插入测试数据
build.xml
build.xml
<?xml version="1.0" encoding="utf-8"?>
<!--
维护
path->hibernatedoclet.class.path
path->tproject.class.path
-->
<project name="XDoclet Examples" basedir=".">
<property name="xdoclet.root.dir" value="${basedir}/.." />
<property file="${xdoclet.root.dir}/build.properties" />
<property file="./ant/build-dist.properties" />
<path id="hibernatedoclet.class.path">
<fileset dir="${xdoclet2.lib.dir}">
<include name="*.jar" />
</fileset>
</path>
<path id="tproject.class.path">
<fileset dir="${tproject.lib}">
<include name="**/*.jar" />
</fileset>
<fileset dir="${tproject.classpath}">
<include name="**/*.*" />
</fileset>
<fileset dir="${xdoclet2.lib.dir}">
<include name="*.jar" />
</fileset>
</path>
<!-- =================================================================== -->
<!-- Initialise -->
<!-- =================================================================== -->
<target name="init">
<tstamp>
<format property="Ttstamp" pattern="yyyy_MM_dd_HH_mm_ss" />
</tstamp>
<taskdef name="hibernatedoclet" classname="xdoclet.modules.hibernate.HibernateDocletTask" classpathref="hibernatedoclet.class.path" />
</target>
<!-- =================================================================== -->
<!-- Invoke XDoclet's hibernate -->
<!-- =================================================================== -->
<target name="hibernate" depends="init" description="Generate mapping documents (run jar first)">
<echo>+---------------------------------------------------+</echo>
<echo>| |</echo>
<echo>| R U N N I N G H I B E R N A T E D O C L E T |</echo>
<echo>| |</echo>
<echo>+---------------------------------------------------+</echo>
<hibernatedoclet destdir="${tproject.conf}" excludedtags="@version,@author,@todo,@see" addedtags="@xdoclet-generated at ${TODAY},@copyright The XDoclet Team,@author XDoclet,@version ${version}" force="${samples.xdoclet.force}" verbose="true">
<!-- hibernate 需要维护的 fileset -->
<fileset dir="demo">
<include name="**/*Bean.java" />
</fileset>
<hibernate version="3.0" />
</hibernatedoclet>
</target>
<!-- ===================================================================
hbm2ddl action
0.建立此动作文件
1.备份数据库,并重建数据库
2.建立hbm2ddl标签并建create table sql 文件
3.执行sql文件
=================================================================== -->
<target name="hbm2ddl" depends="init">
<echo>this->create table ENGINE=InnoDB DEFAULT CHARSET=utf8 </echo>
<echo>--c:\\my.ini--</echo>
<echo>
[mysqld]
default-storage-engine=InnoDB
default-character-set=utf8
[mysql]
default-character-set=utf8
</echo>
<property name="this.sql.path" value="${tproject.conf.sql}/${Ttstamp}" />
<mkdir dir="${this.sql.path}" />
<echo message="for ${hibernate.cfg.xml.dir} -> ${hibernate.cfg.xml.file}" />
<property name="sql.file" value="hbm2ddl.sql" />
<echo message=" dump mysql database - > ${use.database}" />
<property name="hibernate.cfg.jdbc.url" value="jdbc:mysql://localhost:3306/${use.database}?characterEncoding=${characterEncoding}" />
<property name="dbAction.jdbc.url" value="jdbc:mysql://localhost:3306/mysql?characterEncoding=${characterEncoding}" />
<!--1.备份数据库,并重建数据库 -->
<sql driver="${hibernate.cfg.driver}" url="${dbAction.jdbc.url}" userid="${hibernate.cfg.username}" password="${hibernate.cfg.password}" print="yes" >
<classpath refid="tproject.class.path" />
create database if not exists ${use.database} ;
</sql>
<exec executable="cmd">
<arg line="/c ${mysql.home}/bin/mysqldump --opt ${use.database} -u${hibernate.cfg.username} > ${this.sql.path}/dump.sql" />
</exec>
<sql driver="${hibernate.cfg.driver}" url="${dbAction.jdbc.url}" userid="${hibernate.cfg.username}" password="${hibernate.cfg.password}" print="yes" >
<classpath refid="tproject.class.path" />
drop database if exists ${use.database} ;
create database ${use.database} ;
</sql>
<!--建立hbm2ddl标签并建create table sql 文件 -->
<taskdef name="hbm2ddl" classname="org.hibernate.tool.ant.HibernateToolTask" classpathref="hibernatedoclet.class.path">
<classpath location="${tproject.conf}">
</classpath>
<classpath>
<path>
<fileset dir="${tproject.conf}">
<include name="**/*.hbm.xml" />
</fileset>
</path>
</classpath>
</taskdef>
<echo message=" hbm2ddl database by - > ${use.database} ${hibernate.cfg.xml.file} " />
<echo message=" hbm2ddl path = ${this.sql.path} " />
<echo message=" hbm2ddl file = ${sql.file} " />
<hbm2ddl destdir="${this.sql.path}">
<configuration configurationfile="${hibernate.cfg.xml.file}" />
<hbm2ddl export="false" console="false" create="true" update="false" drop="true" outputfilename="${sql.file}" />
</hbm2ddl>
<!-- 执行sql文件 -->
<sql driver="${hibernate.cfg.driver}" url="${hibernate.cfg.jdbc.url}" userid="${hibernate.cfg.username}" password="${hibernate.cfg.password}" onerror="continue" print="yes" src="${this.sql.path}/${sql.file}" classpathref="hibernatedoclet.class.path" />
<sql driver="${hibernate.cfg.driver}" url="${hibernate.cfg.jdbc.url}" userid="${hibernate.cfg.username}" password="${hibernate.cfg.password}" onerror="continue" print="yes" src="./ant/basicData.sql" classpathref="hibernatedoclet.class.path" />
<sql driver="${hibernate.cfg.driver}" url="${hibernate.cfg.jdbc.url}" userid="${hibernate.cfg.username}" password="${hibernate.cfg.password}" onerror="continue" print="yes" src="./ant/test.sql" classpathref="hibernatedoclet.class.path" />
</target>
<target name="make resource">
<delete file="${tproject.conf}/globalMessages_zh_CN.properties">
</delete>
<exec dir="${tproject.conf}" executable="native2ascii">
<arg line="
globalMessages.properties
globalMessages_zh_CN.properties" />
</exec>
</target>
<!-- =================================================================== -->
<!-- junit -->
<!-- =================================================================== -->
<target name="test">
<mkdir dir="${tproject.conf}/htmlreport" />
<mkdir dir="${tproject.conf}/report" />
<junit>
<classpath refid="tproject.class.path" />
<classpath path="${tproject.classpath}">
</classpath>
<formatter type="xml" />
<batchtest haltonfailure="no" todir="${tproject.conf}/report">
<!-- junit 需要维护的 fileset -->
<fileset dir="./demo">
<include name="**/Test*.java" />
</fileset>
</batchtest>
</junit>
<junitreport todir="${tproject.conf}/htmlreport">
<fileset dir="${tproject.conf}/report">
<include name="TEST-*.xml" />
</fileset>
<report format="noframes" todir="${tproject.conf}/htmlreport" />
</junitreport>
<echo message="end test -> print->${tproject.conf}/htmlreport/junit-noframes.html " />
</target>
</project>
<!--
维护
path->hibernatedoclet.class.path
path->tproject.class.path
-->
<project name="XDoclet Examples" basedir=".">
<property name="xdoclet.root.dir" value="${basedir}/.." />
<property file="${xdoclet.root.dir}/build.properties" />
<property file="./ant/build-dist.properties" />
<path id="hibernatedoclet.class.path">
<fileset dir="${xdoclet2.lib.dir}">
<include name="*.jar" />
</fileset>
</path>
<path id="tproject.class.path">
<fileset dir="${tproject.lib}">
<include name="**/*.jar" />
</fileset>
<fileset dir="${tproject.classpath}">
<include name="**/*.*" />
</fileset>
<fileset dir="${xdoclet2.lib.dir}">
<include name="*.jar" />
</fileset>
</path>
<!-- =================================================================== -->
<!-- Initialise -->
<!-- =================================================================== -->
<target name="init">
<tstamp>
<format property="Ttstamp" pattern="yyyy_MM_dd_HH_mm_ss" />
</tstamp>
<taskdef name="hibernatedoclet" classname="xdoclet.modules.hibernate.HibernateDocletTask" classpathref="hibernatedoclet.class.path" />
</target>
<!-- =================================================================== -->
<!-- Invoke XDoclet's hibernate -->
<!-- =================================================================== -->
<target name="hibernate" depends="init" description="Generate mapping documents (run jar first)">
<echo>+---------------------------------------------------+</echo>
<echo>| |</echo>
<echo>| R U N N I N G H I B E R N A T E D O C L E T |</echo>
<echo>| |</echo>
<echo>+---------------------------------------------------+</echo>
<hibernatedoclet destdir="${tproject.conf}" excludedtags="@version,@author,@todo,@see" addedtags="@xdoclet-generated at ${TODAY},@copyright The XDoclet Team,@author XDoclet,@version ${version}" force="${samples.xdoclet.force}" verbose="true">
<!-- hibernate 需要维护的 fileset -->
<fileset dir="demo">
<include name="**/*Bean.java" />
</fileset>
<hibernate version="3.0" />
</hibernatedoclet>
</target>
<!-- ===================================================================
hbm2ddl action
0.建立此动作文件
1.备份数据库,并重建数据库
2.建立hbm2ddl标签并建create table sql 文件
3.执行sql文件
=================================================================== -->
<target name="hbm2ddl" depends="init">
<echo>this->create table ENGINE=InnoDB DEFAULT CHARSET=utf8 </echo>
<echo>--c:\\my.ini--</echo>
<echo>
[mysqld]
default-storage-engine=InnoDB
default-character-set=utf8
[mysql]
default-character-set=utf8
</echo>
<property name="this.sql.path" value="${tproject.conf.sql}/${Ttstamp}" />
<mkdir dir="${this.sql.path}" />
<echo message="for ${hibernate.cfg.xml.dir} -> ${hibernate.cfg.xml.file}" />
<property name="sql.file" value="hbm2ddl.sql" />
<echo message=" dump mysql database - > ${use.database}" />
<property name="hibernate.cfg.jdbc.url" value="jdbc:mysql://localhost:3306/${use.database}?characterEncoding=${characterEncoding}" />
<property name="dbAction.jdbc.url" value="jdbc:mysql://localhost:3306/mysql?characterEncoding=${characterEncoding}" />
<!--1.备份数据库,并重建数据库 -->
<sql driver="${hibernate.cfg.driver}" url="${dbAction.jdbc.url}" userid="${hibernate.cfg.username}" password="${hibernate.cfg.password}" print="yes" >
<classpath refid="tproject.class.path" />
create database if not exists ${use.database} ;
</sql>
<exec executable="cmd">
<arg line="/c ${mysql.home}/bin/mysqldump --opt ${use.database} -u${hibernate.cfg.username} > ${this.sql.path}/dump.sql" />
</exec>
<sql driver="${hibernate.cfg.driver}" url="${dbAction.jdbc.url}" userid="${hibernate.cfg.username}" password="${hibernate.cfg.password}" print="yes" >
<classpath refid="tproject.class.path" />
drop database if exists ${use.database} ;
create database ${use.database} ;
</sql>
<!--建立hbm2ddl标签并建create table sql 文件 -->
<taskdef name="hbm2ddl" classname="org.hibernate.tool.ant.HibernateToolTask" classpathref="hibernatedoclet.class.path">
<classpath location="${tproject.conf}">
</classpath>
<classpath>
<path>
<fileset dir="${tproject.conf}">
<include name="**/*.hbm.xml" />
</fileset>
</path>
</classpath>
</taskdef>
<echo message=" hbm2ddl database by - > ${use.database} ${hibernate.cfg.xml.file} " />
<echo message=" hbm2ddl path = ${this.sql.path} " />
<echo message=" hbm2ddl file = ${sql.file} " />
<hbm2ddl destdir="${this.sql.path}">
<configuration configurationfile="${hibernate.cfg.xml.file}" />
<hbm2ddl export="false" console="false" create="true" update="false" drop="true" outputfilename="${sql.file}" />
</hbm2ddl>
<!-- 执行sql文件 -->
<sql driver="${hibernate.cfg.driver}" url="${hibernate.cfg.jdbc.url}" userid="${hibernate.cfg.username}" password="${hibernate.cfg.password}" onerror="continue" print="yes" src="${this.sql.path}/${sql.file}" classpathref="hibernatedoclet.class.path" />
<sql driver="${hibernate.cfg.driver}" url="${hibernate.cfg.jdbc.url}" userid="${hibernate.cfg.username}" password="${hibernate.cfg.password}" onerror="continue" print="yes" src="./ant/basicData.sql" classpathref="hibernatedoclet.class.path" />
<sql driver="${hibernate.cfg.driver}" url="${hibernate.cfg.jdbc.url}" userid="${hibernate.cfg.username}" password="${hibernate.cfg.password}" onerror="continue" print="yes" src="./ant/test.sql" classpathref="hibernatedoclet.class.path" />
</target>
<target name="make resource">
<delete file="${tproject.conf}/globalMessages_zh_CN.properties">
</delete>
<exec dir="${tproject.conf}" executable="native2ascii">
<arg line="
globalMessages.properties
globalMessages_zh_CN.properties" />
</exec>
</target>
<!-- =================================================================== -->
<!-- junit -->
<!-- =================================================================== -->
<target name="test">
<mkdir dir="${tproject.conf}/htmlreport" />
<mkdir dir="${tproject.conf}/report" />
<junit>
<classpath refid="tproject.class.path" />
<classpath path="${tproject.classpath}">
</classpath>
<formatter type="xml" />
<batchtest haltonfailure="no" todir="${tproject.conf}/report">
<!-- junit 需要维护的 fileset -->
<fileset dir="./demo">
<include name="**/Test*.java" />
</fileset>
</batchtest>
</junit>
<junitreport todir="${tproject.conf}/htmlreport">
<fileset dir="${tproject.conf}/report">
<include name="TEST-*.xml" />
</fileset>
<report format="noframes" todir="${tproject.conf}/htmlreport" />
</junitreport>
<echo message="end test -> print->${tproject.conf}/htmlreport/junit-noframes.html " />
</target>
</project>
配置文件:(我测试项目的)
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
![](http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)
![](http://www.blogjava.net/Images/OutliningIndicators/None.gif)