Writing a Liferay Deployment Script Part 2

Created: 16 September 2016  Modified:

In the previous article we listed requirements that our deployment script needs to meet. The first of these is for it to be possible for the script to run from the command line. This is simple enough as Apache Ant is built for the command line. As I worked on the script it became clear that we would need to pass parameters to Ant to modify its behaviour based on environment. I didn’t want those deploying the application to have to figure out to use Ant. Like any good developer I cheated and decided to use both Bash and Ant.

Before we go any further I want to briefly describe Bash and Apache Ant. Bash is a unix shell and you can write scripts that will automate tasks you might normally run from the command line. Apache Ant is a build tool used by Java developers to compile, test and package applications. Our bash script is straightforward in that it will only contain commands that can be typed on the command line. We will be using Ant extensively and we should explore some of the basics. Below is an example Ant build file.

build.xml

<project name="Example" default="mainTask" basedir=".">
  <description>
	Example Ant Project
  </description>

  <target name="mainTask" depends="otherTask">
  </target>

  <target name="otherTask" if="${itsTuesday}">
  </target>


</project>

You place your build.xml file in a directory, change to the directory, run “ant” and ant will read the build.xml file and attempt to execute the default target. Int the example we have empty targets that do nothing just to display the basic structure of a build file. In this case the default target is “mainTask”. By default Ant will attempt to run this target first. However mainTask depends on “otherTask”. This means that before mainTask can be run, otherTask must be run first. However otherTask will only be run if the ${itsTuesday} property evaluates to true. Using these dependencies and conditions complicated processes can be written. With the basics out of the way let us move onto our Bash script.

deploy.sh

!/bin/sh -e

tar -pxzf apache-ant-1.9.7-bin.tar.gz

export ANT_HOME=/home/chris/apache-ant-1.9.7


echo "Ant extracted and ANT_HOME set to the following."
echo $ANT_HOME

echo "************"
echo "Begin Deployment"

/home/chris/apache-ant-1.9.7/bin/ant -Dservertype=###SERVER TYPE### deploy

rm -rf /home/chris/apache-ant-1.9.7
rm -f *.xml

This script extracts Ant from a zipped tar file. Then sets the ANT_HOME environmental variable. It then runs Ant targeting the “deploy” target and setting the “servertype” variable. Finally the script deletes Ant from the system and removes build files.

The next requirement was that the script should provide feedback letting the user know what is going on. This is easily provided by the Echo task.

echo task

<echo message="Show the user this text."/>

The third requirement was to provide files already configured for the target system. I satisfied this requirement by passing variables to Ant as shown in the Bash script above. Each target system gets a deploy.sh script built specifically for it. The result is I have one Bash script for each system but only one set of Ant build files.

The fourth requirement was to be able to extract packaged files from zipped tar files. At first this seemed quite easy as Ant provides the Tar and Untar tasks. Ant maintaining system compatibility has limited the capabilities of those tasks making them inadequate for our purposes. Instead we will use the exec task. The Exec task allows you to run operating system commands from Ant. Below you can see an examples of running tar using this task.

exec task running tar

<exec executable="tar" failonerror="true">
  <arg value="-pxzf"/>
  <arg value="liferay_deploy.tar.gz"/>
</exec>

<exec executable="tar" failonerror="true">
  <arg value="-czf"/>
  <arg value="liferay_deploy.tar.gz"/>
  <arg value="liferay"/>
</exec>    

We will continue working with our Ant deployment script in part 3 of this series.

tags: liferay - 6.2 - java - deploy - command line
   Less Is More