4 Creating a Split Development Directory Environment

To create a WebLogic Server split development directory that you can use to develop a Java EE application or module, you have to organize the Java EE components and shared classes, generate a basic build.xml file, and develop multiple EAR projects.

This chapter includes the following sections:

Overview of the Split Development Directory Environment

The WebLogic split development directory environment consists of a directory layout and associated Ant tasks that help you repeatedly build, change, and deploy Java EE applications.

Compared to other development frameworks, the WebLogic split development directory provides these benefits:

  • Fast development and deployment. By minimizing unnecessary file copying, the split development directory Ant tasks help you recompile and redeploy applications quickly without first generating a deployable archive file or exploded archive directory.

  • Simplified build scripts. The Oracle-provided Ant tasks automatically determine which Java EE modules and classes you are creating, and build components in the correct order to support common classpath dependencies. In many cases, your project build script can simply identify the source and build directories and allow Ant tasks to perform their default behaviors.

  • Easy integration with source control systems. The split development directory provides a clean separation between source files and generated files. This helps you maintain only editable files in your source control system. You can also clean the build by deleting the entire build directory; build files are easily replaced by rebuilding the project.

Source and Build Directories

The source and build directories form the basis of the split development directory environment. The source directory contains all editable files for your project—Java source files, editable descriptor files, JSPs, static content, and so forth. You create the source directory for an application by following the directory structure guidelines described in Organizing Java EE Components in a Split Development Directory.

The top level of the source directory always represents an enterprise application (.ear file), even if you are developing only a single Java EE module. Subdirectories beneath the top level source directory contain:

  • Enterprise Application Modules (EJBs and Web applications)

    Note:

    The split development directory structure does not provide support for developing new Resource Adapter components.

  • Descriptor files for the enterprise application (application.xml and weblogic-application.xml)

  • Utility classes shared by modules of the application (for example, exceptions, constants)

  • Libraries (compiled.jar files, including third-party libraries) used by modules of the application

The build directory contents are generated automatically when you run the wlcompile ant task against a valid source directory. The wlcompile task recognizes EJB, Web application, and shared library and class directories in the source directory, and builds those components in an order that supports common class path requirements. Additional Ant tasks can be used to build Web services or generate deployment descriptor files from annotated EJB code.

Figure 4-1 Source and Build Directories

Description of Figure 4-1 follows
Description of "Figure 4-1 Source and Build Directories"

The build directory contains only those files generated during the build process. The combination of files in the source and build directories form a deployable Java EE application.

The build and source directory contents can be place in any directory of your choice. However, for ease of use, the directories are commonly placed in directories named source and build, within a single project directory (for example, \myproject\build and \myproject\source).

Deploying from a Split Development Directory

All WebLogic Server deployment tools (weblogic.Deployer, wldeploy, and the WebLogic Server Administration Console) support direct deployment from a split development directory. You specify only the build directory when deploying the application to WebLogic Server.

WebLogic Server attempts to use all classes and resources available in the source directory for deploying the application. If a required resource is not available in the source directory, WebLogic Server then looks in the application's build directory for that resource. For example, if a deployment descriptor is generated during the build process, rather than stored with source code as an editable file, WebLogic Server obtains the generated file from the build directory.

WebLogic Server discovers the location of the source directory by examining the .beabuild.txt file that resides in the top level of the application's build directory. If you ever move or modify the source directory location, edit the .beabuild.txt file to identify the new source directory name.

Deploying Applications Using wldeploy describes the wldeploy Ant task that you can use to automate deployment from the split directory environment.

Figure 4-2 shows a typical deployment process. The process is initiated by specifying the build directory with a WebLogic Server tool. In the figure, all compiled classes and generated deployment descriptors are discovered in the build directory, but other application resources (such as static files and editable deployment descriptors) are missing. WebLogic Server uses the hidden .beabuild.txt file to locate the application's source directory, where it finds the required resources.

Figure 4-2 Split Directory Deployment

Description of Figure 4-2 follows
Description of "Figure 4-2 Split Directory Deployment"

Split Development Directory Ant Tasks

