Tuesday, February 18, 2014

Using Gradle to deploy a portlet on Apache Pluto 2

I am currently in the process of learning portlets.  Until recently I had been using the OpenPortal Portlet Container for all my portlet projects.  The primary reason for using that specific container was that it was the only one that I could get to work on my computer. Unfortunately OpenPortal does not support custom portlet modes nor (I'm guessing) does it support any of the portal bridges (which prevents me from turning a Struts application into a portlet).

I know most companies use IBM WebSphere as their portlet container, but I really don't want to learn how to use another application server (if I can help it).  Also WebSphere is expensive (in my opinion) to use.  I tried several times getting JetSpeed and Lifreray to work but didn't have much success.  Although Liferay has a very active community forum, I felt the product itself was too bloated for my personal tastes.  This left me with only one other option, Apache Pluto.

At measly 11.7 MB, the download for Apache Pluto is extremely small compared to the aforementioned portlet containers.  In comparison JetSpeed is 94 MB in size and (the behemoth) Liferay is 280 MB!  And that's not including any additional files that you may need to get Liferay to work.  Besides being lightweight another attractive feature of Apache Pluto is that it's what other containers use to determine if they are adhering to the Java Portlet Specifications.

See Also: About Pluto and The Pluto Project Mission

The particular version of Apache Pluto that I downloaded is 2.0.3 which comes bundled with Apache Tomcat 7.0.21.  It comes as a zip file which I then unzipped into a folder named tomcat in my user account folder.  I then started Spring Tool Suite (ie Eclispe) and added the Tomcat server.

It was at this point that I started running into problems.  According to Pluto's Getting Started page I'm supposed to go to http://localhost:8080/pluto/portal and login as pluto.  Unfortunately when I did that the only thing that I got was a HTTP 404 error and a message saying "The requested resource (/pluto/portal) is not available."

To solve that particular issue I had to start Tomcat (through the Eclipse IDE), double click the Tomcat server, and then select the "Use Tomcat installation (takes control of Tomcat installation)" option under the "Server Locations" section.  I then closed the tab and clicked "Yes" when it asked me if I wanted to save the modifications made to Tomcat.


Note: An alternative method for accessing this settings window for Tomcat is to right click on the server and then select Open.


See Also:  facesagar's YouTube video "How to resolve 404 Error with Tomcat Serve and Eclipse"

At this point I am able to access and login to the main Pluto page.


When you login to Pluto it takes you to the "About Apache Pluto" page.


To help with deploying portlet applications, Apache Pluto comes with an Ant task named passemble that fills in the details for the deployment descriptor file (ie. web.xml).

See Also: "Deploying Portlet to Pluto Portal" and  "Pluto Ant Tasks"

Well if your like me, I use Gradle instead of Ant for all my Java builds.  Here's the Gradle script that I used:

apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'eclipse-wtp'

configurations {
   passemble
}

repositories {
   mavenLocal()
   mavenCentral()
}

dependencies {
   passemble 'org.apache.portals.pluto:pluto-ant-tasks:2.0.3'
}

sourceSets {
   main {
      java {
         srcDirs "src"
      }

      resources {
         srcDir "src"
      }
   }
}

eclipse {
  wtp {
    component {
      resource deployPath: '/', sourcePath: '/WebContent', tag: 'defaultRootSource'
      resource deployPath: '/WEB-INF/classes', sourcePath: '/src'
    }
  }
}

task passemble {
   description = 'Assemble project so that it may be run on Apache Pluto'

   doLast {
      println 'creating taskdef passemble...'
      ant.taskdef(name: 'passemble', classname: 'org.apache.pluto.ant.AssembleTask', classpath: configurations.passemble.asPath)

      println 'creating temporary web.xml file...'
      File tmp = file('web.xml')
      tmp.createNewFile()

      PrintWriter printWriter = new PrintWriter(tmp);

      printWriter.println '<?xml version="1.0" encoding="UTF-8" standalone="no"?>'
      printWriter.println '<!DOCTYPE web-app PUBLIC'
      printWriter.println '  "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"'
      printWriter.println '  "http://java.sun.com/dtd/web-app_2_3.dtd">'
      printWriter.println '<web-app id="WebApp_ID">'
      printWriter.println '</web-app>'

      printWriter.close();

      println 'executing task passemble...'
      ant.passemble(
         webxml: 'web.xml', 
         portletxml: 'WebContent/WEB-INF/portlet.xml', 
         destfile: 'WebContent/WEB-INF/web.xml')

      println 'deleting temporary web.xml file...'
      delete file('web.xml')
   }
}

See Also: "Chapter 17. Using Ant from Gradle"

The only thing left now to do is export the project as a WAR file and deploy it to Pluto.  To deploy a portlet click on the "Pluto Admin" tab and then select the "Upload and deploy a new portlet war" link on the bottom.

 Note: You will need to modify the tomcat-users.xml file before you can do this otherwise you'll get an HTTP 403 error.  The following picture shows how to access the tomcat-users.xml file through Eclipse.


 In particular I changed the following line...

<user name="tomcat" password="tomcat" roles="tomcat,pluto,manager"/>

...to...

<user name="tomcat" password="tomcat" roles="tomcat,pluto,manager-gui"/>

The following shows how to add the HelloWorldPortlet to the Pluto Admin page.

Once you have everything selected (the portlet you want and the page you want it on) just click Add Portlet and you're done.

Note: If you experience problems displaying the different modes for your custom portlet, try running the "Clean..." task (which clears out all published state) and then the "Clean Tomcat Work Directory..." task.

4 comments:

  1. Hi Todd,

    I have to build a basic Hello World portlet and deploy on Apache Pluto.

    I referred to following link and built my .war file
    http://people.cis.ksu.edu/~hankley/d764/tut07/Konreddy_Portlet.pdf

    But I m not able to get how to add it into Pluto. Also whether project has been rightly made as portal or not.

    I just want basic tutorial/sample project/steps to do this.

    I have Eclipse Juno and Apache Pluto 2.0

    Thanks and Regards,
    Naveen

    ReplyDelete
  2. The instructions in the pdf look okay. You might want to change the body for the param-name element in the web.xml file from portlet-class to portlet-name. Are you able to log into the Pluto server? Are you getting a 403 error when you click the link to upload the portlet war?

    ReplyDelete
  3. Hi Todd,

    Thanks for the reply.

    This is what I have (in brief as I m unable to attach complete xml) in my three files : HelloWorld.java, web.xml and portlet.xml ->

    *******HelloWorld.java*********
    import javax.portlet.*;
    import java.io.*;


    public class HelloWorld extends GenericPortlet{
    protected void doView(RenderRequest request, RenderResponse response) throws
    PortletException, IOException {
    response.setContentType("text/html");
    response.getWriter().println("Hello, world!");
    }

    }
    ***********HelloWorld.java ends***********

    *********portlet.xml*************

    tag : portlet-name
    value : hello
    tag : portlet-class
    value : HelloWorld


    **************portlet.xml ends***************

    *****************web.xml*****************

    tag : servlet-name
    value : hello
    tag : servlet-class
    value : org.apache.pluto.container.driver.PortletServlet



    ******************web.xml ends****************

    Yes, I m able to login to pluto and access Pluto Admin Page. When I click on Upload and deploy a new portlet war I m redirected to tomcat page where all the deployed wars are available. I m stuck on what to do next.

    Thanks and Regards,
    Naveen

    ReplyDelete
  4. Scroll down to the bottom of that page (the Tomcat Web Application Manager). There's a section titled "WAR file to deploy". Click on the browse button and select the war file that contains your portlet application (ex. HelloWorld.war). Then click deploy. After you do that you'll have to re-login to pluto. Click the Pluto Admin tab and your portlet application should appear in the select box under Portlet Applications.

    ReplyDelete