10 Building Java EE Projects for WebLogic Server with Maven

This chapter provides details on how to use the WebLogic Maven archetypes to create, build, and deploy WebLogic Server Java EE applications.

This chapter contains the following sections:

10.1 Introduction to Building Java EE Project with Maven

A Maven plug-in and four archetypes are provided for Oracle WebLogic Server. Table 10-1 describes the Maven coordinates.

Table 10-1 Maven Coordinates with WebLogic Server

Artifact groupId artifactId version

WebLogic Server plug-in

com.oracle.weblogic

weblogic-maven-plugin

12.1.3-0-0

Basic WebApp archetype

com.oracle.weblogic.archetype

basic-webapp

12.1.3-0-0

WebApp with EJB archetype

com.oracle.weblogic.archetype

basic-webapp-ejb

12.1.3-0-0

Basic MDB archetype

com.oracle.weblogic.archetype

basic-mdb

12.1.3-0-0

Basic WebServices archetype

com.oracle.weblogic.archetype

basic-webservice

12.1.3-0-0


As with Maven archetypes in general, the Oracle WebLogic Maven archetype provides a set of starting points and examples for building your own applications.

10.2 Using the Basic WebApp Maven Archetype

To create a new Basic WebApp project using the Maven archetype, you must issue a command similar to the following:

mvn archetype:generate
    -DarchetypeGroupId=com.oracle.weblogic.archetype
    -DarchetypeArtifactId=basic-webapp
    -DarchetypeVersion=12.1.3-0-0
    -DgroupId=org.mycompany
    -DartifactId=my-basic-webapp-project
    -Dversion=1.0-SNAPSHOT

This runs Maven's archetype:generate goal which enables you to create a new project from an archetype. Table 10-2 describes the parameters.

Table 10-2 Parameters for the Basic WebApp Project

Parameter Purpose

archetypeGroupId

Identifies the group ID of the archetype that you want to use to create the new project. This must be com.oracle.weblogic as shown in the preceding example.

archetypeArtifactId

Identifies the archetype artifact ID of the archetype that you want to use to create the new project. This must be basic-webapp as shown in the preceding example.

archetypeVersion

Identifies the version of the archetype that you want to use to create the new project. This must be 12.1.3-0-0 as shown in the preceding example.

groupId

Identifies the group ID for your new project. This usually starts with your organization's domain name in reverse format.

artifactId

Identifies the artifact ID for your new project. This is usually an identifier for this project.

version

Identifies the version number for your new project. This is usually 1.0-SNAPSHOT for a new project.


You can also run the command without any arguments, as shown in the following example. In this case, Maven displays a list of available archetypes and prompts you to enter the required information.

mvn archetype:generate

If you want to limit Maven to look only into a particular repository, you can specify the -DarchetypeCatalog option. Specify the value as local to look only in your local repository, or specify the serverId for the repository you want Maven to look in. This limits the number of archetypes that you are shown and makes the command execute much faster.

After creating your project, it contains the following files:

Description of maven_dt_003.png follows
Description of the illustration maven_dt_003.png

These files make up a small sample application, which you can deploy as is. You can use this application as a starting point for building your own application.

There are a number of files included in the project, as described in Table 10-3.

Table 10-3 Files Created for the Basic WebApp project

File Purpose

pom.xml

The Maven Project Object Model (POM) file that describes your new project. It includes the Maven coordinates that you specified for your project, and it also includes the appropriate plug-in definitions to use the WebLogic Maven plug-in to build your project.

Files under src/main/java

An example Enterprise Java Bean that is used by the Web application to store data.

All other files

HTML and other files that make up the web application user interface.


After you have written your project code, you can use Maven to build the project. It is also possible to build the sample as is.

This section contains the following topics:

10.2.1 Customizing the Project Object Model File to Suit Your Environment

The Project Object Model (POM) file that is created by the archetype is sufficient in most cases. You should review the POM and update any of the settings where the provided default values differ from what you use in your environment.