Oracle provides a collection of Ant tasks designed to help you develop applications using the split development directory environment. Each Ant task uses the source, build, or both directories to perform common development tasks:

  • wlcompile—This Ant task compiles the contents of the source directory into subdirectories of the build directory. wlcompile compiles Java classes and also processes annotated .ejb files into deployment descriptors, as described in Compiling Applications Using wlcompile.

  • wlappc—This Ant task invokes the appc compiler, which generates JSPs and container-specific EJB classes for deployment. See Building Modules and Applications Using wlappc.

  • wldeploy—This Ant task deploys any format of Java EE applications (exploded or archived) to WebLogic Server. To deploy directly from the split development directory environment, you specify the build directory of your application. See wldeploy Ant Task Reference.

  • wlpackage—This Ant task uses the contents of both the source and build directories to generate an EAR file or exploded EAR directory that you can give to others for deployment.

Using the Split Development Directory Structure: Main Steps

In a split development directory structure, you can develop and deploy applications faster, simplify build scripts, and integrate with source control systems.

The following steps illustrate how you use the split development directory structure to build and deploy a WebLogic Server application.

  1. Create the main EAR source directory for your project. When using the split development directory environment, you must develop Web applications and EJBs as part of an enterprise application, even if you do not intend to develop multiple Java EE modules. See Organizing Java EE Components in a Split Development Directory.
  2. Add one or more subdirectories to the EAR directory for storing the source for Web applications, EJB components, or shared utility classes. See Organizing Java EE Components in a Split Development Directory and Organizing Shared Classes in a Split Development Directory.
  3. Store all of your editable files (source code, static content, editable deployment descriptors) for modules in subdirectories of the EAR directory. Add the entire contents of the source directory to your source control system, if applicable.
  4. Set your WebLogic Server environment by executing either the setWLSEnv.cmd (Windows) or setWLSEnv.sh (UNIX) script. The scripts are located in the WL_HOME\server\bin\ directory, where WL_HOME is the top-level directory in which WebLogic Server is installed.

    Note:

    On UNIX operating systems, the setWLSEnv.sh command does not set the environment variables in all command shells. Oracle recommends that you execute this command using the Korn shell or bash shell.

  5. Use the weblogic.BuildXMLGen utility to generate a default build.xml file for use with your project. Edit the default property values as needed for your environment. See Generating a Basic build.xml File Using weblogic.BuildXMLGen.
  6. Use the default targets in the build.xml file to build, deploy, and package your application. See Generating a Basic build.xml File Using weblogic.BuildXMLGen for a list of default targets.

Organizing Java EE Components in a Split Development Directory

The split development directory structure requires each project to be staged as a Java EE enterprise application. Oracle therefore recommends that you stage even standalone Web applications and EJBs as modules of an enterprise application, to benefit from the split directory Ant tasks. This practice also allows you to easily add or remove modules at a later date, because the application is already organized as an EAR.

Note:

If your project requires multiple EARs, see also Developing Multiple-EAR Projects Using the Split Development Directory.

The following sections describe the basic conventions for staging the following module types in the split development directory structure:

The directory examples are taken from the splitdir sample application installed in ORACLE_HOME\wlserver\samples\src\examples\splitdir, where ORACLE_HOME represents the directory in which the WebLogic Server code examples are configured. For more information about the WebLogic Server code examples, see Sample Applications and Code Examples in Understanding Oracle WebLogic Server.

Source Directory Overview

The following figure summarizes the source directory contents of an enterprise application having a Web application, EJB, shared utility classes, and third-party libraries. The sections that follow provide more details about how individual parts of the enterprise source directory are organized.

Figure 4-3 Overview of Enterprise Application Source Directory

Description of Figure 4-3 follows
Description of "Figure 4-3 Overview of Enterprise Application Source Directory"

Enterprise Application Configuration

The top level source directory for a split development directory project represents an enterprise application. The following figure shows the minimal files and directories required in this directory.

Figure 4-4 Enterprise Application Source Directory

Description of Figure 4-4 follows
Description of "Figure 4-4 Enterprise Application Source Directory"

The enterprise application directory will also have one or more subdirectories to hold a Web application, EJB, utility class, and/or third-party Jar file, as described in the following sections.

Web Applications

Web applications use the basic source directory layout shown in the figure below.

Figure 4-5 Web Application Source and Build Directories

