Script to build and stage Java applications from a given svn URL

  bash, java

Example script call:

buildProject.sh svn://www.example.com/myRepo/my-map/tags/my-map-1.0.3/

Shell script:

#!/bin/bash
# if no argument was passed
RETURNCODE=1
if [ -z $1 ] ; then
        echo ""
        echo "Syntax:  buildProject.sh svn://www.example.com/<repo>/<project>/<subdir>/<version-dir>"
        echo ""
        echo "Ex: buildProject.sh svn://www.example.com/myRepo/my-map/tags/my-map-1.0.3/"
        echo ""
        exit $RETURNCODE
else
        svnLocation=$1
        echo "[INFO] Let's work on building your project in $svnLocation"
fi
 
# Check each section of the passed in svn URL
repository=`echo "$svnLocation" | cut -d'/' -f 4`
project=`echo "$svnLocation" | cut -d'/' -f 5`
subdir=`echo "$svnLocation" | cut -d'/' -f 6`
versionDir=`echo "$svnLocation" | cut -d'/' -f 7`
versionNumber="${versionDir##*-}"
 
echo "--------------------------------------"
echo " repository: $repository"
echo "    project: $project"
# echo "     subdir: $subdir"
# echo "version dir: $versionDir"
echo "  version #: $versionNumber"
echo "--------------------------------------"
read -p "Is this correct (y/n)? "
if [ "$REPLY" == "n" ]; then
        echo "Sorry about that. Go ahead and change the program arguments, and let's try again."
        exit $RETURNCODE
fi
 
 
svnUserName=svnBuildAccount
svnPassword=secretpassword
 
#export the project
if [ -d tmp ]
then
  echo "[INFO] I'll be using the ./tmp directory in `pwd`"
else
  mkdir tmp
  echo "[INFO] I just created a tmp directory to work in"
fi
 
# cd to tmp
cd tmp
echo "[INFO] cd tmp. I'm currently in: `pwd`"
 
echo "[INFO] Exporting  project from $svnLocation"
svn --username $svnUserName --password $svnPassword export $svnLocation
 
echo "[INFO] Entering the $versionDir directory"
cd "$versionDir"
 
echo "[INFO] Eliminating the <scm> entry, if it exists"
cat pom.xml | grep -v "<scm>" | grep -v "<connection>scm:" | grep -v "<developerConnection>scm" | grep -v "<url>scm" | grep -v "</scm>" > pom2.xml
cp pom2.xml pom.xml
 
echo "[INFO] Trying to build the project"
mvn -e package > buildresult.txt
 
# grep for success
SUCCESS=`grep "BUILD SUCCESSFUL" buildresult.txt`
 
if [ "$SUCCESS" == "[INFO] BUILD SUCCESSFUL" ] ; then
        echo "[INFO] Great! Your build was successful!"
else
        echo "[INFO] Sorry, but your build was not successful:"
        cat buildresult.txt
        exit $RETURNCODE
fi
 
 
# try to figure out what the file is that we just built, ear or war
cd target
PRODUCT=`ls *.war`
if [ -z $PRODUCT ]; then
        echo "[INFO] Couldn't find any .war files, trying .ear now"
        PRODUCT=`ls *.ear`
fi
if [ -z $PRODUCT ]; then
        echo "Sorry, could not find any .war or .ear files in `pwd`. Exiting..."
        exit $RETURNCODE
fi
 
 
PRODUCT=`pwd`/$PRODUCT
echo "[INFO] Final product is: $PRODUCT"
 
# create the path where the fill will go
BASEPATH=/share/maven-builds/releases
 
# go to the base path
cd $BASEPATH
 
# repository dir, ex: "myRepo"
if [ -d $repository ]
then
  cd $repository
  echo "[INFO] $repository already exists, I'm now in `pwd`"
else
  mkdir $repository
  cd $repository
  echo "[INFO] I created dir $repository and I'm now in `pwd`"
fi
 
# project dir, ex: "my-map"
if [ -d $project ]
then
  cd $project
  echo "[INFO] I'm now in `pwd`"
else
  mkdir $project
  cd $project
  echo "[INFO] I created dir $project and I'm now in `pwd`"
fi
 
# version number dir, ex: "1.0.3"
if [ -d $versionNumber ]
then
  cd $versionNumber
  echo "[INFO] I'm now in `pwd`"
else
  mkdir $versionNumber
  cd $versionNumber
  echo "[INFO] I created dir $versionNumber and I'm now in `pwd`"
fi
 
# copy the file
echo "Now executing: cp $PRODUCT `pwd`"
cp "$PRODUCT" `pwd`
 
# done
echo [INFO] Finished.

Sample output:

[omega@omega ~]$ ./buildProject.sh svn://www.example.com/myRepo/my-map/tags/my-map-1.0.3/
[INFO] Let's work on building your project in svn://www.example.com/myRepo/my-map/tags/my-map-1.0.3/
--------------------------------------
 repository: myRepo
    project: my-map
  version #: 1.0.3
--------------------------------------
Is this correct (y/n)? y
[INFO] I'll be using the ./tmp directory in /home/omega
[INFO] cd tmp. I'm currently in: /home/omega/tmp
[INFO] Exporting  project from svn://www.example.com/myRepo/my-map/tags/my-map-1.0.3/
svn: Destination directory exists; please remove the directory or use --force to overwrite
svn: 'my-map-1.0.3' already exists
[INFO] Entering the my-map-1.0.3 directory
[INFO] Eliminating the <scm> entry, if it exists
[INFO] Trying to build the project
[INFO] Great! Your build was successful!
[INFO] Final product is: /home/omega/tmp/my-map-1.0.3/target/my-map-1.0.3.war
[INFO] myRepo already exists, I'm now in /share/maven-builds/releases/myRepo
[INFO] I'm now in /share/maven-builds/releases/myRepo/my-map
[INFO] I'm now in /share/maven-builds/releases/myRepo/my-map/1.0.3
Now executing: cp /home/omega/tmp/my-map-1.0.3/target/my-map-1.0.3.war /share/maven-builds/releases/myRepo/my-map/1.0.3
[INFO] Finished.
[omega@omega ~]$