10 Building Java EE Projects for WebLogic Server with Maven

You can use the WebLogic Maven archetypes to create, build, and deploy WebLogic Server Java EE applications.

Topics:

10.1 Introduction to Building a 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.2.1-0-0

Basic WebApp archetype

com.oracle.weblogic.archetype

basic-webapp

12.2.1-0-0

WebApp with EJB archetype

com.oracle.weblogic.archetype

basic-webapp-ejb

12.2.1-0-0

Basic MDB archetype

com.oracle.weblogic.archetype

basic-mdb

12.2.1-0-0

Basic WebServices archetype

com.oracle.weblogic.archetype

basic-webservice

12.2.1-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 build a Java EE project using the basic WebApp Maven archetype, you create the basic project, then customize, compile and package it. Then you deploy it and test it.

This section contains the following topics:

10.2.1 Creating a Basic WebApp Project

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.2.1-0-0
    -DgroupId=org.mycompany
    -DartifactId=my-basic-webapp-project
    -Dversion=1.0-SNAPSHOT

This command 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

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

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

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

groupId

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

artifactId

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

version

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:

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.

Table 10-3 describes the files included in the project.

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. It also includes the appropriate plug-in definitions needed 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.

10.2.2 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. 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, 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.3 Compiling Your Java EE Project

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

mvn compile

This command 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.4 Packaging Your Java EE Project

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

mvn package

This command 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 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.5 Deploying Your Java EE Project to the WebLogic Server Using Maven

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

mvn pre-integration-test

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

10.2.6 Deploying Your Java EE 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.7 Testing Your Basic WebApp Project

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

  1. Enter the following URL:
    http://servername:7001/basicWebapp/index.xhtml
    

    The following shows the user interface for the Basic WebApp:

  2. 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 build a Java EE project using the basic WebApp with EJB Maven archetype, you create the basic project, then customize, compile and package it. Then you deploy it and test it.

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.2.1-0-0
        -DgroupId=org.mycompany
        -DartifactId=my-basic-webapp-ejb-project
        -Dversion=1.0-SNAPSHOT
    

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

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

    Parameter Purpose

    archetypeGroupId

    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

    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

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

    groupId

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

    artifactId

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

    version

    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:

    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.

    Table 10-5 describes the files included in the project.

    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. 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 Customizing the Project Object Model File to Suit Your Environment.
  4. Compile your Basic WebApp with EJB Project. See Compiling Your Java EE Project.
  5. Package your Basic WebApp with EJB Project. See Packaging Your Java EE Project.
  6. Deploy your Basic WebApp with EJB Project. For information about deploying it using Maven, see Deploying Your Java EE Project to the WebLogic Server Using Maven. For information about deploying it using other options, see Deploying Your Java EE Project to the WebLogic Server Using Different Options.
  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:

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

10.4 Using the Basic WebService Maven Archetype

To build a Java EE project using the basic WebService Maven archetype, you create the basic project, then customize, compile and package it. Then you deploy it and test it.

To use the Basic WebService project using the Maven Archetype:

  1. Create a new Basic WebService project using the Maven archetype by issuing a command similar to the following:
    mvn archetype:generate
        -DarchetypeGroupId=com.oracle.weblogic.archetype
        -DarchetypeArtifactId=basic-webservice
        -DarchetypeVersion=12.2.1-0-0
        -DgroupId=org.mycompany
        -DartifactId=my-basic-webservice-project
        -Dversion=1.0-SNAPSHOT
    

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

    Table 10-6 Parameters for the Basic WebService Project

    Parameter Purpose

    archetypeGroupId

    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

    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

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

    groupId

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

    artifactId

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

    version

    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:

    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.

    Table 10-7 describes the files included in the project.

    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 Customizing the Project Object Model File to Suit Your Environment.
  4. Compile your Basic WebService Project. See Compiling Your Java EE Project.
  5. Package your Basic WebService Project. See Packaging Your Java EE Project.
  6. Deploy your Basic WebService Project. For information about deploying it using Maven, see Deploying Your Java EE Project to the WebLogic Server Using Maven. For information about deploying it using other options, see Deploying Your Java EE Project to the WebLogic Server Using Different Options.
  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:

  8. 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.
  9. 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.
  10. Scroll down to see the test results, as shown in the following example:

10.5 Using the Basic MDB Maven Archetype

To build a Java EE project using the basic MDB Maven archetype, you create the basic project, then customize, compile and package it. Then you deploy it and test it.

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.2.1-0-0
        -DgroupId=org.mycompany
        -DartifactId=my-basic-mdb-project
        -Dversion=1.0-SNAPSHOT
    

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

    Table 10-8 Parameters for the Basic MDB Project

    Parameter Purpose

    archetypeGroupId

    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

    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

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

    groupId

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

    artifactId

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

    version

    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:

    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.

    Table 10-9 describes the files included in the project.

    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. 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 Customizing the Project Object Model File to Suit Your Environment.
  4. Compile your Basic MDB Project. See Compiling Your Java EE Project.
  5. Package your Basic MDB Project. See Packaging Your Java EE Project.
  6. Deploy your Basic MDB Project. For information about deploying it using Maven, see Deploying Your Java EE Project to the WebLogic Server Using Maven. For information about deploying it using other options, see Deploying Your Java EE Project to the WebLogic Server Using Different Options.
  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:

  8. Provide the Account Name and Amount, then select Deposit:
  9. 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