If you are using an internal Maven Repository Manager, like Archiva, you should add a pluginRepository to the POM file. The following is an example; you can modify it to suit your environment:

 <pluginRepositories>
    <pluginRepository>
      <id>archiva-internal</id>
      <name>Archiva Managed Internal Repository</name>
      <url>http://localhost:8081/archiva/repository/internal/</url>
      <releases>
        <enabled>true</enabled>
      </releases>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </pluginRepository>
  </pluginRepositories>

10.2.2 Compiling Your Project

To compile the source code in your project, such as Java Beans, Servlets, and JSPs, use the following command:

mvn compile

This uses the standard Maven plug-ins to compile your source artifacts into class files. You can find the class files in the target directory of your project.

10.2.3 Packaging Your Project

To build the deployment archive, for example WAR or EAR file, use the following command:

mvn package

Again, this uses the standard Maven plug-ins to package your compiled artifacts and metadata into a deployment archive. When you run a Maven goal like package, Maven runs not just that goal, but all of the goals up to and including the goal you name. This is very similar to a standard Java EE application, except that if you have some WebLogic deployment descriptors in your project, they are also packaged into the deployment archive.

The deployment archive, in this case a WAR file, is available in the target directory of your project.

10.2.4 Deploying Your Project to the WebLogic Server Using Maven

To deploy the deployment archive using Maven, use the following command:

mvn pre-integration-test

This executes the deploy goal in the WebLogic Maven plug-in. This goal supports all standard types of deployment archives.

10.2.5 Deploying Your Project to the WebLogic Server Using Different Options

After you have packaged your project, you can also deploy it to the WebLogic Server using any of the other existing (non-Maven) mechanisms. For example, the WebLogic Administration Console, or an ANT or WLST script.

10.2.6 Testing Your Basic WebApp Project

You can test the Basic WebApp by visiting the following URL on the WebLogic Server where you deployed it:

http://servername:7001/basicWebapp/index.xhtml

The following shows the user interface for the Basic WebApp:

Description of wls_02.jpg follows
Description of the illustration wls_02.jpg

Provide the Account Name and Amount, then select Deposit to see how the application works.

10.3 Using the Basic WebApp with EJB Maven Archetype

To use the Basic WebApp with EJB project using the Maven archetype:

  1. Create a new Basic WebApp project using the Maven archetype, executing a command similar to the following:

    mvn archetype:generate
        -DarchetypeGroupId=com.oracle.weblogic.archetype
        -DarchetypeArtifactId=basic-webapp-ejb
        -DarchetypeVersion=12.1.3-0-0
        -DgroupId=org.mycompany
        -DartifactId=my-basic-webapp-ejb-project
        -Dversion=1.0-SNAPSHOT
    

    This runs Maven's archetype:generate goal which enables you to create a new project from an archetype. See Table 10-4 for a description of the parameters.

    Table 10-4 Parameters for the Basic WebApp with EJB Project

    Parameter Purpose

    archetypeGroupId

    Identifies the group ID of the archetype that you want to use to create the new project. This must be com.oracle.weblogic as shown in the preceding example.

    archetypeArtifactId

    Identifies the artifact ID of the archetype that you want to use to create the new project. This must be basic-webapp-ejb as shown in the preceding example.

    archetypeVersion

    Identifies the version of the archetype that you want to use to create the new project. This must be 12.1.3-0-0 as shown in the preceding example.

    groupId

    Identifies the group ID for your new project. This usually starts with your organization's domain name in reverse format.

    artifactId

    Identifies the artifact ID for your new project. This is usually an identifier for this project.

    version

    Identifies the version number for your new project. This is usually 1.0-SNAPSHOT for a new project.


    You can also run the command without any arguments, as shown in the following example. In this case, Maven displays a list of available archetypes and prompts you to enter the required information.

    mvn archetype:generate
    

    After creating your project, it contains the following files:

    Description of maven_dt_002.png follows
    Description of the illustration maven_dt_002.png

    These files make up a small sample application, which you can deploy as is. You can use this application as a starting point for building your own application.

    There are a number of files included in the project, as described in Table 10-5.

    Table 10-5 Files Created for the Basic WebApp with EJB Project

    File Purpose

    pom.xml

    The Maven Project Object Model (POM) file that describes your new project. It includes the Maven coordinates that you specified for your project, and it also includes the appropriate plug-in definitions to use the WebLogic Maven plug-in to build your project.

    Files under src/main/java

    An example Enterprise Java Bean that is used by the web application to store data.

    All other files

    HTML and other files that make up the web application user interface.


  2. After you have written your project code, you can use Maven to build the project. It is also possible to build the sample as is.

  3. Customize the POM to suit your environment. See Section 10.2.1.

  4. Compile your Basic WebApp with EJB Project. See Section 10.2.2.

  5. Package your Basic WebApp with EJB Project. See Section 10.2.3.

  6. Deploy your Basic WebApp with EJB Project. For information about deploying it using Maven, see Section 10.2.4. For information about deploying it using other options, see Section 10.2.5.

  7. Test your Basic WebApp with EJB Project.

    You can test the Basic WebApp with EJB by visiting the following URL on the WebLogic Server where you deployed it:

    http://servername:7001/basicWebapp/index.xhtml
    

    The following shows the user interface for the Basic WebApp with EJB:

    Description of wls_04.jpg follows
    Description of the illustration wls_04.jpg

    Provide the Account Name and Amount, then select Deposit to see how the application works.

