xdoclet 工具使用

 

Xdoclet可以干什么?

Xdoclet实际上就是一个自动代码生成的工具,它可以生成各种各样的代码或配置文件(如果你很清楚xdoclet,你也可以对它进行扩展)。在默认的情况下,xdoclet已经给我们提供了很多的解决方案。这些解决方案叫做plugin,即插件。在xdoclet的官方网站上:http://xdoclet.codehaus.org,我们可以获得关于它支持的所有的plugin的情况。

 

Xdoclet通过plugin来支持代码的自动生成。我们要使用xdoclet,可以将它定义为ant的一个任务(如上所述)。然后就可以使用xdoclet了。在<xdoclet>标签内部使用xdoclet。由于xdoclet通常用来对源文件进行扫描,并读取源文件中的注释,然后再根据这些注释生成相应的配置文件,所以,通常我们需要定义让xdoclet扫描哪些源代码文件。对于这个需要,我们通过<fileset>标签来满足!

 

通过<component>标签,我们可以来使用xdoclet的众多plugin。上述例子,我们使用了xdoclet对hibernate支持的plugin。

具体如何使用这些plugin,请参考这些plugin的使用手册!!

Xdoclet基本概念

我们可以在java代码中使用类似于javadoc的注释,来表达更多的内容。这些额外的注释,通过使用xdoclet工具,我们可以将它们转换为我们需要的各种配置文件。先看一个简单的例子:

比如有一个java文件的源代码如下:

/**

 * @hibernate.class

 *          table="T_Party"

 */

public class Party {

      

       /**

        * @hibernate.id

        *          generator-class="native"

        */

       private int id;

      

       /**

        * @hibernate.many-to-one

        *          column="parentid" 

        */

       private Party parent;

      

       /**

        * @hibernate.set

        * @hibernate.key

        *   column = "parentid"

        * @hibernate.one-to-many

        *          class = "com.bjsxt.oa.model.Party"     

        */

       private Set children;

      

       /**

        * @hibernate.property

        *          column="thename"

        */  

       private String name;

      

       /**

        * @hibernate.property

        */

       private String sn;

      

       /**

        * @hibernate.property

        */

       private String description;

      

       /**

        *

        * @return

        */

       public Set getChildren() {

              return children;

       }

       public void setChildren(Set children) {

              this.children = children;

       }

       public String getDescription() {

              return description;

       }

       public void setDescription(String description) {

              this.description = description;

       }

      

       /**

        */  

       public int getId() {

              return id;

       }

       public void setId(int id) {

              this.id = id;

       }

      

 

       public String getName() {

              return name;

       }

       public void setName(String name) {

              this.name = name;

       }

       public Party getParent() {

              return parent;

       }

       public void setParent(Party parent) {

              this.parent = parent;

       }

       public String getSn() {

              return sn;

       }

       public void setSn(String sn) {

              this.sn = sn;

       }

}

 

 

通过xdoclet,我们可以得到关于这个类的Hibernate映射文件,如下:

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

 

<hibernate-mapping>

  <class table="T_Party" name="com.bjsxt.oa.model.Party">

    <id name="id">

      <generator class="native"/>

    </id>

    <many-to-one column="parentid" name="parent"/>

    <set name="children">

      <key column="parentid"/>

      <one-to-many class="com.bjsxt.oa.model.Party"/>

    </set>

    <property name="name" column="thename"/>

    <property name="sn"/>

    <property name="description"/>

  </class>

</hibernate-mapping>

 

 

如何开始使用Xdoclet?

Xdoclet不是单独可以运行的工具(不像Ant工具),它可以与其它工具一起配合运行,如Ant。我们下面的例子就是基于Ant和xdoclet的。

 

1、  首先需要保证ant是可用的

2、  下载并解压xdoclet的包(我们现在使用的是xdoclet2,具体版本是xdoclet-plugins-1.0.3)。

3、  在ant构建工具中定义xdoclet任务,并使用:

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

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

 

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

   <property name="build.dir" value="${basedir}/bin"/>

    <property name="webapp.dir" value="${basedir}/src/webapp"/>

    <property name="xdoclet.home" value="D:/opensources/xdoclet/xdoclet-plugins-1.0.3"/>

 

   <!-- Build classpath -->

   <path id="xdoclet.task.classpath">

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

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

         </fileset>

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

         <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/bjsxt/oa/model">

              <include name="**/*.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_200706"

              jdbcdriver="com.mysql.jdbc.Driver"

              jdbcusername="root"

              jdbcpassword="mysql"

              dialect="org.hibernate.dialect.MySQLDialect"

              showsql="true"

           />

       </xdoclet>

    </target>

    <target name="生成hibernate映射文件">

       <xdoclet>

           <fileset dir="${src.dir}/com/bjsxt/oa/model">

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

           </fileset>

           <component

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

              version="3.0"

              destdir="${src.dir}"

           />

       </xdoclet>

    </target>

</project>

 

 

 

以上就是一个完整的可运行的ant构建脚本。我们努力来理解这个文件把:

 

1、<property>标签定义一些变量,这些变量可以通过${变量名}的方式引用

2、<path>标签定义了类路径

3、<taskdef>标签定义了xdoclet任务(因为ant本身肯定是不包含xdoclet任务的)

4、我们在下面的两个<target>中,使用了<xdoclet>标签,这个标签正是我们自己定义的。

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值