Description of Figure 4-5 follows
Description of "Figure 4-5 Web Application Source and Build Directories"

The key directories and files for the Web application are:

  • helloWebApp\ —The top level of the Web application module can contain JSP files and static content such as HTML files and graphics used in the application. You can also store static files in any named subdirectory of the Web application (for example, helloWebApp\graphics or helloWebApp\static.)

  • helloWebApp\WEB-INF\ —Store the Web application's editable deployment descriptor files (web.xml and weblogic.xml) in the WEB-INF subdirectory.

  • helloWebApp\WEB-INF\src —Store Java source files for Servlets in package subdirectories under WEB-INF\src.

When you build a Web application, the appc Ant task and jspc compiler compile JSPs into package subdirectories under helloWebApp\WEB-INF\classes\jsp_servlet in the build directory. Editable deployment descriptors are not copied during the build process.

EJBs

EJBs use the source directory layout shown in the figure below.

Figure 4-6 EJB Source and Build Directories

Description of Figure 4-6 follows
Description of "Figure 4-6 EJB Source and Build Directories"

The key directories and files for an EJB are:

  • helloEJB\ —Store all EJB source files under package directories of the EJB module directory. The source files can be either .java source files, or annotated .ejb files.

  • helloEJB\META-INF\ —Store editable EJB deployment descriptors (ejb-jar.xml and weblogic-ejb-jar.xml) in the META-INF subdirectory of the EJB module directory. The helloWorldEar sample does not include a helloEJB\META-INF subdirectory, because its deployment descriptors files are generated from annotations in the .ejb source files. See Important Notes Regarding EJB Descriptors.

During the build process, EJB classes are compiled into package subdirectories of the helloEJB module in the build directory. If you use annotated .ejb source files, the build process also generates the EJB deployment descriptors and stores them in the helloEJB\META-INF subdirectory of the build directory.

Important Notes Regarding EJB Descriptors

EJB deployment descriptors should be included in the source META-INF directory and treated as source code only if those descriptor files are created from scratch or are edited manually. Descriptor files that are generated from annotated .ejb files should appear only in the build directory, and they can be deleted and regenerated by building the application.

For a given EJB component, the EJB source directory should contain either:

  • EJB source code in .java source files and editable deployment descriptors in META-INF

or:

  • EJB source code with descriptor annotations in .ejb source files, and no editable descriptors in META-INF.

In other words, do not provide both annotated .ejb source files and editable descriptor files for the same EJB component.

Organizing Shared Classes in a Split Development Directory

The WebLogic split development directory also helps you store shared utility classes and libraries that are required by modules in your enterprise application.

The following sections describe the directory layout and classloading behavior for shared utility classes and third-party JAR files.

Shared Utility Classes

Enterprise applications frequently use Java utility classes that are shared among application modules. Java utility classes differ from third-party JARs in that the source files are part of the application and must be compiled. Java utility classes are typically libraries used by application modules such as EJBs or Web applications.

Figure 4-7 Java Utility Class Directory

Description of Figure 4-7 follows
Description of "Figure 4-7 Java Utility Class Directory"

Place the source for Java utility classes in a named subdirectory of the top-level enterprise application directory. Beneath the named subdirectory, use standard package subdirectory conventions.

During the build process, the wlcompile Ant task invokes the javac compiler and compiles Java classes into the APP-INF/classes/ directory under the build directory. This ensures that the classes are available to other modules in the deployed application.

Third-Party Libraries

You can extend an enterprise application to use third-party .jar files by placing the files in the APP-INF\lib\ directory, as shown below:

Figure 4-8 Third-party Library Directory

Description of Figure 4-8 follows
Description of "Figure 4-8 Third-party Library Directory"

Third-party JARs are generally not compiled, but may be versioned using the source control system for your application code. For example, XML parsers, logging implementations, and Web application framework JAR files are commonly used in applications and maintained along with editable source code.

During the build process, third-party JAR files are not copied to the build directory, but remain in the source directory for deployment.

Class Loading for Shared Classes

The classes and libraries stored under APP-INF/classes and APP-INF/lib are available to all modules in the enterprise application. The application classloader always attempts to resolve class requests by first looking in APP-INF/classes, then APP-INF/lib.

