Automated Java application build and tomcat deployment bash script

Sat, 04/29/2017 - 11:50

We humans are inherently lazy. We want to duplicate ourselves so that we don't work. However, our clones will eliminate us one beautiful day and govern the world on their own. I am sure we ourselves did the same to those who wrote our DNA code. Whatever, here I am not going to clone anybody. I only want to automate the boring sequence of commands that I type in my terminal every time I want to build my project, deploy it to any chosen local tomcat server and finally open the deployed application in my favorite browser.

I hear you shouting "Use Eclipse, Idiot !". Well, there might be a couple of reasons I do not want to use Eclipse now. It's not that I do not like Eclipse or I never use it. In fact I exploit it every single day, without even appreciating the tremendous job it does for me behind the stage. Let's say you downloaded a third-party source code from an open GitHub and you want to see what it does. In my opinion running a script in terminal  in order to build and deploy is much faster than just even creating an Eclipse project from an existing source.

Ok, where to create our script or what should we write into it? First of all we need to have the script's directory in our search path. Linux determines the executable search path with the $PATH environment variable. I just decided to create a bin folder inside  my home /home/ara/ directory and add it to my $PATH. Let's just do that. 

$ mkdir ~/bin
$ echo -e "\nexport PATH=\$PATH:~/bin" | sudo tee -a /etc/profile

Actually, here we need to restart our linux machine in order to add ~/bin to search path.

I want my command to look like this

$ deploy appname /path/to/tomcat [classpath1 classpath2 ...]

This means that our script file must be called deploy. First argument must stand for the appname.war file to be deployed to a tomcat server having the second argument as the path to the server's $CATALINA_HOME directory. From the third argument on we can supply all the class paths to be added to $CLASSPATH environment variable during the build of an application. 

Let's start by creating the deploy file. and make sure that it is executable.

$ touch ~/bin/deploy
$ chmod 755 ~/bin/deploy

open the file in your favorite text editor (mine is sublime)

$ subl ~/bin/deploy

and copy the script bellow. I left description for each separate logical block as a comment within the script. I also assume that the deploy command should be run inside the directory containing the src/ folder. Since the web content directory name varies from application to application I take it as the parent directory of the WEB_INF folder. I called it $webapp in the script.

#!/bin/bash

APP_NAME=$1
TOMCAT_DIR=$2

# Finding web content directory
WEB_INF=$(find -type d -name 'WEB-INF')
webapp=$(dirname "$WEB_INF")

# exporting CLASSPATH
export CLASSPATH=$3
aray_arg=${@:4}
for arg in $aray_arg; do
  export CLASSPATH=$CLASSPATH:$arg
done

# Find all the .java files inside src/ directory
# and list them in source.txt file 
find src -name *.java > source.txt

# Create the classes/ directory where the .java files should be compiled
mkdir $webapp/WEB-INF/classes

# Compile all the .java files listed inside source.txt
javac -d $webapp/WEB-INF/classes @source.txt
# Remove source.txt after the compilation
rm source.txt

# Create the deployment .war file inside the web content directory
cd $webapp
jar -cvf $APP_NAME.war *
# Remove the classes/ folder after the .war file is created
rm -r WEB-INF/classes/

# Deploy the .war file to tomcat and restart the server
mv $APP_NAME.war $TOMCAT_DIR/webapps/$APP_NAME.war
$TOMCAT_DIR/bin/shutdown.sh
sleep 1
$TOMCAT_DIR/bin/startup.sh
sleep 1

# Open the application in the default browser
xdg-open http://localhost:8080/$APP_NAME

Save and close the deploy file.

Now if we run the deploy command from the terminal within the directory containing the src/ folder of the web project, the app will be built, deployed and viewed at http://localhost:8080/appname.