Developing JSR-168 portlets in Maven+Eclipse environment

Portal technology aims at providing personalized view of content aggregated from different systems with a single sign on. Here we will see how we can develop portlets (JSR-168 & 286 specifications) which can be deployed on any portal container using Maven and Eclipse environment. I assume the reader has idea of Portal system and had worked with them in different environment. There are many portal containers currently available now. among them following few famous portal systems

For now I will use Liferay to deploy my portlet. But the portlet that we are going to develop can deployed any portal system with minor changes.

Setting up environment :
We would require following softwares installed .

Creating basic project structure:

You can use maven archetype to create the basic project structure needed for developing a portlet. Execute following maven command to create structure

mvn archetype:generate
Once you execute the above command , you should see following list

......................................................................................................................
......................................................................................................................
12: internal -> maven-archetype-mojo (A Maven Java plugin development project)
13: internal -> maven-archetype-portlet (A simple portlet application)
14: internal -> maven-archetype-profiles ()
15: internal -> maven-archetype-quickstart ()
......................................................................................................................
......................................................................................................................
Choose a number: (1/2/3/4/5/6/7/8/9/10/11/12/13/14/15/16/17/18/19/20/21/22/23/24/25/26/27/28/29/30/31/32/33/34/35/36) 15: :

Choose portlet archetype and give the proper parameters it needed. You can learn more about maven archetypes here.

Converting to Eclipse Project :


With maven we can create eclipse descriptors easily so that we can use eclipse to work on our project. Use following maven command

mvn eclipse:eclipse

After you have imported this project into eclipse , You should see the following file structure in eclipse ( Assuming you named your project as “samplePortlet”).

Now our portlet is ready and you can use mvn clean:package to package it to a war file.

Tweaking it to make it work on any portal

The portlet we have developed above can be deployed only on Pluto container(Pluto is the Reference Implementation of the Java Portlet Specfication) . Because the maven archetype we used already configured its web.xml to use Pluto container. You can check the web.xml in yout project folder. Remove all other information related to portal from web.xml. Just retain generic tags and any tag library (other than portlet tag libraries) configurations.If you dont need any configurations, in simplest case it should look like as follows

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC 
                        "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
                        "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
    <display-name>Sample Portlet</display-name>
    <description>
                   Automated generated Application Wrapper
    </description>
</web-app>

Another bug is with portlet.xml. It gives a wrong portlet class name in it. It just gives the package name. Append class name to this value in portlet.xml . If you haven’t changed the default class name (which is “MyPortlet.java”) , the portlet-class tag should look as follows

<portlet-class>
    com.xebia.samplePortlet.MyPortlet
</portlet-class>

The portlet specific tags in web.xml will be added by portal container once we deploy it.

Deploying to portal container

We can deploy the above portlet on any portal container by just adding some descriptor files which are specific to each portal container. If it is liferay , we need to add a xml file with name liferay-display.xml under WEB-INF. Its content looks like follows

<pre lang="xml">
<?xml version="1.0"?>
<!DOCTYPE display PUBLIC "-//Liferay//DTD Display 5.1.0//EN" 
"http://www.liferay.com/dtd/liferay-display_5_1_0.dtd">
<display>
	<category name="Sample Portlets"> // Under what category should this portlet should be shown
		<portlet id="samplePortlet" /> // Portlet name specified in portlet.xml
	</category>
</display>

Debugging the code

A Portlet is a web application running in a J2EE container. Common web debugging tools like Sysdeo can be used along with eclipse. Sysdeo is a eclipse plugin helps in debugging portlet applications. Once we are done with configuring sysdeo , we can configure project to be a application running in tomcat using project properties.

This environment helps the developer to quickly start with portlet development . Because we are using maven here , we can make use of all the benfits maven provides like Reports generation , dependency managemt , etc.

Advertisements

One thought on “Developing JSR-168 portlets in Maven+Eclipse environment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s