Tuesday, September 30, 2008

Automate uPortal deployPortletApp Plutofication with Plutofy Bash Script

Plutofication is the changing of web.xml so that a portlet (webapp) can work with the Apache Pluto portlet API. While there is a Maven 2 plugin that can actually do this for you here:
* http://portals.apache.org/pluto/maven-pluto-plugin/index.html
* http://portals.apache.org/pluto/v11/deploying.html
it requires that you know the correct version of pluto for your portal. It also makes it difficult to use that plugin with other plugins that modify web.xml like the jspc plugin (JSP compiler):
* http://mojo.codehaus.org/jspc-maven-plugin/usage.html
So I wrote a bash script that will call the uPortal deployPortletApp ant target to easily plutofy a war in preparation for its deployment called plutofy.sh:
#!/bin/bash

# plutofy.sh
#
# Written by Gary S. Weaver
#
# Note: To be jar-compliant, the MANIFEST.MF file must be the first entry in the
# archive, and this doesn't enforce that. In fact, you can see for yourself with
# jar tvf warfilename that it might not be in that order. However, that hasn't 
# caused a problem for me yet.
#
# To unplutofy, see: http://github.com/garysweaver/unplutofy
#
# Release 2009-12-17:
# Removed requirement for canonicalize.sh external script.
#
# Release 2009-10-15:
# It now will be sure that existing plutofied war directory and plutofied file are removed and fail if they aren't. Now returns error code if fails.
#
# Release 2009-04-10:
# readlink -f doesn't work in new Leopard Mac OS X 10.5 installs (although it works for 10.5 that was upgraded), so using Jesse Wilson's canonicalize script instead
# available at http://publicobject.com/2006/06/canonical-path-of-file-in-bash.html
#
# Release 2009-01-13:
# Previously wasn't deleting previous webapp, so all kinds of just was accumulating from past builds.
# Be sure to backup the deployed webapp directory before running this script, as it will be
# deleted.
#
# Release 2008-09-30:
# This will use uPortal's deployPortletApp target to call the right version of pluto to plutofy
# the web.xml within your portlet war file and will create a new (warfile).plutofied file in
# the same directory as the original war file
set -e

warfilepathname="$1"
currentdir="$PWD"

if [ -z "$UPORTAL_HOME" ]; then
  echo
  echo "Must set UPORTAL_HOME environment variable to uPortal home. for example, for the quickstart this might be: /mystuff/uPortal_rel-2-5-3-1-GA-quick-start/uPortal_rel-2-5-3-1-GA"
  echo
  echo "FAILED!"
  exit 1
fi

if [ -z "$UPORTAL_TOMCAT_HOME" ]; then
  echo
  echo "Must set UPORTAL_TOMCAT_HOME environment variable to the uPortal Tomcat home. for example, for the quickstart this might be: /mystuff/uPortal_rel-2-5-3-1-GA-quick-start/apache-tomcat-5.5.20"
  echo
  echo "FAILED!"
  exit 1
fi

if [ ! -f "$warfilepathname" ]; then
  echo
  echo "usage: plutofy.sh warfilepathname"
  echo
  echo "FAILED!"
  exit 1
fi

# Note: use to use "readlink -f $warfilepathname" to get the canonical pathname but some versions of readlink have different behavior for -f, so that won't work! -e is same but causes error to be generated if file doesn't exist.

# canonicalize warfilepathname
warfilepathname=`cd -P -- "$(dirname -- "$warfilepathname")" && printf '%s\n' "$(pwd -P)/$(basename -- "$warfilepathname")"`
# this gets the file name from the pathname
warfilename="${warfilepathname##*/}"
# this gets the file name without the extension
warfiledirname="${warfilename%%.*}"

newwardir="$UPORTAL_TOMCAT_HOME/webapps/$warfiledirname"

if [ ! -d "$UPORTAL_TOMCAT_HOME" ]; then
  echo
  echo "Please update plutofy.sh with the name of the tomcat directory in $UPORTAL_HOME/.."
  echo
  echo "FAILED!"
  exit 1
fi

rm -rf $newwardir
if [ -d "$newwardir" ]; then
  echo
  echo "Please first remove existing directory $newwardir"
  echo
  echo "FAILED!"
  exit 1
fi

rm -rf $warfilepathname.plutofied
if [ -f "$warfilepathname.plutofied" ]; then
  echo
  echo "Please first remove existing file $warfilepathname.plutofied"
  echo
  echo "FAILED!"
  exit 1
fi

cd "$UPORTAL_HOME"
ant deployPortletApp -DportletApp="$warfilepathname"
cd "$newwardir"
zip -r "$warfilepathname.plutofied" .

if [ -f "$warfilepathname.plutofied" ]; then
  echo "Created $warfilepathname.plutofied"
fi
Note: the line to canonicalize was based on the "canonicalize" bash script by Jesse Wilson:
#!/bin/bash
# Provided/written by Jesse Wilson 6/4/06. http://publicobject.com/2006/06/canonical-path-of-file-in-bash.html
# This is better than readlink -f since it works for new Leopard OS X 10.5 installs.
cd -P -- "$(dirname -- "$1")" &&
printf '%s\n' "$(pwd -P)/$(basename -- "$1")"
Be sure to add plutofy.sh your path and to chmod +x it.

No comments: