Building the MedRec Applications

Tutorial 7: Compiling Applications Using the Split Development Directory

This tutorial explains how to compile Enterprise Application source files using the wlcompile ant task. wlcompile works with a WebLogic split development directory structure to produce a build or output directory, which contains the compiled Java classes. The build directory and the source directory described in Tutorial 6: Understanding the WebLogic Server Split Directory Structure constitute a deployable application in WebLogic Server.

Later tutorials explain how to use other WebLogic Server ant tasks that work with the split development directory to perform other application building tasks such as:

- Packaging files from the source and build directories into an EAR file or expanded EAR directory
- Deploying applications

This tutorial includes the following sections:

- Prerequisites
- Procedure
- Best Practices
- The Big Picture
- Related Reading

Prerequisites

Before starting this tutorial:

- Create the project directory and copy over the MedRec source files and output directories using the instructions in Tutorial 5: Creating the MedRec Project Directory.
- Read the instructions in Tutorial 6: Understanding the WebLogic Server Split Directory Structure to understand the organization of source files in the WebLogic Server split development directory.

Procedure

Follow these steps to use the wlcompile task with a split development directory in the MedRec application suite:

- Step 1: Create the build.xml file.
- Step 2: Compile the application.
- Step 3: Examine the output files.

Step 1: Create the build.xml file.

Storing your source files using the WebLogic split development directory structure simplifies the build.xml file required to compile your applications. For most Enterprise Applications, a simple script of several lines is adequate to compile all modules—the wlcompile task automatically determines the modules used in the application and maintains classpath dependencies accordingly.

1. To see how wlcompile works, create a simple XML file to compile the Physician application. First move to the physicianEar subdirectory in the MedRec project directory:

   >     cd c:\medrec_tutorial\src\physicianEar

   The top-level of physicianEar contains subdirectories for the Web Application and EJB components that form the Enterprise Application. You will store the XML file here as well.

2. Use a text editor to create a new mybuild.xml file in the physicianEar directory:

   >     notepad mybuild.xml

   Note: If you do not want to enter the build.xml file manually, copy the file wlcompile_tutorial.xml file to the new file name, mybuild.xml. Then follow along to understand the file contents.

3. Start the mybuild.xml file by defining a project named physiciantutorial:

   >     <project name="tutorial" default="build">

4. Define the main target for building the application. This target (named "build") is fairly simple. It uses the wlcompile task to identify the source directory (which uses the split development directory structure) and an output directory for storing compiled files. Enter the following lines:

   >       <target name="build">

   >         <wlcompile srcdir="c:/medrec_tutorial/src/physicianEar"
   >     destdir="c:/medrec_tutorial/build/physicianEar"/>

   >       </target>

   For most simple Enterprise Applications, you need only to point wlcompile to the source and build directories to use for compiling. Always make sure the srcdir and destdir directories point to separate locations—you want to ensure that your source and output files remain separate during the development process.

5. To complete the mybuild.xml file, add the following line to close the project:

   >     </project>

   Your completed file should resemble the following. Remember that you can copy over wlcompile_tutorial.xml if you do not want to type in the full text:

   >     <project name="tutorial" default="build">

   >       <target name="build">

   >         <wlcompile srcdir="c:/medrec_tutorial/src/physicianEar"
   >     destdir="c:/medrec_tutorial/build/physicianEar"/>

   >       </target>

   >     </project>

Step 2: Compile the application.

After you create the mybuild.xml file, you can use it to compile the application.

1. Make sure you have set your environment using the MedRecDomain environment script:

   >     c:\bea\user_projects\domains\MedRecDomain\setEnv.cmd

2. Move to the physicianEar directory and compile by running the mybuild.xml script with ant:

   >     cd c:\medrec_tutorial\src\physicianEar

   >     ant -f mybuild.xml

   Although you did not add any informational messages to your build script, the wlcompile task produces its own output to show its progress:

   >     Buildfile: mybuild.xml

   >     mybuild:

   >       [javadoc] Generating Javadoc

   >       [javadoc] Javadoc execution

   >       [javadoc] Loading source file
   >     C:\medrec_tutorial\build\physicianEar\physSessionEjbs\com\bea\m
   >     edrec\controller\PhysicianSessionEJB.java...

   >       [javadoc] Constructing Javadoc information...

   >       [javadoc] EJBGen 2.13beta

   >       [javadoc]     Creating
   >     C:\medrec_tutorial\build\physicianEar\physSessionEjbs\com\bea\m
   >     edrec\controller\PhysicianSessionHome.java

   >       [javadoc]     Creating
   >     C:\medrec_tutorial\build\physicianEar\physSessionEjbs\com\bea\m
   >     edrec\controller\PhysicianSession.java

   >       [javadoc]     Creating
   >     C:\medrec_tutorial\build\physicianEar\physSessionEjbs\ejb-jar.x
   >     ml

   >       [javadoc]     Creating
   >     C:\medrec_tutorial\build\physicianEar\physSessionEjbs\weblogic-
   >     ejb-jar.xml

   >       [javadoc]     Creating
   >     C:\medrec_tutorial\build\physicianEar\physSessionEjbs\ejbgen-bu
   >     ild.xml

   >          [move] Moving 2 files to
   >     C:\medrec_tutorial\build\physicianEar\physSessionEjbs\META-INF

   >         [javac] Compiling 3 source files to
   >     C:\medrec_tutorial\build\physicianEar\physSessionEjbs

   >         [javac] Compiling 13 source files to
   >     C:\medrec_tutorial\build\physicianEar\physicianWebApp\WEB-INF\c
   >     lasses

   >     BUILD SUCCESSFUL

   >     Total time: 4 seconds

