Running Ant Builds on OpenShift

OpenShift supports Apache Ant

OpenShift has long supported Apache Maven as the default build tool for Java applications. But a lot of Java developers have been asking us to support Apache Ant as an alternative build system. We have listened to the developers, and you can now use Apache Ant for automating software build processes. The Apache Ant is available for Java applications as well as all other application runtimes. All application gears have support for the Apache Ant build system installed. In this blog, I will show you how you can use Ant as a build tool with an OpenShift application.

Step 1: Sign up for an OpenShift Account

If you don’t already have an OpenShift account, head on over to the website and signup. It is completely free and Red Hat gives every user three free Gears on which to run your applications. At the time of this writing, the combined resources allocated for each user is 1.5 GB of memory and 3 GB of disk space.

Step 2: Install the client tools on your machine

The OpenShift client tools are written in a very popular programming language called Ruby. With OSX 10.6 or later and most Linux distributions, ruby is installed by default so installing the client tools is a snap. Simply issue the following command on your terminal application:

sudo gem install rhc

Step 3 : Setting up OpenShift

The rhc client tool makes it very easy to setup your OpenShift instance with ssh keys, git and your applications namespace. The namespace is a unique name per user which becomes part of your application URL. For example, if your namespace is t20 and application name is antdemo then the url of the application will be https://antdemo-t20.rhcloud.com/. The command is shown below:

rhc setup -l openshift_login

Step 4 : Creating Tomcat 7 application

After setting up your OpenShift account, the next step is to create an application. To create an application, type the following command:

rhc app create antdemo tomcat-7

The command shown above will create an application container for us, called a gear, and setup all of the required SELinux policies and cgroup configuration. Next, it will install all the required software on your gear. OpenShift will also setup a private git repository with some template code, and then clone the repository to your local system. Finally, OpenShift will propagate the DNS to the outside world.

The application will be accessible at https://antdemo-t20.rhcloud.com/. Please replace t20 with your account domain name.

The templated Java application created by OpenShift is still Maven based and we will be working to support Ant based templated applications. So in the future it will be possible at application creation time to specify whether you want Ant based or Maven based Java application. Also, we might provide support for dependency manager like Apache Ivy so that you don't have to manually package all your libraries within the application. Later in the post I will show you how you can tell OpenShift to invoke your Ant build.

Step 5 : Verifying Ant installation on application gear

With the current release, all OpenShift applications will have Apache Ant installed on their application gear. To verify that Ant is installed on the application gear, we will ssh into the application gear as shown below:

$ cd antdemo
$ rhc app ssh -a antdemo

Once you have ssh'ed into the application gear, you can check Ant is installed by running the command shown below:

[antdemo-t20.rhcloud.com xxxx]\> ant -version
Apache Ant version 1.7.1 compiled on February 3 2011

The output shown above confirms that Ant version 1.7.1 is installed on the application gear.

Step 6 : Pulling sample Ant application code from github

I have created a very simple Spring MVC application which uses Ant as the build automation tool. To get the source code run the following commands on your local machine.

# Remove the template source code
 
$ git rm -rf src/ pom.xml
$ git commit -am "removed default files"
 
# pull the source from github remote
$ git remote add upstream -m master git://github.com/shekhargulati/springmvc-ant-quickstart.git
$ git pull -s recursive -X theirs upstream master

Look at the code

The application is a Hello World Spring MVC application. Let us take a look the directory structure of the code pulled from github.

  • src : The src folder contains Java code related to the application. In this application it is a Spring MVC controller.
  • war : The *war *folder contains jsp, and web.xml. You can place jar files in the lib folder under WEB-INF folder.
  • build.xml : The build.xml is ant build descriptor file. The build.xml has a target called "package" which first compiles the source code, then creates a war file with name ROOT, and finally copies the ROOT.war to webapps folder. So, in your build.xml file make sure you have a target which does the same.
<target name="package" depends="build" description="Package a war file">
    <war destfile="webapps/ROOT.war" webxml="${web.dir}/WEB-INF/web.xml">
        <fileset dir="${web.dir}">
            <include name="**/*.*"/>
        </fileset>
    </war>
</target>

Step 7 : Updating the build hook

Next we will update the action hook to execute the build.xml package target. In the snippet shown below, we are first changing directories to $OPENSHIFT_REPO_DIR, this is the directory where your application source code resides, and finally we will execute ant package command.

set -X
cd $OPENSHIFT_REPO_DIR
ant package

Step 8 : Commit and push the changes

Next we will commit the code changes and push the code to OpenShift server.

$ git commit -am "updated build action hook"
$ git push

You will see output like as shown below. The output will clearly show that ant package command was ran which created a WAR file.

git push
Counting objects: 5, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 379 bytes, done.
Total 3 (delta 2), reused 0 (delta 0)
remote: restart_on_add=false
remote: Sending SIGTERM to jboss:19632 ...
remote: kill -9 20180
remote: kill -9 19632
remote: Done
remote: restart_on_add=false
remote: Running .openshift/action_hooks/pre_build
remote: Running .openshift/action_hooks/build
remote: + cd /var/lib/openshift/514ee0534382ec5991000091/app-root/runtime/repo/
remote: + ant package
remote: Buildfile: build.xml
remote: 
remote: build:
remote: 
remote: package:
remote:       [war] Building war: /var/lib/openshift/514ee0534382ec5991000091/app-root/runtime/repo/webapps/ROOT.war
remote: 
remote: BUILD SUCCESSFUL
remote: Total time: 7 seconds
remote: Running .openshift/action_hooks/deploy
remote: hot_deploy_added=false
remote: Found 127.7.151.1:8080 listening port
remote: Done
remote: Running .openshift/action_hooks/post_deploy
To ssh://514ee0534382ec5991000091@antdemo-t20.rhcloud.com/~/git/antdemo.git/

Finally you can view the application at https://antdemo-t20.rhcloud.com/ and will see a hello message with the current date and timestamp as shown below.

Hello World Spring Application

Thats it for this blog. In the next blog, I will show you how you can Ant Ivy with OpenShift.

What's Next?