Saturday, July 26, 2014

Liferay Hello World Portlet | How To Create Portlet Using Liferay Plugins SDK?

In this post we will create and deploy a simple "hello world" portlet using the Liferay Plugins SDK using step by step guide as follows.

Initial Setup
  • Install Java Development Kit JDK 1.5 or higher, and set JAVA_HOME environment variable point to installation home directory of JDK, (Click here to know how to setup environment variable on different operating systems like windows / Unix / Linux platforms)
    • For Windows, right click on My Computer -> Properties -> Advanced -> Environment Variables here you can add or edit windows environment variables e.g. if JDK is installed at "c:\program files\java\jdk1.6" then set JAVA_HOME to "c:\program files\java\jdk1.6"
    • For Unix or Linux platforms go to home directory of user and add entry to set variable and export it in hidden file named .profile 
      For example,
       
          JAVA_HOME=/usr/lib/java/java-1.6
          export JAVA_HOME
    • 
      Steps for creating portlet using Liferay Plugins SDK
      Creating portlets with the Plugins SDK is extremely simple. There is a portletsfolder inside of the Plugins SDK folder. This is where your portlet projects will reside. To create a new portlet, first decide what its name is going to be. You need both a project name (without spaces) and a display name (which can have spaces).
      When you have decided on your portlet's name, you are ready to create the project. For the greeting portlet, the project name is “hello-word”, and the portlet title is “Hello World”. Navigate to the portlets directory in the terminal and enter the following command
      
      For Linux, Unix and Mac OS X:
      
      ./create.sh hello-word "Hello World"
      
      For all windows platforms:
      create.bat hello-word "Hello World"
      
      You should get a BUILD SUCCESSFUL message from Ant, and there will now be a new folder inside of the portlets folder in your Plugins SDK. This folder is your new portlet project. This is where you will be implementing your own functionality. Notice that the Plugins SDK automatically appends “-portlet” to the project name when creating this folder. In this case you can find directory "hello-word-portlet" in side portlets directory Deploying the portlet on Liferay Portal
      • Make sure your Liferay Portal is already running or else you can start it by starting containing application server.
      • To deploy open a terminal window in your portlets/hello-world-portlet directory and enter this command
         
             ant deploy
        
        You should get a BUILD SUCCESSFUL message, which means that your portlet is now being deployed. If you switch to the terminal window running Liferay, and wait for a few seconds, you should see the message “1 portlet for my-greeting-portlet is available for use.” If not, something is wrong and you should double check your configuration.
      Go to your web browser and log in to the portal as explained earlier. Then mouse over Add at the top of the page, and click on More.
                                          
Select the Sample category, and then click Add next to Hello World. Your portlet should appear in the page below. Congratulations, you've just created your first portlet!