DBA > Articles

Introduction to Ant-Driven Development and Testing of Oracle WebLogic Server Apps in Eclipse

By: Jean Drole
To read more DBA articles, visit http://dba.fyicenter.com/article/

Modern integrated development environments (IDEs)—particularly Eclipse (with Oracle Enterprise Pack for Eclipse) and Oracle JDeveloper—provide many productivity features, such as automatic build and deployment, that simplify and speed up application development. Oracle Enterprise Pack for Eclipse WebLogic Server adapters provide remote/local deployment and debugging, start/stop, domain creation, support for JSR-88 deployment plans, and more. Similar features are provided by Oracle JDeveloper. These features provide excellent support for the development and testing of simple components and even multiple applications, but as projects grow in complexity, driving the build, creation of new domains, deployment/redeployment/undeployment, and testing of many components that may even involve more than one application server and many domains rapidly becomes a tedious task that demands the flexibility of a scripting language.

Fortunately, these IDEs provide features for sewing these operations together into coherent automated tasks. One of these features is Ant, a popular extensible scripting environment that runs both inside and outside Oracle Enterprise Pack for Eclipse. This article explores how to complement basic IDE features in Oracle Enterprise Pack for Eclipse with Ant to create scripts that build applications, control application server instances, create domains, deploy and run applications, and finally compare results with expected values. The emphasis is on the integration of operations in Oracle WebLogic Server applications with Ant and the use of BeanShell to extend Ant. The article concludes with an example that illustrates the use of version application lifecycle listeners.

The examples described in this article were developed and tested in an Oracle Enterprise Pack for Eclipse installation augmented with BeanShell alongside an Oracle WebLogic Server 11g installation. However, they should work, with only minimal modifications, in vanilla Eclipse 3 (Java Platform, Enterprise Edition [Java EE]) alongside any Oracle WebLogic Server version that supports the wlsserver and wlsdeploy Ant tasks. The last example also requires a version of the application server with support for ApplicationVersionLifecycleListener. It is assumed that the reader has minimal experience with Eclipse, Oracle WebLogic Server, and a basic understanding of what an Ant task is. The examples work in a UNIX environment and on Windows with a UNIX-like environment such as CygWin.

* Download and install Oracle WebLogic Server. (Do a default installation; no need to create a domain upon completion.) Choosing a package that contains Oracle Enterprise Pack for Eclipse allows skipping of the next download step. Note that the installation location will henceforth be referred to as $BEA_HOME. It contains the following directories:

jdk160_11 modules registry.dat utils
jrockit_160_05_R27.6.2-20 ocm.rsp registry.xml wlserver_10.3
logs oepe_11. user_projects

* Download and install Oracle Enterprise Pack for Eclipse. You should skip this step if you already have an Eclipse installation; it is not necessary to download the Oracle Enterprise Pack for Eclipse plug-ins in order to reproduce the examples of this article.

* Download BeanShell and related Java Archive (JAR) files:
o commons-logging-1.1.jar from http://commons.apache.org/downloads/download_logging.cgi
o bsf.jar from expanding the file at http://apache.opensourceresources.org/jakarta/bsf/binaries/bsf-bin-2.4.0.tar.gz
o bsh.jar from http://www.beanshell.org/bsh-2.0b4.jar

Initial Configuration
Ant was designed to be extensible. It has a rich API for creating new Ant tasks in Java that opens the door to limitless expansion. Although the Ant API is relatively easy to use, it is not really convenient enough to incorporate into a daily development routine. Ant also has its own scripting language that works fine for certain tasks such as compiling or jar-ring a project but falls short as soon as one deviates from the cookie-cutter. In addition, Ant can be extended with scripting languages. This is a happy compromise between the Java API approach and the limitations of built-in tasks. Of these scripting languages, BeanShell is of particular interest, because it allows scripting in a language that developers have already mastered: Java. (BeanShell supports valid Java code, and it can also handle a form of Java, preferred by script writers, that is less verbose than strict Java.)

