Book Image

Java EE Development with Eclipse

By : Deepak Vohra
Book Image

Java EE Development with Eclipse

By: Deepak Vohra

Overview of this book

<p>Java EE is the industry standard on enterprise computing and Oracle WebLogic Server is the most comprehensive platform for enterprise applications. The book combines Java EE with WebLogic Server in the most commonly used Java IDE, the Eclipse IDE 3.7.<br /><br />"Java EE Development with Eclipse" is the only book on Eclipse IDE for Java EE Developers. The book is a practical guide for using the most commonly used Java EE technologies and frameworks in Eclipse IDE. Sample applications are available in downloadable format and may be customized to meet requirements. Oracle Enterprise Pack for Eclipse 12c, an enhancement to Eclipse IDE, providing additional project facets and an integrated support for Oracle WebLogic Server is used.<br /><br />"Java EE Development with Eclipse" is based on developing applications with some of the commonly used technologies using the project facets in Eclipse 3.7 and its enhancement Oracle Enterprise Pack for Eclipse 12c. <br /><br />The book starts with a discussion on EJB 3.0 database persistence with Oracle database XE and Oracle WebLogic Server. JAXB is discussed in the context of bi-directional mapping between XML and Java. A generic web project is developed for PDF and Excel spread sheet reports. JavaServer Faces, the most commonly used view component in web applications is discussed for developing a data table. Facelets, which was integrated into JSF with 2.0 version is discussed in the context of templating. ADF Faces components are used to develop another user interface (UI) application. Web services are discussed with JAX-WS and JAX-RS technologies. Java EE frameworks Ajax and Spring are also discussed.</p>
Table of Contents (17 chapters)
Java EE Development with Eclipse
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Packaging and deploying the entity bean application


In this section, we shall compile, package, and deploy the EJB application to the WebLogic server using an Ant build file. First, add the web.xml deployment descriptor. Create a WEB-INF folder in the webModule folder and add web.xml to the WEB-INF folder. The web.xml is an optional deployment descriptor for a web application deployed to the WebLogic descriptor and is listed as follows:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee/web- app_2_5.xsd" version="2.5">
</web-app>

If we were using servlets or other web application artifacts, we would have configured those in web.xml. We need a build.xml file for the tasks involved in assembling and deploying the entity bean application. Create a build.xml file in the project EJB3JPA by going to File | New | Other and in the New wizard, in XML, select XML file. Specify the following properties in the build.xml file:

Property

Value

Description

src.dir

${basedir}/ejbModule

Source directory

web.module

${basedir}/webModule

Web module directory

weblogic.home

C:/Oracle/Middleware

WebLogic home directory

weblogic.server

${weblogic.home}/wlserver_12.1/server

WebLogic Server directory

build.dir

${basedir}/build

Build directory

deploy.dir

${weblogic.home}/user_projects/domains/base_domain/autodeploy

Deploy directory

Specify a path element to include the WebLogic JAR files. Add the following tasks in the build file:

Task

Description

prepare

Makes the required directories

compile

Compiles the Java classes

jar

Creates a EJB JAR

war

Creates a web application WAR file

assemble-app

Assembles the JAR and the WAR files into an EAR file

deploy

Deploy the EAR file to WebLogic server

clean

Deletes directories when recompiling

The build script

The build.xml file is listed as follows:

<?xml version="1.0" encoding="UTF-8"?>
  <!--
    WebLogic build file
  -->

First, declare the different properties used in the build script:

<project name="EJB3" default="deploy" basedir=".">
  <property environment="env" />
  <property name="src.dir" value="${basedir}/ejbModule" />
  <property name="web.module" value="${basedir}/webModule" />
    <property name="weblogic.home" value="C:/Oracle/Middleware/" />
  <property name="weblogic.server" value="${weblogic.home}/wlserver_12.1/server" />  <property name="build.dir" value="${basedir}/build" />

  <property name="deploy.dir"
    value="${weblogic.home}/user_projects/domains/base_domain/autodeploy" />

Specify the class path for the classes to be compiled:

  <path id="classpath">
    <fileset dir="${weblogic.home}/modules">
      <include name="*.jar" />
    </fileset>
    <fileset dir="${weblogic.server}/lib">
      <include name="*.jar" />
    </fileset>

    <pathelement location="${build.dir}" />
  </path>
  <property name="build.classpath" refid="classpath" />
  <target name="prepare">
    <mkdir dir="${build.dir}" />
  </target>    

Compile the Java classes, which include the entity beans and the session bean facade:

  <target name="compile" depends="prepare">
    <javac srcdir="${src.dir}" destdir="${build.dir}" debug="on"
      includes="**/*.java">
      <classpath refid="classpath" />
    </javac>
  </target>

Generate the EJB JAR file, which includes the compiled EJB classes and the deployment descriptors:

  <target name="jar" depends="compile">
    <jar destfile="${build.dir}/ejb3.jar">
      <fileset dir="${build.dir}">
        <include name="**/*.class" />
      </fileset>
      <fileset dir="${src.dir}">
        <include name="META-INF/*.xml" />
      </fileset>
    </jar>
  </target>

Generate the web application WAR file, which includes the JSP client:

  <target name="war" depends="jar">
    <war warfile="${build.dir}/weblogic.war">
      <fileset dir="webModule">
        <include name="*.jsp" />
      </fileset>
      <fileset dir="webModule">
        <include name="WEB-INF/web.xml" />
      </fileset>
    </war>
  </target>

Assemble the application into an EAR file, which includes the EJB JAR and the WAR files and the application.xml deployment descriptor:

  <target name="assemble-app" depends="war">
    <jar jarfile="${build.dir}/ejb3.ear">
      <metainf dir="META-INF">
        <include name="application.xml" />
      </metainf>
      <fileset dir="${build.dir}" includes="*.jar,*.war" />
    </jar>
  </target>

Deploy the application to the WebLogic Server deploy directory, the autodeployed directory:

  <target name="deploy" depends="assemble-app">
    <copy file="${build.dir}/ejb3.ear" todir="${deploy.dir}" />
  </target>
  <target name="clean">
    <delete file="${build.dir}/ejb3.ear" />
    <delete file="${build.dir}/ejb3.jar" />
    <delete file="${build.dir}/weblogic.war" />
  </target>
</project>

Running the build script

Having created a build script, now we shall run it. Right-click on build.xml and select Run As | Ant Build as shown in the following screenshot:

Select the deploy task and click on Run as shown in the following screenshot. As dependence is declared between tasks all the preceding tasks will also get run.

The EJB application gets compiled, built, assembled, and deployed to WebLogic Server's autodeployed directory as an EAR file. The BUILD SUCCESSFUL message indicates that the build and deployment was without error. If the build needs to be re-run first run the clean task, which deletes any previously generated EAR, WAR, and JAR files.

Start the WebLogic Server if not already started. In the WebLogic Server's Admin Server Console, the EAR file is shown deployed in Deployments. On expanding the EAR file the EJB JAR and web module WAR get listed as shown in the following screenshot: