Welcome to the third article in these series about Liferay usage, maintenance and development. In this post I will show you how to set up your Liferay development enviroment using Apaches Maven2 build tool. We will develop a really simple JSR-168 portlet which can be deployed into Liferay. I assume you have read my previous articles and that you are using Liferay 5.2.3 on JBoss 5.1. I hope you like this post as much as the previous ones, if you have any feedback feel free to contact me or post a response.

The next version of Liferay, release date end Q1 begin Q2, will have complete support for maven including custom archetypes. Some of the steps described in this article will be obsolete then, but lets get started.

Installing Maven

The first step is to actually install Maven. You can find the download and installation instructions on the Maven website http://maven.apache.org/. Confirm that Maven is installed properly by executing the following command:

mvn --version

You should see the Maven version number.

Installing the Liferay dependencies into your local Maven repository

The next step is to install the Liferay portal-kernel and portal-service into your local repository. This manual step is required in order for Maven to resolve the Liferay portal dependencies. Navigate the ‘%JBOSS_HOME%/server/default/lib/ext’ folder where the JARs are located. Execute the following commands:

mvn install:install-file -Dfile="portal-kernel.jar" -DgroupId="com.liferay.portal" -DartifactId="portal-kernel" -Dversion="5.2.3-RELEASE" -Dpackaging=jar
mvn install:install-file -Dfile="portal-service.jar" -DgroupId="com.liferay.portal" -DartifactId="portal-service" -Dversion="5.2.3-RELEASE" -Dpackaging=jar

Now that you have installed the required dependencies let set up a quick Hello World portlet using Maven.

Setting up the Hello World Portlet

Now that we have all the required dependencies installed in our local Maven repository we can start building an application. Download the complete source here. Create the following folder structure:

/pom.xml
/src/main/java/nl/devatwork/HelloWorldPortlet.java
/src/main/webapp/WEB-INF/portlet.xml
/src/main/webapp/WEB-INF/web.xml

First let set up the POM so our dependencies can be loaded. Notice that I have set the Liferay dependencies as being provided. Add the following code to the pom.xml file you have just created:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>devatwork</groupId>
    <version>1.0.0-SNAPSHOT</version>
    <artifactId>maven-hello-world</artifactId>
    <packaging>war</packaging>
    <name>Maven Hello World Portlet</name>
    <dependencies>
        <!-- Java dependencies -->
        <dependency>
            <groupId>javax.portlet</groupId>
            <artifactId>portlet-api</artifactId>
            <version>2.0</version>
            <scope>provided</scope>
        </dependency>
        <!-- liferay dependencies -->
        <dependency>
            <groupId>com.liferay.portal</groupId>
            <artifactId>portal-service</artifactId>
            <version>5.2.3-RELEASE</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.liferay.portal</groupId>
            <artifactId>portal-kernel</artifactId>
            <version>5.2.3-RELEASE</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</project>

The portlet will be a very simple portlet. It will  print a string and the name of the current theme. Add the following code to the HelloWorldPortlet.java file:

package nl.devatwork;

import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.theme.ThemeDisplay;

import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

public class HelloWorldPortlet extends GenericPortlet {
    protected void doView(RenderRequest request, RenderResponse response) throws PortletException, java.io.IOException {
        ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);
        response.setContentType("text/html");
        response.getWriter().print("Hello World, " + themeDisplay.getTheme().getName());
    }
}

The next step is to set up the web.xml file, just copy the following code into it:

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

The final step of this sample portlet is the portlet.xml, again really straight forward. Just copy in the following code:

<?xml version="1.0" encoding="UTF-8"?>
<portlet-app xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0" xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd">
    <portlet>
        <portlet-name>HelloWorldPortlet</portlet-name>
        <display-name>Maven Hello World Portlet</display-name>
        <portlet-class>nl.devatwork.HelloWorldPortlet</portlet-class>
        <expiration-cache>0</expiration-cache>
        <supports>
            <mime-type>text/html</mime-type>
            <portlet-mode>view</portlet-mode>
        </supports>
    </portlet>
</portlet-app>

Now that the application is finished you can package it by executing the following command in the root of your application:

mvn clean package

Deploy the WAR to your application server and test it. Download the complete source here.

Now you are able to build Liferay plug-ins using Maven. I hope you liked this article. Please let me know if you have any questions or comments. Bye for now!

Be Sociable, Share!