Friday, April 25, 2014

Generic Portlet Development using Liferay Plugins SDK and ANT Tool

Liferay have given Plugins SDK to develop portlets and deploy into liferay portal environment.
Liferay Plugins SDK have used ANT tool to build and deploy the portlet applications.
So we need to install ANT tool in our machines.


Download Generic Portlet Examples


Note:

Before start all Java/JDK should be installed in our machine

Required Steps:

Step: 1

Install ANT so that we can build and deploy the portlet applications using Plugins SDK.

Download ANT binary distribution from Apache site

http://ant.apache.org/bindownload.cgi

Extract in any desired directory.

Crete ANT_HOME system environment variable.

Add ANT bin path  i.e. %ANT_HOME%/bin to system path variable so that we can run ant commands from any directory.

Install ANT

Step: 2

Download Liferay Portal Server and configure data base properties in portlet-ext.properties file which we need to place Liferay Home Directory.

Go through Following Link


Step: 3

Download Liferay Plugins SDK and Extract in desired Directory and change properties in build.prpoperties file and configure the portal server information. Provide Liferay Portal server information in Plugins SDK build.properties file

Instead of we change the properties in build.properties file we just clone the file and we modified it name as build.systyemUserName.properties

Any property of build.properties needs to change then we will use 
build.systyemUserName.properties file.


Example:
build.E5410.properties (my system user name E5410)




The following is liferay portal server information in build.systyemUserName.properties


app.server.type=tomcat
app.server.portal.dir=D\:\\Liferay Work Space\\bundles\\tomcat-7.0.40\\webapps\\ROOT
app.server.lib.global.dir=D\:\\Liferay Work Space\\bundles\\tomcat-7.0.40\\lib\\ext
app.server.deploy.dir=D\:\\Liferay Work Space\\bundles\\tomcat-7.0.40\\webapps
app.server.dir=D\:\\Liferay Work Space\\bundles\\tomcat-7.0.40


Follow the articles for more information


Step: 4

Now open command prompt and go to Liferay Plugins SDK portlet directory (D:\Liferay Work Space\liferay-plugins-sdk-6.2.0\portlets)

Portlet create command as Follows


Create Command Syntax

create  PortletName  "portlet Display Name"  PortletFrameWork


Real Use In Command Prompt

create  hello-world "Hello World" mvc



We have different Portlet Frame work that supported By Liferay Plugins SDK

mvc
jsf
icefaces
liferay_faces_alloy 
primefaces

Note:

In the above we might not see generic portlet but first use mvc portlet after create the portlet Skelton then change portlet extended class with Generic Portlet and use the code which I explained in previous article.



Tip:

To see portlet create command then open create.bat/create.sh files in Plugins SDK portlets directory. Open create.bat and see the syntax and option for create command

Example path

D:\Liferay Work Space\liferay-plugins-sdk-6.2.0\portlets\ create.bat




Important Observations:
  • When we run portlet create command from Plugins SDK portlets Directory then it will create portlet application with basic configuration file and its application structure. we can say portlet project Skelton with basic artifacts.
  • Now we need to add more code and other pages then we need to deploy portlet

Portlet Application Skelton after we run the portlet create command


Inside WEB-INF (before run ant deploy target)


Portlet Deployment

As soon as we run portlet create command from Plugins SDK portlet directory then it will create the portlet application with basic configuration file and it application structure.

In the portlet project parent directory we can see build.xml file and it have some ant targets to compile and deploy the portlet.

Run following command from portlet project directory where build.xml file


ant deploy




Tip:

To see available ant targets for portlet run following ant command

ant –p



 Once we run ant deploy target from portlet application then portlet will be deployed into liferay portlet server.

We already specify the portal server information in Plugins SDK build.proerties file so that as soon as we run ant deploy target portlet automatically deployed into server.

The following is screen show run portlet ant deploy target


Important Observation after run ant deploy target
  • Portlet ant deploy command is default ant target for portlet it’s responsible to deploy portlet into liferay portal environment and server information already provided in build properties file.
  • When we run ant deploy its will compile and package portlet application as war file and it will be deployed into portal server.
  • When we run ant deploy following action was takes place

Compilation:

All java classes and jsp pages will be compiled and at time of compilation it will create classes and lib directories in portlet WEB-INF directory.

All java .class file will be places in classes and if ant jar file then it will be places in lib 
directory. We can also add our project required jar file in lib directory.


Packaging:

In the packaging action all application files will be packages as war file and it will be placed in Plugins SDK /dist directory.



Deployment:

In the deployment action the created war file in dist directory will be copied into liferay portal server deploy directory.



As soon as war file places in liferay portal deploy directory then it will be copied into server deployment directory. Server deployment directory will be depends on server type

Example:


Tomcat Deployment Directory as Follows

tomcat-7.0.40\webapps

Jboss Deployment directory as Follows:

jboss-7.1.1\standalone\deployments

Note:

Server deployment directory information we already provided in build properties file that is why war file will be copied specified server deployment directory.


At the time of deployment liferay will add some other additional configuration file and its configuration to portlet so that it will be run in the server successfully.


Example:

It will liferay-plugin-package.xml file
In Jboss environment it will add one more action files called jboss-deployment-structure.xml


Note:

The important thing here liferay will add more configuration and more files to the portlet to successfully run in different portal environments and it will be take care by hot deployment process.