Generating a Basic build.xml File Using weblogic.BuildXMLGen

After you set up your source directory structure, use the weblogic.BuildXMLGen utility to create a basic build.xml file. weblogic.BuildXMLGen is a convenient utility that generates an Ant build.xml file for enterprise applications that are organized in the split development directory structure. The utility analyzes the source directory and creates build and deploy targets for the enterprise application as well as individual modules. It also creates targets to clean the build and generate new deployment descriptors.

Additionally, optional packages are supported as Java EE shared libraries in weblogic.BuildXMLGen, whereby all manifests of an application and its modules are scanned to look for optional package references. If optional package references are found they are added to the compile and appc tasks in the generated build.xml file.

For example, if a library located at lib\echolib.jar is referenced as an optional package, the tasks generated by weblogic.BuildXMLGen will contains an appc task that would appear as follows:

<target name="appc" description="Runs weblogic.appc on your application">
  <wlappc source="${dest.dir}" verbose="${verbose}">
    <library file="lib\echolib\echolib.jar" />
  </wlappc>
</target>

The compile and appc tasks for modules also use the lib\echolib\echolib.jar library.

weblogic.BuildXMLGen Syntax

The syntax for weblogic.BuildXMLGen is as follows:

java weblogic.BuildXMLGen [options] <source directory>

where options include:

  • -help—Print standard usage message

  • -version—Print version information

  • -projectName <project name>—Name of the Ant project

  • -d <directory>—Directory where build.xml is created. The default is the current directory.

  • -file <build.xml>—Name of the generated build file

  • -librarydir <directories>—Create build targets for shared Java EE libraries in the comma-separated list of directories. See Creating Shared Java EE Libraries and Optional Packages..

  • -username <username>—User name for deploy commands

  • -password <password>—User password

After running weblogic.BuildXMLGen, edit the generated build.xml file to specify properties for your development environment. The list of properties you need to edit are shown in the listing below.

Example 4-1 build.xml Editable Properties

<!-- BUILD PROPERTIES ADJUST THESE FOR YOUR ENVIRONMENT -->
  <property name="tmp.dir" value="/tmp" />
  <property name="dist.dir" value="${tmp.dir}/dist"/>
  <property name="app.name" value="helloWorldEar" />
  <property name="ear" value="${dist.dir}/${app.name}.ear"/>
  <property name="ear.exploded" value="${dist.dir}/${app.name}_exploded"/>
  <property name="verbose" value="true" />
  <property name="user" value="USERNAME" />
  <property name="password" value="PASSWORD" />
  <property name="servername" value="myserver" />
  <property name="adminurl" value="iiop://localhost:7001" />

In particular, make sure you edit the tmp.dir property to point to the build directory you want to use. By default, the build.xml file builds projects into a subdirectory tmp.dir named after the application (/tmp/helloWorldEar in the above listing).

The following listing shows the default main targets created in the build.xml file. You can view these targets at the command prompt by entering the ant -projecthelp command in the EAR source directory.

Example 4-2 Default build.xml Targets

appc                 Runs weblogic.appc on your application
build                Compiles helloWorldEar application and runs appc
clean                Deletes the build and distribution directories
compile              Only compiles helloWorldEar application, no appc
compile.appStartup   Compiles just the appStartup module of the application
compile.appUtils     Compiles just the appUtils module of the application
compile.build.orig   Compiles just the build.orig module of the application
compile.helloEJB     Compiles just the helloEJB module of the application
compile.helloWebApp  Compiles just the helloWebApp module of the application
compile.javadoc      Compiles just the javadoc module of the application
deploy                 Deploys (and redeploys) the entire helloWorldEar application
descriptors          Generates application and module descriptors
ear                  Package a standard J2EE EAR for distribution
ear.exploded         Package a standard exploded J2EE EAR
redeploy.appStartup  Redeploys just the appStartup module of the application
redeploy.appUtils    Redeploys just the appUtils module of the application
redeploy.build.orig  Redeploys just the build.orig module of the application
redeploy.helloEJB    Redeploys just the helloEJB module of the application
redeploy.helloWebApp Redeploys just the helloWebApp module of application
redeploy.javadoc     Redeploys just the javadoc module of the application
undeploy               Undeploys the entire helloWorldEar application

