ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Using Ant and WebLogic EJBs
Pages: 1, 2

Building all EJBs


This is what the project's <ejbjar> task would look like, assuming ${weblogic.classpath} includes the service packs, jars and class directories needed by WebLogic:



<ejbjar srcdir="build"
                descriptordir="descriptor">
                <weblogic destdir="build/deploy"
                  classpath="${weblogic.classpath}:build"
                                  compiler="javac"/>
                <include name="**/*-ejb-jar.xml"/>
                <exclude name="**/*weblogic*.xml"/>
</ejbjar>

Eight formatted lines efficiently compile any number of EJBs. (In a "real-world" antfile, most of the attribute values would be properties, making the number of total lines needed a bit larger. To avoid wasting space, this article has explicitly defined the attributes.) The operation consists of two main tasks, <ejbjar> and <weblogic>. The documentation lists many more attributes and nested elements, but those above are the only ones needed for this example. <ejbjar> builds the "generic" EJB, ready for container-specific compiling and deployment. <weblogic> runs weblogic.ejbc on the "generic" EJB, preparing it for deployment to a WebLogic container. There are no class-specific or module-specific attributes. For a great performance boost, the compiler attribute can be replaced with Jikes like so:

compiler="jikes -nowarn"
The 'no-warn' argument is for weblogic.ejbc, since it interprets some warnings from Jikes as errors and would "fail" to build an otherwise good EJB.

With this, building 100 EJBs will not make it seem like the computer is rendering dust for a Pixar movie. Experience has shown that the building of the EJBs takes the most time in builds; performance in this part of a build means a great deal. As an example, a project using roughly 700 classes and 70 EJBs can be compiled in under 10 minutes (on a Pentium III 800 with 512MB of RAM). The same project compiled with the javac compiler takes 30 to 40 minutes to complete. In both instances, the classes alone compile in under 30 seconds (the jikes version taking slightly less time).

Just One Bean

By using some command-line/property-file specified attributes, a user can specify that a specific EJB be built.

<ejbjar srcdir="build" descriptordir="descriptor/${bean.name}"> <weblogic destdir="build/${bean.name}/${bean.name}.jar" classpath="${weblogic.classpath}:build" keepgenerated="true" compiler="javac"/> <include name="**/*-ejb-jar.xml"/> <exclude name="**/*weblogic*xml"/> </ejbjar>

The previous code fragment tells <ejbjar> and <weblogic> to create, compile and place an EJB in the ${bean.name} deployment directory -- the same location it would be found in a global compile. This snippet is handy for the developers who do not want to rebuild the entire system every time. It should be noted that static dependencies and the like are not taken into consideration during dependency checks. A complete rebuild is not without merit.

Quirks, Tips and Optional Features

Descriptor discrepancies

Some containers, WebLogic's included, expect the descriptors to follow a different naming convention than the one Ant's tasks require. The <ejbjar> task takes care of this internally, renaming the files prior to packaging them within the jar. However, this means that the descriptors on the file system are in a naming scheme that may break other deployment programs and programming IDEs.

There are a couple of ways to handle this. In systems with symbolic-link capable file systems, it is easy to maintain two sets of filenames: one for the IDE, one for Ant. Links would need to be recreated during version-control updates. Scripts and Ant both have features capable of solving this issue. In a non-linking file system (such as Windows), it will be necessary to maintain either two sets of files, or have Ant or a script rename everything prior to building the application.

CMP descriptors are particularly vulnerable to this naming scheme. Following the scheme, a developer loses the ability to build EJBs by hand, since the CMP descriptor name is also specified in the WebLogic descriptor. As of Ant 1.3, it is possible for the user to allow Ant to discover the container-managed descriptors from the WebLogic descriptor. This is a benefit for those with multiple CMP descriptors, as well. Those using Ant 1.2 can patch the EJB modules with the latest code to get this functionality. To turn this on, add the newCMP attribute to the <weblogic> element. For example:

<ejbjar srcdir="build"
            descriptordir="descriptor">
                           <weblogic destdir="build/deploy"
             classpath="${weblogic.classpath}:build"
                             compiler="javac"
                             newCMP="true"/>
           <include name="**/*-ejb-jar.xml"/>
           <exclude name="**/*weblogic*Xml"/>
</ejbjar>

Generic EJBs for Dynamic Deployment Containers

jBoss and WebLogic 6.0, for example, perform dynamic compiling and deployment. For these servers, "generic" EJBs can be created by omitting the nested WebLogic task. Ant will create a jar with the proper classes and the EJB descriptor, leaving it up to the user or application server to compile and create a fully deployable bean from this jar file.

Here's a simple example:

<ejbjar srcdir="build"
        descriptordir="descriptor">
     <include name="**/*-ejb-jar.xml"/>
     <exclude name="**/*weblogic*Xml"/>
</ejbjar>

DTD Location

Typically, DTD URLs are found in the headers of the deployment descriptors. WebLogic's compiler knows these DTDs can exist in the classpath. If the WebLogic classes are in the classpath for the Ant JVM (not just the <weblogic> task's classpath), these DTDs are found. If not, they are located via standard net communications. If the network is latent or nonexistent, the requests can fail, causing a compile and build error. Beginning with Ant 1.3's EJB tasks, DTDs can be specified as a nested element. For example (assuming ${weblogic.home} is the root location for a WebLogic installation):

<ejbjar srcdir="build"
 descriptordir="descriptor">
 <weblogic destdir="build/deploy"
      classpath="${weblogic.classpath}:build"
          compiler="javac"/>
 <include name="**/*-ejb-jar.xml"/>
<exclude name="**/*weblogic*xml"/>
<dtd publicId="-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 1.1//EN"
   location="${weblogic.home}/classes/weblogic/ejb/deployment/xml/ejb-jar.dtd"/>
<dtd publicId="-//BEA Systems, Inc.//DTD WebLogic 5.1.0 EJB//EN"
   location="${weblogic.home}/classes/weblogic/ejb/deployment/xml/weblogic-ejb-jar.dtd"/>
</ejbjar>

Jesse E. Tilly is a developer with HotelTools, Inc. in Atlanta, GA. He has been working exclusively with BEA WebLogic developing and managing hospitality applications over the Internet.


Return to ONJava.com.