Using Maven 2 to Build WebLogic Portal Applications

After all that work we are now ready to create our POM files to enable Maven to build our portal application. First, though, a quick word is needed about the portal project structure in WebLogic Workshop. Maven is a tool that is based on the concept of convention over configuration; this means that it expects things to be laid out in a certain way. With respect to portal project structure, Maven has a structure that it prefers for Java and Web projects, and it is recommended that this structure be used when possible.

Using the Maven preferred structure is straightforward; when creating your WebLogic Portal WAR, simply specify the paths to the source and the Web content directories, as shown below. Note that while this is the recommended structure, if you have an existing project that uses the default paths, you can still configure Maven to build the project, but you will need additional configuration settings in the POM to inform Maven of the changed paths.

Figure 1. Eclipse new portal Web project dialog

With respect to the WebLogic Portal EAR, the default structure works just fine with Maven, and no changes are needed.

As a final note on project structure, it is highly recommended that when creating a new project that the source code for the project be placed outside the workspace into a separate directory. This makes the project much easier to manage under source control since there is no temptation to check in unneeded workspace artifacts. The diagram below is an example structure used for this tutorial that illustrates this.

Figure 2. Example project structure

Now we can finally create our various POM files for each part of the application. In total, we will have three POM files, as discussed in subsequent sections. Note that full versions of each POM are included in the sample code and thus we will only be highlighting the prominent aspects of each POM in the sections below, in particular those areas that relate to building a WebLogic Portal application. Creating and using POMs is well documented on the Maven site and this information will not be duplicated here.

Parent POM

The first POM we will create is the parent POM. This file enables us to build both the EAR and WAR as one unit and allows the centralization of certain settings. While in Maven each artifact, such as the EAR and WAR, is a separate entity, given their intimate nature you will most likely be building and deploying them as a unit.

Within this parent POM we declare modules for the portalEAR and portalWAR projects as follows:

 portalEAR portalWAR  

This declaration is what enables us to build both projects at the same time. Note that this is not strictly necessary and is provided as a convenience, since the portalEAR also declares a dependency on portalWAR in its POM, and so the war file will automatically be built by Maven if it has not been built previously.

Next, we configure the maven-compiler-plug-in to target JDK 5 for compile purposes, as shown here:

 maven-compiler-plugin 1.5 1.5   

Web Application POM

The Web application POM is used to build the WAR represented by the portalWAR project. In general, building a WebLogic Portal WAR is no different than building a standard Java EE WAR as supported by Maven. However, we do need to support annotations given that WebLogic Portal applications typically make use of page flows and Beehive annotations.

The JDK includes an annotation processing tool (APT), and Maven 2 does have a plug-in that wraps this tool, maven-apt-plugin , which can be found here. At the time this tutorial was written, the maven-apt-plugin had a few rough edges that prevented it from working correctly with a WebLogic Portal application. A patched version that corrects these deficiencies is included in the sample code and will need to be built first. This patched version can be built and installed running the mvn:package and mvn:install command in the maven-apt-plugin directory. To avoid version conflict, this version is labeled with a -BEA label.

Note that since the time this article was written, the maven-apt-plugin has been corrected to fix the deficiencies noted here. Thus you have the choice of using the plugin included with this article or using the snapshot version of the maven-apt-plugin . If you use the snapshot version of the maven-apt-plugin , the sourcePath configuration option is no longer required and can be removed from the pom.xml for the portalWAR project.

Having Maven process the annotations is straightforward, and the section of the POM that does so is shown below:

 org.apache.myfaces.tobago maven-apt-plugin 1.0.10-BEA  generate-sources execute  .apt_generated x=3, web.content.root=$/src/main/webapp 1.5 true true false false $/src/main/java     

The key item here is that we set the web.content.root property to point to the Web content source directory as this parameter is required by the Beehive annotation processor. The other parameter of interest is the sourcePath . This was added to the patched version of maven-apt-plugin as it is required by the APT tool.

The last item of interest is the dependencies on the various WebLogic Server and WebLogic Portal libraries and shared libraries. These are included at the bottom of the POM and can be viewed by downloading the sample code.

Enterprise Application POM

The POM for the portalEAR project is, for the most part, a standard POM that could be used with any Java EE application. The configuration of the maven-ear-plugin appears as follows:

 org.apache.maven.plugins maven-ear-plugin  Portal Maven2 Sample Application Portal Maven2 Sample Application 1.4 EarContent **/application.xml  portal-maven2-sample portalWAR /portalWAR     

The only item of interest here is that we exclude the application.xml file and allow Maven to generate this file for us. This is required since the portalWAR will be generated with a version number appended to it as per Maven conventions, and allowing Maven to generate the application.xml ensures the correct module name is used.

Building the Application

Now that we have all the pieces, building the portal application is as easy as running mvn:package from the /applications directory. If you check the target directory under the portalWAR and portalEAR folders you should see copies of the WAR and EAR respectively.

Sample Code

This tutorial includes an archive that contains a sample WebLogic Portal application with all the resources needed to build the application using Maven 2. To use this sample, simply unpack the archive, ensuring that paths are preserved, and then modify maven/scripts/maven.bat to point to the location of your BEA and Maven home directories.

Summary

Building WebLogic Portal applications in Maven 2 can be accomplished with a little bit of effort. The various features included with Maven 2 make it an attractive option to many organizations, and this tutorial should assist you in integrating Maven 2 with WebLogic Portal.

References

Gerald Nunn is a business principal consultant with BEA Systems Professional Services.