It is important to set the environment properly prior to starting Eclipse:
1. Change the directory to ${BEA_HOME}/wlserver_10.3/common/bin.
2. Open a Bash shell, and source the commEnv.sh by executing the following command in the shell:

. ./commEnv.sh?
?(In case there is any confusion about ${BEA_HOME}, note that this shell command will correctly set the environment variable ${BEA_HOME}.)?
3. Check that the $WEBLOGIC_CLASSPATH environment variable has been defined:

4. ? Start Eclipse. If you installed the full Oracle Enterprise Pack for Eclipse WebLogic package, you will find an Eclipse executable in the ${BEA_HOME}/oepe_11. directory inside your Oracle WebLogic Server installation. Run the Eclipse executable:
Because several projects will be added and because global Eclipse preferences will be modified, it's a good idea to create a new workspace: choose Switch Workspace from the File menu, and select Other. Type the new workspace location. Expand the Copy Settings item, and choose which settings to copy, if any.
Once the BeanShell JAR files have been downloaded, it is a trivial task to configure Oracle Enterprise Pack for Eclipse to use it.
1. Choose Preferences from the Windows menu to show the Preferences panel.
2. In the left pane of the Preferences panel, navigate to Ant -> Runtime.
3. Choose the Classpath tab, and select Global Entries.
4. Click the Add External JARs button to add the JAR files needed by BeanShell: bsh, bsf, and commons-logging. Also, if it is not already there, add tools.jar from the lib directory of your jre directory. For example, for Oracle JRockit, use ${BEA_HOME}/jrockit_160_05_R27.6.2-20/lib.

The Basics
When creating Java EE-style projects (dynamic Web, enterprise application, utility modules), enable the WebLogic Extensions facet. In the Project Creation dialog box, click the Modify button in the Creation area and check the box to add Oracle WebLogic Web/EAR/EJB extensions. Oracle provides several editors for common Oracle WebLogic Server descriptors such as weblogic.xml and weblogic-application.xml that make editing these files much easier.

With BeanShell configured, it is time to start building the foundations for the upcoming examples. Start by creating a new project named UtilityScripts. It will hold common Ant utility scripts used by the upcoming examples.
1. Choose New from the File menu, and select the Project subitem.
2. Select the project type, General -> Project, and click the Next button.
3. Enter the project name, UtilityScripts, and click the Finish button.

Create a new Ant file that will contain the test targets for the utility scripts:
1. In the Package Explorer, select the UtilityScripts project.
2. Choose New from the File menu, and select the Other subitem.
3. Select the project type, XML -> xml, and click Next.
4. Enter the filename, test.xml, and click Finish.
5. Locate the test.xml file in the Package Explorer, and double-click it to open it. (When opened, the file may appear in the Design facet. If this is the case, switch to the Source facet.) Then add the following code to the test.xml script file:
<project name="test">
<target name="test-beanshell">
<script language="beanshell">
System.out.println("Hello world!");

<project> is the root element of all Ant files. Its name is displayed in the Ant view. The script task specifies the BeanShell language and thus will execute its content as a BeanShell script. BeanShell will interpret any valid Java source code, but—of particular interest to those who script routinely—it supports many shortcuts that can help make scripts less verbose than standard Java.

Add the Ant view to the Perspective for Java class to facilitate access to the Ant targets:
1. Choose Show View from the Windows menu, and select the Other subitem.Select Ant -> Ant, and click the OK button. The following Ant view will appear. (The Ant view will appear in whichever pane was active when it was added. You can move it to another pane by dragging the view's tab to another pane.)?

2. ? Click the +ant icon (). Then navigate to the UtilityScript project's test.xml file and click the OK button.?

Now, to check that BeanShell works in Oracle Enterprise Pack for Eclipse, expand the test item in the Ant view to expose the test-beanshell target. Double-click the test-beanshell target to run it. The Console view should include the following results:?

[script] Hello world!

Add a test to check that the $WEBLOGIC_CLASSPATH environment variable is available to Ant inside the IDE: ?

Full article...

Other Related Articles

... to read more DBA articles, visit http://dba.fyicenter.com/article/