Portlet Hot Deployment is mechanism and it responsible to deploy portlet successfully into different server’s environments to do this task it will add additional configurations and additional configuration files to portlet application.

That’s is what as soon as war file available in liferay deploy directory then it will be copied and unpacked in server deployment directory.

Best observation you can see as follows

See the web.xml file configuration in Portlet Application and some have look in deployed portlet application web.xml



liferay-plugins-sdk-6.2.0\portlets\hello-world-portlet\docroot\WEB-INF\web.xml

Vs

D:\Liferay Work Space\bundles\tomcat-7.0.40\webapps\hello-world-portlet\WEB-INF\web.xml


Configuration files and Directories in Portlet Application before Deploy as follows



Configuration files and Directories in Portlet Application after Deploy as follows


Note:

As we already know portlet create command will be created Liferay MVC portlet and it will be extended MVCPortlet class but we want develop Generic Portlet

Modification required making it as Generic Portlet.

We already get the portlet Skelton with all configuration files and directories.

Step: 1

Change the portlet class in portlet.xml file to our custom class and that should extends the Generic Portlet Class

The following is configuration in portlet.xml file

<?xml version="1.0"?>
<portlet-app xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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" version="2.0">
       <portlet>
              <portlet-name>hello-world</portlet-name>
              <display-name>Hello World</display-name>
              <portlet-class>com.meera.generic.HelloWorld</portlet-class>
              <init-param>
                     <name>view-template</name>
                     <value>/view.jsp</value>
              </init-param>
              <expiration-cache>0</expiration-cache>
              <supports>
                     <mime-type>text/html</mime-type>
              </supports>
              <portlet-info>
                     <title>Hello World</title>
                     <short-title>Hello World</short-title>
                     <keywords>Hello World</keywords>
              </portlet-info>
              <security-role-ref>
                     <role-name>administrator</role-name>
              </security-role-ref>
              <security-role-ref>
                     <role-name>guest</role-name>
              </security-role-ref>
              <security-role-ref>
                     <role-name>power-user</role-name>
              </security-role-ref>
              <security-role-ref>
                     <role-name>user</role-name>
              </security-role-ref>
       </portlet>
</portlet-app>

Note:

Apart from portlet.xml file we have liferay-portlet.xml file and liferay-display.xml file you can see the tags inside those two file then you can understand more.

All liferay Configuration XML files DTD available in following location


Step: 2

Create src directory in portlet docroot/WEB-INF directory



Step: 3

Create java class in src directory that is we specifies in portlet.xml file in the previous step and java class should extends Generic Portlet class

Crete java class in the following package structure

com\meera\generic


The following is Portlet Class

public class HelloWorld extends GenericPortlet {

    public void init() {
        viewJSP = getInitParameter("view-template");
    }
   
    public void doView(
            RenderRequest renderRequest, RenderResponse renderResponse)
        throws IOException, PortletException {
       String currentViewPage=renderRequest.getParameter("jspPage");
       if(currentViewPage!=null&&!currentViewPage.equals("")){
              include(currentViewPage, renderRequest, renderResponse);
       }else{
              include(viewJSP, renderRequest, renderResponse);
       }
    }
   
    protected void include(
            String path, RenderRequest renderRequest,
            RenderResponse renderResponse)
        throws IOException, PortletException {

        PortletRequestDispatcher portletRequestDispatcher =
            getPortletContext().getRequestDispatcher(path);

        if (portletRequestDispatcher == null) {
            _log.error(path + " is not a valid include");
        }
        else {
            portletRequestDispatcher.include(renderRequest, renderResponse);
        }
    }

    protected String viewJSP;

    private static Log _log = LogFactoryUtil.getLog(HelloWorld.class);

}

Step: 4

Now run ant deploy target form portlet application directory then portlet will be deployed.
When deploy portlet if any compile errors will be display in command prompt console and build will be failed.

We already done many Generic Portlet Examples in Previous article you can apply all code sample and deploy portlet.


Note:

Before deploy the portlet make sure you portal server should be in running mode.

Using Portlet:

Access Liferay portal using you host name port


Generally when create portlet then it will be available in sample category.

Login as liferay admin in liferay portal in the application section you can see the sample category.

The following is login screen


Add portlet to page



Portlet Application Directory Structure Changes After Deployment

When we create portlet from create command then it will create portlet application in Plugins SDK portlets directory.

The portlet application Directory Name that is we provides in create command and its portlet root directory.

In Portlet Application we can see docroot directory and buil.xml file
build.xml file is ant file to deploy or build the portlet application i.e. deploy into liferay portal server.


docroot is directory there all portlet artifacts like jsp page and other directories will be available.


We can say portlet directory starts with docroot directory and for the development convenience liferay have made like that.

When we give the path names we will exclude the docroot directory in the name we specifies in the code or program.

At time of war package docroot will be excluded in portlet war file

Actual Portlet Application Structure


After Packages as War file or Deployed in Server Deployment Directory


When we observe above two screens in the second one docroon directory is not available it was excluded after packaged as war.

Note:

Ant+Plugins SDK not much flexible environment so that we will use Liferay IDE or MAVEN or Liferay Developers Studio for portlet development. But we need to have more undestand about all environments that is why this article help us.
Author

Popular Posts

Recent Posts

Recent Posts Widget