Developing Multiple-EAR Projects Using the Split Development Directory

Projects that require building multiple enterprise applications simultaneously require slightly different conventions and procedures in organizing libraries and classes shared by multiple EARs and linking multiple build.xml files.

The split development directory examples and procedures described previously have dealt with projects consisting of a single enterprise application. Projects that require building multiple enterprise applications simultaneously require slightly different conventions and procedures, as described in the following sections.

Note:

The following sections refer to the MedRec sample application, which consists of three separate enterprise applications as well as shared utility classes, third-party JAR files, and dedicated client applications. The MedRec source and build directories are installed under ORACLE_HOME/user_projects/domain/medrec, where ORACLE_HOME is the directory you specified as Oracle Home when you installed Oracle WebLogic Server. For more information about the WebLogic Server samples, see Sample Applications and Code Examples in Understanding Oracle WebLogic Server.

Organizing Libraries and Classes Shared by Multiple EARs

For single EAR projects, the split development directory conventions suggest keeping third-party JAR files in the APP-INF/lib directory of the EAR source directory. However, a multiple-EAR project would require you to maintain a copy of the same third-party JAR files in the APP-INF/lib directory of each EAR source directory. This introduces multiple copies of the source JAR files, increases the possibility of some JAR files being at different versions, and requires additional space in your source control system.

To address these problems, consider editing your build script to copy third-party JAR files into the APP-INF/lib directory of the build directory for each EAR that requires the libraries. This allows you to maintain a single copy and version of the JAR files in your source control system, yet it enables each EAR in your project to use the JAR files.

The MedRec sample application installed with WebLogic Server uses this strategy, as shown in the following figure.

Figure 4-9 Shared JAR Files in MedRec

Description of Figure 4-9 follows
Description of "Figure 4-9 Shared JAR Files in MedRec"

MedRec takes a similar approach to utility classes that are shared by multiple EARs in the project. Instead of including the source for utility classes within the scope of each ear that needs them, MedRec keeps the utility class source independent of all EARs. After compiling the utility classes, the build script archives them and copies the JARs into the build directory under the APP-INF/LIB subdirectory of each EAR that uses the classes, as shown in figure Figure 4-9.

Linking Multiple build.xml Files

When developing multiple EARs using the split development directory, each EAR project generally uses its own build.xml file (perhaps generated by multiple runs of weblogic.BuildXMLGen.). Applications like MedRec also use a main build.xml file that calls the other build.xml files for each EAR in the application suite.

Ant provides a core task (named ant) that allows you to execute other project build files within a main build.xml file. The following line from the MedRec main build file shows its usage:

<ant inheritAll="false" dir="${root}/startupEar" antfile="build.xml"/>

The above task instructs Ant to execute the file named build.xml in the /startupEar subdirectory. The inheritAll parameter instructs Ant to pass only user properties from the main build file to the build.xml file in /startupEar.

MedRec uses multiple tasks similar to the above to build the startupEar, medrecEar, and physicianEar applications, as well as building common utility classes and client applications.

Best Practices for Developing WebLogic Server Applications

The WebLogic Server documentation library includes a number of recommended best practices for application development, including topics such as packaging, distribution, deployment, and more.

Oracle recommends the following "best practices" for application development.

  • Package applications as part of an enterprise application. See Packaging Applications Using wlpackage.

  • Use the split development directory structure. See Organizing Java EE Components in a Split Development Directory.

  • For distribution purposes, package and deploy in archived format. See Packaging Applications Using wlpackage.

  • In most other cases, it is more convenient to deploy in exploded format. See Archive versus Exploded Archive Directory.

  • Never deploy untested code on a WebLogic Server instance that is serving production applications. Instead, set up a development WebLogic Server instance on the same computer on which you edit and compile, or designate a WebLogic Server development location elsewhere on the network.

  • Even if you do not run a development WebLogic Server instance on your development computer, you must have access to a WebLogic Server distribution to compile your programs. To compile any code using WebLogic or Java EE APIs, the Java compiler needs access to the weblogic.jar file and other JAR files in the distribution directory. Install WebLogic Server on your development computer to make WebLogic distribution files available locally.