10.4 Using the Basic WebService Maven Archetype

To use the Basic WebService project using the Maven Archetype:

  1. Create a new Basic WebService project using the Maven archetype, issue a command similar to the following:

    mvn archetype:generate
        -DarchetypeGroupId=com.oracle.weblogic.archetype
        -DarchetypeArtifactId=basic-webservice
        -DarchetypeVersion=12.1.3-0-0
        -DgroupId=org.mycompany
        -DartifactId=my-basic-webservice-project
        -Dversion=1.0-SNAPSHOT
    

    This runs Maven's archetype:generate goal which enables you to create a new project from an archetype. See Table 10-6 for the parameters and description.

    Table 10-6 Parameters for the Basic WebService Project

    Parameter Purpose

    archetypeGroupId

    Identifies the group ID of the archetype that you want to use to create the new project. This must be com.oracle.weblogic as shown in the preceding example.

    archetypeArtifactId

    Identifies the artifact ID of the archetype that you want to use to create the new project. This must be basic-webservice as shown in the preceding example.

    archetypeVersion

    Identifies the version of the archetype that you want to use to create the new project. This must be 12.1.3-0-0 as shown in the preceding example.

    groupId

    Identifies the group ID for your new project. This usually starts with your organization's domain name in reverse format.

    artifactId

    Identifies the artifact ID for your new project. This is usually an identifier for this project.

    version

    Identifies the version number for your new project. This is usually 1.0-SNAPSHOT for a new project.


    You can also run the command without any arguments, as shown in the following example. In this case, Maven displays a list of available archetypes and prompts you to enter the required information.

    mvn archetype:generate
    

    After creating your project, it contains the following files:

    Description of maven_dt_004.png follows
    Description of the illustration maven_dt_004.png

    These files make up a small sample application, which you can deploy as is. You can use this application as a starting point for building your own application.

    There are a number of files included in the project; see Table 10-7 for the purpose of each file.

    Table 10-7 Files Created for the Basic WebService Project

    File Purpose

    pom.xml

    The Maven Project Object Model (POM) file that describes your new project. It includes the Maven coordinates that you specified for your project, and it also includes the appropriate plug-in definitions to use the WebLogic Maven plug-in to build your project.

    SayHello.java

    An example Web Service.


  2. After you have written your project code, you can use Maven to build the project. It is also possible to build the sample as is.

  3. Customize the POM to suit your environment. See Section 10.2.1.

  4. Compile your Basic WebService Project. See Section 10.2.2.

  5. Package your Basic WebService Project. See Section 10.2.3.

  6. Deploy your Basic WebService Project. For information about deploying it using Maven, see Section 10.2.4. For information about deploying it using other options, see Section 10.2.5.

  7. Test your Basic WebService Project.

    You can test the Basic WebService by visiting the following URL, on the WebLogic Server where you have deployed it:

    http://servername:7001/basicWebservice/SayHello
    

    The following shows the user interface for the Basic WebService:

    Description of wls_06.jpg follows
    Description of the illustration wls_06.jpg

    You can access the WSDL for the web service, and you can open the WebLogic Web Services Test Client by selecting the Test link. This enables you to invoke the web service and observe the output.

    To test the web service, select SayHello operation in the left hand pane, then enter a value for arg0 as shown in the following example, and select Invoke.

    Description of wls_08.jpg follows
    Description of the illustration wls_08.jpg

    Scroll down to see the test results, as shown in the following example:

    Description of wls_10.jpg follows
    Description of the illustration wls_10.jpg

