|
XDoclet by example
|
|
Monday, October 18, 2004
XDoclet is an open-source code-generation engine. Building the enterprise applications in Java involves working with Enterprise Java Beans (EJB) and that means writing a lot of deployment decriptors for your application configuration, a time-consuming job. XDoclet helps you avoid this overhead by generating these descriptors and other source code files for you. XDoclet was initially aimed at simplifying EJB development but its scope is no longer limited to EJB. You specify certain tags similar to Javadoc tags in your source code and XDoclet reads those tags to decide how to generate the required source files. A typical XDoclet tag consists of the following values: @namespace.tag-name attribute-name="attribute value" Each tag belongs to a namespace and the tag name is unique within that namespace. Each tag can have one or more attributes and the corresponding value for that tag. Let us have a look at the XDoclet tags inside an Entity Bean. /** The code here uses the tag "bean" from the "ejb" namespace to define the properties that apply to EJB. For example: Similarly there are other tags such as finder, transaction and interface to indicate the XDoclet for generating the required information in the source files. XDoclet is generally used with Apache Ant tool, a popular build tool used for Java development. You can add your XDoclet tasks inside the build.xml file so that it generates the required files for you by executing that target from the Ant tool. By integrating XDoclet with Ant and other tools such as JUnit, you can achieve continuous integration throughout your project. For example, the target for generating EJB-specific source files could be added into an Ant build file in the following manner. This example generates an deployment descriptor for a Sun One-Application Server.
This task uses the xdoclet.modules.ejb.EjbDocletTask class from XDoclet to generate the EJB-specific files such as local interface, local home interface, EJB deployment descriptor and deployment descriptor for a Sun One-Application server. To compile, package and deploy the generated EJB files, you can add more tasks to the build.xml file as mentioned below.
Download the above code (pdf) Download the executable example that includes the Ant build file containing all the above-mentioned steps, from generating the interfaces and descriptors to the deployment on the Sun One-Application Server. Ant build files expect installation of XDoclet version 1.2.1 at the following path: c:\xdoclet-1.2.1. We have used Ant version 1.6 for this example. Change the build.properties file according to your environment settings. To see the steps for the Ant task process, type ant -projecthelp under the unzipped directory TryXDoclet on the console. To verify the deployment of EJB module, you can look into the Administration console of your application server; in this case it is Sun One-Application Server. To download XDoclet, visit the official site of XDoclet. By Rehman Adil
Last published February 17, 2007
|
|