Thursday, November 10, 2011

Bash script to build and deploy GWT-SpringRoo-MVN app to tomcat


Following is a bash script that I wrote to perform following batch of tasks:
1. Invoke ROO shell to perform a startup scan to update generated sources. Optional(--rooscan option)
2. Somehow ROO updated the project POM to use GWT-2.3.0. Hence after scan is complete, using "sed" to update the POM(--modpom).
3. There is an option to do an "mvn gwt:run" or
to do an "mvn package" and deploy it to tomcat installation(--deploy) and copy some extra libs into the deployed app(--cpextjars)



#!/bin/sh
####################################################
#
# Author: Tapomay Dey
# Start Date: 9th November 2011
#
####################################################
# Please change the following variables to match your environment

project_home=
roo_home=
maven_home=
tomcat_deploydir=/var/lib/tomcat6/webapps
version=0.1.0.BUILD-SNAPSHOT

CLEAN=clean
ROOSCAN=false
DEPLOY=false
MODPOM=false
CPEXTJARS=false
GWTVERSION=2.2.0

usage() {
  echo "Usage: argument descriptions"
  echo "${0} [--rooscan] [--deploy] [--noclean] [--modpom] [--extjars]"
  echo "--rooscan: invokes roo shell for auto scan before proceeding"
  echo "--deploy: deploys app to tomcat. If not set, the app is run using gwt:run"
  echo "--noclean: mvn clean is not performed"
  echo "--modpom: Modifies project POM to reset GWT version to 2.2.0 and sets datanucleus fork=true."
  echo "--extjars: After deploying app to tomcat, copies external jars to deployment and restarts tomcat."
}

rooscan() {
  cd $project_home
  echo "Running ROO shell in $project_home ....."

  TEMP_FILE=`mktemp -t input.XXXXXX.tmp`
  echo 'quit' >> $TEMP_FILE
  $roo_home/bin/roo.sh < $TEMP_FILE
}

maven_run() {
  echo "Running APP....."
  cd $project_home
  pwd
  echo "$maven_home/bin/mvn ${CLEAN} ..... gwt:run -e";
  $maven_home/bin/mvn ${CLEAN}..... gwt:run -e

}

maven_deploy() {
  echo "Deploying to tomcat....."
  cd $project_home
  pwd
  echo "$maven_home/bin/mvn ${CLEAN} ..... package -e"
  $maven_home/bin/mvn ${CLEAN} ..... package -e

  cd target
  cp project-${version}.war project.war
  sudo cp project.war ${tomcat_deploydir}
  sudo /etc/init.d/tomcat6 restart
  echo 'Deployed to tomcat.....Press Enter'
  read s
}

modpom() {
  file=pom.xml
  temp_file=pOmTeMp.tmp
sed "
  # Change fork value for datanucleus to true
  /<groupId>org.datanucleus<\/groupId>/,/<\/plugin>/ {
  s/<fork>false<\/fork>/<fork>true<\/fork>/
  }
  # Set GWT versions to 2.2.0
  /<artifactId>gwt-maven-plugin<\/artifactId>/,/<\/plugin>/ {
  s/<version>.*<\/version>/<version>${GWTVERSION}<\/version>/
  }
  " <${file} > ${temp_file}
 
  mv ${temp_file} ${file}
}

cpextjars() {
  echo "Copying external jars to tomcat deployment....."
  cd $project_home
  sudo cp externaljars/extralib1.jar ${tomcat_deploydir}/project/WEB-INF/lib
  sudo cp externaljars/extralib2.jar ${tomcat_deploydir}/project/WEB-INF/lib
  sudo /etc/init.d/tomcat6 restart
  echo 'Deployed External JARs to tomcat.....Press Enter'
  read s
}

echo "WELCOME ${USER}"
cd $project_home
pwd

echo "Options Selected: $*"
for i in $*
do
case $i in
    --help)
usage
exit
;;
    --rooscan)
echo 'Rooscan option detected.'
ROOSCAN=true
;;
    --noclean)
echo 'No-clean option detected.'
CLEAN=''
;;
--deploy)
echo 'Deploy option detected'
DEPLOY=true
;;
--modpom=*)
echo 'modpom with value option detected'
MODPOM=true
GWTVERSION=`echo $i | sed 's/[-a-zA-Z0-9]*=//'`
;;
--modpom)
echo 'modpom option detected'
MODPOM=true
;;
--cpextjars)
echo 'cpextjars option detected'
CPEXTJARS=true
;;
    *)
                # unknown option
;;
  esac
done

echo "ROOSCAN=${ROOSCAN}"
echo "DEPLOY=${DEPLOY}"
echo "MODPOM=${MODPOM}"
echo "GWTVERSION=${GWTVERSION}"
echo "CPEXTJARS=${CPEXTJARS}"

if ${ROOSCAN};
then
rooscan
fi

if ${MODPOM};
then
modpom
fi

if ! ${DEPLOY};
then
echo 'RUN GWT'
maven_run
else
echo 'DEPLOY TO TOMCAT'
maven_deploy
fi

if ${CPEXTJARS};
then
cpextjars
fi

sudo -k
echo 'Exiting.....'

No comments:

Post a Comment