Xdoclet结合Ant使用

XDoclet可以通过你在java源代码中的一些特殊的注释信息,自动为你生成配置文件、源代码等等,例如webejb的部署描述文件、为你生成strutsstruts-config.xml配置文件、javascript校验等。

XDoclet is an open source code generation engine. It enables Attribute-Oriented Programming for java. In short, this means that you can add more significance to your code by adding meta data (attributes) to your java sources. This is done in special JavaDoc tags. This use of JavaDoc tags for attributes formed the original ideas for Java 5 Annotations.

XDoclet2 is the next generation of this technology. Based on Generama, it uses standard template engines such as Velocity and Freemarker for generation of text-oriented output, and Jelly for XML output. The function of XDoclet is to seed the generation contexts for these template engines.

创建build.xml配置文件:

<?xml version="1.0" encoding="GBK"?>

<projectname="系统构建脚本"default="生成Hibernate配置文件"basedir=".">

<propertyname="src.dir"value="${basedir}/src"/>

<propertyname="xdoclet.home"value="F:/Java/xdoclet-plugins-dist-1.0.4"/>

<!-- Build classpath -->

<pathid="xdoclet.task.classpath">

<filesetdir="${xdoclet.home}/lib">

<includename="**/*.jar"/>

</fileset>

<!--

<fileset dir="${xdoclet.home}/plugins">

<include name="**/*.jar"/>

</fileset>

-->

</path>

<taskdef

name="xdoclet"

classname="org.xdoclet.ant.XDocletTask"

classpathref="xdoclet.task.classpath"

/>

<targetname="生成Hibernate配置文件">

<xdoclet>

<filesetdir="${src.dir}/com/sjg/oa/model">

<includename="**/*.java"/>

</fileset>

<component

classname="org.xdoclet.plugin.hibernate.HibernateConfigPlugin"

destdir="${src.dir}"

version="3.0"

hbm2ddlauto="update"

jdbcurl="jdbc:mysql://127.0.0.1/oa"

jdbcdriver="com.mysql.jdbc.Driver"

jdbcusername="root"

jdbcpassword="123456"

dialect="org.hibernate.dialect.MySQLDialect"

showsql="true"

/>

</xdoclet>

</target>

<targetname="生成hibernate映射文件">

<xdoclet>

<filesetdir="${src.dir}/com/sjg/oa/model">

<includename="**/*.java"/>

</fileset>

<component

classname="org.xdoclet.plugin.hibernate.HibernateMappingPlugin"

version="3.0"

destdir="${src.dir}"

/>

</xdoclet>

</target>

</project>


User.java文件:(注释产生配置)

package com.sjg.oa.model;

/**

*

* @author SJG

* @hibernate.class table="t_user"

*/

publicclass User {

/**

* @hibernate.id

* generator-class="native"

*/

privateintid;

/**

* @hibernate.property

*

*/

private String username;

/**

* @hibernate.property

*

*/

private String password;

publicint getId() {

returnid;

}

publicvoid setId(int id) {

this.id = id;

}

public String getUsername() {

returnusername;

}

publicvoid setUsername(String username) {

this.username = username;

}

public String getPassword() {

returnpassword;

}

publicvoid setPassword(String password) {

this.password = password;

}

}


执行这两步,可以产生文件:

115231495.png

成功结果:


User.hbm.xml文件

115307981.png


Hibernate.cfg.xml配置文件


115403575.png

你可能感兴趣的:(ant,xdoclet)