Ant+XDoclet工具使用

Ant+XDoclet工具使用
Ant使用:
1、下载ant并解压到硬盘的某个位置
2、配置ant的环境变量ANT_HOME=D:\commons\apache-ant-1.7.1  path=%ANT_HOME%\bin
3、测试ant是否安装成功。
运行cmd命令(小窗口+r可以调出运行窗口键入cmd)进入dos窗口,键入ant回车运行得到
Buildfile: build.xml does not exist!
Build failed
这就说明ant安装成功。
4、创建build.xml文件

Xml代码
  1. <? xml   version = "1.0"   encoding = "gbk" ?>   
  2. < project   name = "测试脚本"   default = "copyfile"   basedir = "." >   
  3.     < target   name = "copyfile"   >   
  4.         < copy    file = "d:/a.txt"   todir = "e:/Temp"   overwrite = "true" />   
  5.     </ target >   
  6. </ project >   
<?xml version="1.0" encoding="gbk"?>
<project name="测试脚本" default="copyfile" basedir=".">
    <target name="copyfile" >
        <copy  file="d:/a.txt" todir="e:/Temp" overwrite="true"/>
    </target>
</project>


直接运行ant,结果:
D:\>ant
Buildfile: build.xml
copyfile:
     [copy] Copying 1 f
BUILD SUCCESSFUL
Total time: 0 seconds
测试ant成功!

**/CVS/*
两个**代表当前目录下的所有文件以及所有的子目录
/CVS代表当前目录下或者子目录下只要有CVS这个目录
/*代表CVS下的所有文件

XDoclet使用:
下载http://xdoclet.codehaus.org/
Ant+XDoclet:
OA工程里创建一个需要映射的对象

Java代码
  1. package  com.oa.model;  
  2. /**  
  3.  * @author lukuijun  
  4.  * @hibernate.class table="t_user"  
  5.  */   
  6. public   class  User {  
  7.     /**  
  8.      * @hibernate.id  
  9.      * generator-class="native"  
  10.      */   
  11.     private   int  id;  
  12.     /**  
  13.      * @hibernate.property  
  14.      */   
  15.     private  String username;  
  16.     /**  
  17.      * @hibernate.property  
  18.      */   
  19.     private  String password;  
  20.     public   int  getId() {  
  21.         return  id;  
  22.     }  
  23.     public   void  setId( int  id) {  
  24.         this .id = id;  
  25.     }  
  26.     public  String getUsername() {  
  27.         return  username;  
  28.     }  
  29.     public   void  setUsername(String username) {  
  30.         this .username = username;  
  31.     }  
  32.     public  String getPassword() {  
  33.         return  password;  
  34.     }  
  35.     public   void  setPassword(String password) {  
  36.         this .password = password;  
  37.     }  
  38. }  
package com.oa.model;
/**
 * @author lukuijun
 * @hibernate.class table="t_user"
 */
public class User {
	/**
	 * @hibernate.id
	 * generator-class="native"
	 */
	private int id;
	/**
	 * @hibernate.property
	 */
	private String username;
	/**
	 * @hibernate.property
	 */
	private String password;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
}

建立build.xml

Xml代码
  1. <? xml   version = "1.0"   encoding = "UTF-8" ?>   
  2. < project   name = "OA系统构建脚本"   default = "生成Hibernate配置脚本"   basedir = "." >   
  3.     < property   name = "src.dir"   value = "${basedir}/src" />   
  4.     < property   name = "xdoclet.home"   value = "D:/commons/xdoclet-plugins-dist-1.0.4" />   
  5.       
  6.     <!-- build classpath -->   
  7.     < path   id = "xdoclet.task.classpath" >   
  8.         < fileset   dir = "${xdoclet.home}/lib" >   
  9.             < include   name = "**/*.jar" />   
  10.         </ fileset >   
  11.     </ path >   
  12.       
  13.     < taskdef   
  14.         name = "xdoclet"   
  15.         classname = "org.xdoclet.ant.XDocletTask"   
  16.         classpathref = "xdoclet.task.classpath"   
  17.     />   
  18.       
  19.     < target   name = "生成Hibernate配置文件" >   
  20.         < xdoclet >   
  21.             < fileset   dir = "${src.dir}/com/oa/model" >   
  22.                 < include   name = "**/*.java" />   
  23.             </ fileset >   
  24.         </ xdoclet >   
  25.         < component   
  26.             classname = "org.xdoclet.plugin.hibernate.HibernateConfigPlugin"   
  27.             destdir = "${src.dir}"   
  28.             version = "3.0"   
  29.             jdbcurl = "jdbc:mysql://localhost/oa"   
  30.             jdbcdriver = "com.mysql.jdbc.Driver"   
  31.             jdbcusername = "root"   
  32.             jdbcpassword = "lukuijun"   
  33.             dialect = "org.hibernate.dialect.MySQLDialect"   
  34.             showsql = "true"   
  35.          />   
  36.     </ target >   
  37.       
  38.     < target   name = "生成hibernate映射文件" >   
  39.         < xdoclet >   
  40.             < fileset   dir = "${src.dir}/com/oa/model" >   
  41.                 < include   name = "**/*.java" />   
  42.             </ fileset >   
  43.             < component   
  44.                 classname = "org.xdoclet.plugin.hibernate.HibernateMappingPlugin"   
  45.                 version = "3.0"   
  46.                 destdir = "${src.dir}"   
  47.              />   
  48.         </ xdoclet >   
  49.     </ target >   
  50. </ project >   
<?xml version="1.0" encoding="UTF-8"?>
<project name="OA系统构建脚本" default="生成Hibernate配置脚本" basedir=".">
    <property name="src.dir" value="${basedir}/src"/>
    <property name="xdoclet.home" value="D:/commons/xdoclet-plugins-dist-1.0.4"/>
    
    <!-- build classpath -->
    <path id="xdoclet.task.classpath">
        <fileset dir="${xdoclet.home}/lib">
            <include name="**/*.jar"/>
        </fileset>
    </path>
    
    <taskdef
        name="xdoclet"
        classname="org.xdoclet.ant.XDocletTask"
        classpathref="xdoclet.task.classpath"
    />
    
    <target name="生成Hibernate配置文件">
        <xdoclet>
            <fileset dir="${src.dir}/com/oa/model">
                <include name="**/*.java"/>
            </fileset>
        </xdoclet>
        <component
            classname="org.xdoclet.plugin.hibernate.HibernateConfigPlugin"
            destdir="${src.dir}"
            version="3.0"
            jdbcurl="jdbc:mysql://localhost/oa"
            jdbcdriver="com.mysql.jdbc.Driver"
            jdbcusername="root"
            jdbcpassword="lukuijun"
            dialect="org.hibernate.dialect.MySQLDialect"
            showsql="true"
         />
    </target>
    
    <target name="生成hibernate映射文件">
        <xdoclet>
            <fileset dir="${src.dir}/com/oa/model">
                <include name="**/*.java"/>
            </fileset>
            <component
                classname="org.xdoclet.plugin.hibernate.HibernateMappingPlugin"
                version="3.0"
                destdir="${src.dir}"
             />
        </xdoclet>
    </target>
</project>


说明一点:build.xml文件在保存的时候格式一定要和encoding="UTF-8"一致,否则报错Invalid byte 1 of 1-byte UTF-8 sequence.错误的原因就是因为xml指定了utf-8格式,但保存的时候不是utf-8编码, 解决办法是用记事本打开之后,保存时候指定编码为utf-8编码。
下面运行ant就生成hibernate的配置文件和映射文件

你可能感兴趣的:(Hibernate,mysql,xml,ant,cvs)