10.5 Using the Basic MDB Maven Archetype

To use the Basic MDB project using the Maven Archetype:

  1. Create a new Basic MDB project using the Maven archetype, by running a command similar to the following:

    mvn archetype:generate
        -DarchetypeGroupId=com.oracle.weblogic.archetype
        -DarchetypeArtifactId=basic-mdb
        -DarchetypeVersion=12.1.3-0-0
        -DgroupId=org.mycompany
        -DartifactId=my-basic-mdb-project
        -Dversion=1.0-SNAPSHOT
    

    This runs Maven's archetype:generate goal which enables you to create a new project from an archetype. See Table 10-8 for the parameters and description.

    Table 10-8 Parameters for the Basic MDB Project

    Parameter Purpose

    archetypeGroupId

    Identifies the group ID of the archetype that you want to use to create the new project. This must be com.oracle.weblogic as shown in the preceding example.

    archetypeArtifactId

    Identifies the artifact ID of the archetype that you want to use to create the new project. This must be basic-mdb as shown in the preceding example.

    archetypeVersion

    Identifies the version of the archetype that you want to use to create the new project. This must be 12.1.3-0-0 as shown in the preceding example.

    groupId

    Identifies the group ID for your new project. This usually starts with your organization's domain name in reverse format.

    artifactId

    Identifies the artifact ID for your new project. This is usually an identifier for this project.

    version

    Identifies the version number for your new project. This is usually 1.0-SNAPSHOT for a new project.


    You can also run the command without any arguments, as shown in the following example. In this case, Maven displays a list of available archetypes and prompts you to enter the required information.

    mvn archetype:generate
    

    After creating your project, it contains the following files:

    Description of maven_dt_001.png follows
    Description of the illustration maven_dt_001.png

    These files make up a small sample application, which you can deploy as is. You can use this application as a starting point for building your own application.

    There are a number of files included in the project; see Table 10-9 for the purpose of each file.

    Table 10-9 Files Created for the Basic MDB Project

    File Purpose

    pom.xml

    The Maven Project Object Model (POM) file that describes your new project. It includes the Maven coordinates that you specified for your project, and it also includes the appropriate plug-in definitions to use the WebLogic Maven plug-in to build your project.

    Files under src/main/java

    An example Message Driven Bean that is used by the web application to store data.

    All other files

    HTML files that make up the web application user interface.


  2. After you have written your project code, you can use Maven to build the project. It is also possible to build the sample as is.

  3. Customize the POM to suit your environment. See Section 10.2.1.

  4. Compile your Basic MDB Project. See Section 10.2.2.

  5. Package your Basic MDB Project. See Section 10.2.3.

  6. Deploy your Basic MDB Project. For information about deploying it using Maven, see Section 10.2.4. For information about deploying it using other options, see Section 10.2.5.

  7. Test your Basic MDB Project.

    You can test the Basic MDB by visiting the following URL on the WebLogic Server where you deployed it:

    http://servername:7001/basicMDB/index.xhtml
    

    The following shows the user interface for the Basic MDB:

    Description of wls_12.jpg follows
    Description of the illustration wls_12.jpg

    Provide the Account Name and Amount, then select Deposit:

    Description of wls_14.jpg follows
    Description of the illustration wls_14.jpg

    As indicated in the user interface, you must check the WebLogic Server output to find the message printed by the MDB. It looks like the following example:

    The money has been deposited to frank, the balance of the account is 500.0