Run Gradle Builds on OpenShift

Gradle builds on OpenShift
OpenShift has supported Apache Maven as default build system for Java based projects since the first release. All the Java projects created by OpenShift are maven based. A few months ago we also added support for Apache Ant. All OpenShift gears now have Apache Ant installed on them. So, using Apache Ant to build your project is as easy as updating the OpenShift build action hook. You can refer to my blog post to learn how you can use Ant to build your OpenShift projects.

Lately I have seen lot of developers asking how they can use Gradle to build their projects. Gradle combines the power and flexibility of Ant with the dependency management and conventions of Maven into a more effective way to build. A lot of open source projects and enterprises are using Gradle as their build system. The Spring Framework is one popular example of an open source project using Gradle. In this blog post, we will learn how we can configure an OpenShift Java project to use Gradle instead of Apache Maven to build the project.


Before we can start building the application, we’ll have to do few setup tasks :

  1. Sign up for an OpenShift Account. 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.

  2. Install the rhc client tool on your machine. The rhc is a ruby gem so you need to have ruby 1.8.7 or above on your machine. To install rhc, just typesudo gem install rhc. If you already have one, make sure it is the latest one. To update your rhc, execute the command shown below.sudo gem update rhc. For additional assistance setting up the rhc command-line tool, see the following page:

  3. Setup your OpenShift account using rhc setup command. This command will help you create a namespace and upload your ssh keys to OpenShift server.

Step 1 : Create OpenShift 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 gradledemo 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://gradledemo-{domain-name} Please replace {domain-name} with your account domain name.

Step 2 : Delete Template Application Source code

OpenShift creates a template Maven based Java project when you ran the command in step 1.

$ git rm -rf src/
$ git rm -f pom.xml
$ git commit -am "removed template application source code"

Step 3 : Pull Source code from github

Next we will pull the source code from my github repository. It is a simple spring mvc application using Gradle for build.

$ git remote add upstream -m master
$ git pull -s recursive -X theirs upstream master

The project has one SampleController which has a request mapping to “home”. If a user visits http://localhost:8080/home then user will view “Hello, Gradle User!” in the browser. If you have gradle installed on your local machine, then run gradle jettyRunWar and go to http://localhost:8080/home. Y

Gradle Demo App

Look at build.gradle

For users who are new to Gradle, build.gradle is the place where you define your build script. The build.gradle for this project is shown below:

1. apply plugin: 'war'
2. apply plugin: 'jetty'
3. apply plugin: 'eclipse-wtp'
4. repositories {
5. dependencies {
   providedCompile 'javax.servlet:servlet-api:2.5'
   compile 'org.springframework:spring-webmvc:3.2.2.RELEASE'
   runtime 'javax.servlet:jstl:1.1.2'
/* Change context path (base url). otherwise defaults to name of project */
6. jettyRunWar.contextPath = '' 

The build.gradle is very easy to read and self explanatory.

  1. The first three lines of the build.gradle specify the plugins that will be used in this project. We will be using war plugin to assemble a war file, jetty plugin to run the jetty container on local machine, and eclipse-wtp plugin to generate eclipse files.

  2. Next at line 4 we are specifying that our project will be using Maven central to download all the dependencies.

  3. The next section at line 5 specifies the dependencies required to build this project.

  4. Finally at line 6 we specify that the application will be accessible at root context path. You can change this to “sampleapp” and then home page will be accessible at http://localhost:8080/sampleapp/home.

Step 4 : Update build.gradle

We need to make a couple of changes to build.gradle to make things work on OpenShift. The first change is that the war file should be named “ROOT.war” so that the application is available at the root URL. The next change is to make sure that war file is archived to in the webapps folder. This is done by customizing the war plugin as shown below:

 archiveName = 'ROOT.war'
 destinationDir = file('webapps')

You can view the full build.script here.

Step 5 : Create OpenShift Action Hooks

Finally we need to tell our OpenShift gear to install Gradle and then use Gradle to build our project. This is done by creating two action hooks — pre_build and build. The pre_build action hook is called before the build step. It is ideal for installing gradle. The build action hook is called before the deploy script and we will use it to build the project.

Create pre_build action hook

Create a file called pre_build under .openshift/action_hooks folder.

$ touch .openshift/action_hooks/pre_build

Update the pre_build with the following content. The commands shown below will download the gradle binary zip file, unzip it and remove the zip file. This will be done only first time as there is no point doing this step again.

set -x
if [ ! -d $OPENSHIFT_DATA_DIR/gradle-1.6 ]
    mkdir gradle
    rm -f

Create build action hook

Next we will create the build action hook under .openshift/action_hooks folder.

$ touch .openshift/action_hooks/build

Update the contents of build action hook with content shown below. The commands shown below creates couple of environment variables required to tell Gradle where user home and gradle home are. In OpenShift, write permissions are only in $OPENSHIFT_DATA_DIR. Finally we execute the gradle build under $OPENSHIFT_REPO_DIR

set -x
gradle build

The action hooks that we created are not executable by default so our OpenShift gear will not be able to execute them. To make them executable, run the command shown below.

$ chmod +x .openshift/action_hooks/pre_build
$ chmod +x .openshift/action_hooks/build

Step 6 : Commit Changes and Push

Finally we have to commit the changes to git and then push the changes to OpenShift gear.

$ git add .
$ git commit -am "added action hooks"
$ git push

You can view the application running at http://gradledemo-{domain-name} Replace {domain-name} with your account domain name.

Step 7 : Working with Jenkins

Using Jenkins to build the project has many benefits as mentioned in one of my earlier blog on Jenkins. One major benefit of using Jenkins is that the build takes place in it’s own Gear. Without Jenkins, application builds occur in the same Gear where the application runs which may cause gear to run out of available resources like memory.

To use Jenkins, first create a Jenkins application. This command on success will return Jenkins admin username and password. Please make a note of them.

$ rhc app create jenkins jenkins

After creating Jenkins application, add Jenkins client cartridge to gradledemo application.

$ rhc cartridge add jenkins-client --app gradledemo

Login to the Jenkins instance using the username/password mentioned OpenShift gave you when you created Jenkins application. Just go the gradledemo build and click on “Build Now” as shown below.

Gradle demo builds on Jenkins

Now the action hooks will be invoked on the Jenkins builder so everything will work as expected.


In this blog, we looked at how we can use Gradle to build OpenShift Java projects. OpenShift’s flexibility makes it very easy to try out your favorite tools. So, if you are a Gradle user , try out OpenShift and deploy your application on it.

What’s Next?

Java, OpenShift Online
Comments are closed.