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.....'

Wednesday, November 9, 2011

Parsing Bash Script Command Line Arguments

http://www.digitalpeer.com/id/parsing

for i in $*
do
 case $i in
     --prefix=*)
  PREFIX=`echo $i | sed 's/[-a-zA-Z0-9]*=//'`
  ;;
     --searchpath=*)
  SEARCHPATH=`echo $i | sed 's/[-a-zA-Z0-9]*=//'`
  ;;
     --lib=*)
  DIR=`echo $i | sed 's/[-a-zA-Z0-9]*=//'`
  ;;
     --default)
  DEFAULT=YES
  ;;
     *)
                # unknown option
  ;;
   esac
done

Tuesday, November 8, 2011

Maven - mavenize project and recipes

Links and commands used by the authors:
http://i-proving.com/2007/03/29/Maven-Recipes/

http://i-proving.com/2007/04/27/How-to-Mavenize-an-Existing-Eclipse-Project/ :
Insure that your M2_REPO variable is properly defined
mvn -Declipse.workspace=<path-to-eclipse-workspace> eclipse:add-maven-repo
mvn eclipse:eclipse -DdownloadSources=true

http://i-proving.com/2009/11/03/creating-a-maven-project-and-adding-it-to-subversion/ :
mvn archetype:create -DgroupId=[packageName] -DartifactId=[projectName]
mvn eclipse:eclipse -DdownloadSources.
For integration with Hudson, some nodes should be added to the pom.xml
 1. parent 
 2. distributionManagement 
 3. scm
Project under an existing project:
Using the command line, enter the following in the root of the parent project
mvn archetype:create -DgroupId=[packageName] -DartifactId=[projectName]
svn add 'projectName'
svn commit [projectName] pom.xml
mvn eclipse:eclipse -DdownloadSources

http://i-proving.com/2007/05/10/How-to-Determine-the-Maven-dependency-to-use-for-a-jar/ :
$ md5sum commons-collections.jar
$ openssl dgst -md5 commons-collections.jar
$ openssl dgst -sha commons-collections.jar
mvn deploy:deploy-file -Dfile=<the file to deploy> \
   -Durl=<url to your repository> \
   -Dpackaging=jar \
   -DrepositoryId=<the repositoryID> \
   -DpomFile=<path to your pom.xml file>

http://i-proving.com/2007/03/29/Split-Your-Project-Into-Sub-Projects/ :
+-- workspace/
    +-- myproject/
    |    +-- pom.xml    --IMP: please refer link for contents. Note <packaging>pom</packaging>
    +-- subproject-1/
    |    +-- pom.xml
    +-- subproject-2/
        +-- pom.xml
The  myproject pom makes myproject your "one stop shopping" location for running mvn goals such as install or release or eclipse:eclipse
If subproject-1 pom has
<groupId>ca.intelliware</groupId>
  <artifactId>subproject-1</artifactId>
  <version>1.0-SNAPSHOT</version>
and if subproject-2 consumes subproject-1 as a dependency
then
<dependencies>
    <dependency>
        <groupId>ca.intelliware</groupId>
        <artifactId>subproject-1</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
  </dependencies>
eclipse:eclipse goal behaviour:
If dependency.SNAPSHOT: subproject-2 should reference the Eclipse project for subproject-1 as a project dependency in its Java build path
else if dependency.RELEASE: Eclipse project that gets generated by the eclipse:eclipse will simply refer to the released version of the JAR in your M2_REPO repository
1. mvn archetype:create -DartifactId=myNewProject -DgroupId=ca.intelliware
2. cd myNewProject 
3. mvn archetype:create -DartifactId=myNewSubProject-i -DgroupId=ca.intelliware
4. cd ..
5. mv myNewProject/myNewSubProject-i ./myNewSubProject-i
6. Edit myNewProject.POM
<modules>
        <module>../myNewSubProject-1</module>
        <module>../myNewSubProject-2</module>
</modules>
7. mvn -DdownloadSources=true eclipse:eclipse
SPLITTING PROJECT: foobar->{foo,bar}
1. Rename foobar as foo
2. mvn archetype:create -DartifactId=foobar -DgroupId=ca.intelliware

Edit foobar.POM.packaging to "pom"
3. cd foobar
4. mvn archetype:create -DartifactId=bar -DgroupId=ca.intelliware
5. cd ..
6. mv foobar/foo ./foo
7. Edit foo.POM.artifactId to foo
8. Copy bar.POM.parent to foo.POM.parent
9. Edit foobar.POM

<modules>
    <module>../foo</module>
    <module>../bar</module>
</modules>
10. Move code you want to split from foo to bar
11. mvn eclipse:eclipse
Thats it for now!!!

C:\>mvn install:install-file -Dfile=utils.jar -DgroupId=com.zparacha.example -DartifactId=utils -Dversion=1.0 -Dpackaging=jar