3. If you did not receive the above output, you probably made a typo while creating the mybuild.xml file. If so, run the alternate compile command using the installed tutorial build file:

   >     ant -f wlcompile_tutorial.xml

Step 3: Examine the output files.

Now that you have compiled physicianEar, take a look at the build directory to see what happened. All output for the build target is placed in the output directory for the Enterprise Application, c:\medrec_tutorial\build\physicianEar.

The wlcompile output shows that the build started by running ejbgen on the Physician application's EJBs. Verify that the deployment descriptors were created:

>     dir c:\medrec_tutorial\build\physicianEar\physSessionEjbs\META-INF

>      Directory of
>     c:\medrec_tutorial\build\physicianEar\physSessionEjbs\META-INF

>     02/21/2003  05:32p      <DIR>          .

>     02/21/2003  05:32p      <DIR>          ..

>     02/21/2003  05:32p                 697 ejb-jar.xml

>     02/21/2003  05:32p                 884 weblogic-ejb-jar.xml

wlcompile also compiled the and copied the actual EJB classes to the physSessionEjbs directory:

>     dir
>     c:\medrec_tutorial\build\physicianEar\physSessionEjbs\com\bea\med
>     rec\controller

>      Directory of
>     c:\medrec_tutorial\build\physicianEar\physSessionEjbs\com\bea\med
>     rec\controller

>     02/21/2003  05:32p      <DIR>          .

>     02/21/2003  05:32p      <DIR>          ..

>     02/21/2003  05:32p              11,359 PhysicianClientUtils.class

>     02/21/2003  05:32p                 681 PhysicianSession.class

>     02/21/2003  05:32p               2,212 PhysicianSession.java

>     02/21/2003  05:32p               5,770 PhysicianSessionEJB.class

>     02/21/2003  05:32p               8,710 PhysicianSessionEJB.java

>     02/21/2003  05:32p                 324 PhysicianSessionHome.class

>     02/21/2003  05:32p                 428 PhysicianSessionHome.java

>                    7 File(s)         29,484 bytes

wlcompile compiled the Web Application servlet classes and placed them in the WEB-INF\classes directory:

>     dir
>     c:\medrec_tutorial\build\physicianEar\physicianWebApp\WEB-INF\cla
>     sses\com\bea\medrec

>      Directory of
>     c:\medrec_tutorial\build\physicianEar\physicianWebApp\WEB-INF\cla
>     sses\com\bea\medrec

>     02/24/2003  10:53a      <DIR>          .

>     02/24/2003  10:53a      <DIR>          ..

>     02/24/2003  10:53a      <DIR>          actions

>     02/24/2003  10:53a      <DIR>          utils

The actions directory stores struts action classes and the utils directory contains a utility class that stores MedRec constants.

Notice that the entire build directory for the Enterprise Application (c:\medrec_tutorial\build\physicianEar) contains deployment descriptor files only for the EJB components. This is because the EJB descriptors are generated using ejbgen tags. You can recreate the entire contents of the build directory, including the EJB deployment descriptors, by rerunning the build script.

The Enterprise Application and Web Application deployment descriptors are left in the source directory because they are created and edited manually, and cannot be easily replaced or rebuilt.

Best Practices

More complex Enterprise Applications may have compilation dependencies that are not automatically handled by the wlcompile task. However, you can use the include and exclude options to wlcompile to enforce your own dependencies. include and exclude accept the names of Enterprise Application modules—the names of subdirectories in the Enterprise Application source directory—to include or exclude them from the compile stage. See The Big Picture for an example.

The Big Picture

Although the MedRec Enterprise Applications use the WebLogic split development directory structure and wlcompile task in their build scripts, they have certain dependencies that are not handled by the default wlcompile task. For example, examine this excerpt from the medrecEar\build.xml file:

>         <wlcompile srcdir="${medrec.ear.src.dir}" destdir="${dest.dir}"

>                    excludes="adminWebApp, xml, mdbEjbs, webServicesEjb"/>

You can see that the build script starts by compiling all modules in the Enterprise Application except for adminWebApp, xml, mdbEjbs, and webServicesEjb. These correspond to subdirectories names in the medrecEar source directory.

The build then continues by compiling only the xml and webServicesEjb modules in the application:

>         <wlcompile srcdir="${medrec.ear.src.dir}" destdir="${dest.dir}"

>                    includes="xml, webServicesEjb"

Related Reading

- Developing WebLogic Server Applications
- Developing Web Applications for WebLogic Server
- Programming WebLogic Server Enterprise JavaBeans
- Programming WebLogic Web Services