Building and Deploying Java Source Code with OpenShift Express

One of the cool features of deploying a Java project on OpenShift Express is that you can deploy a web application without having to build it locally and then adding the war file to the Express Git repository.  The primary benefit of doing this is that it conserves application disk space over subsequent deployments.  Because a war file is binary, each time you commit a new version to the Git repository, the incremental disk space used to support the new version is the same as the size of the war file.  So if my war file is 5mb, each time I commit a new version, the size of my Express application increases by 5mb (plus or minus depending on what has been added/removed from my app).

With source deployments, if my source code is 100k (remember, source is text), and my pom.xml (Maven build file) is setup properly, my project will be built server side and deployed when JBoss starts.  Deployments that are built on the server are not committed to the Git repository so each time I edit source and redeploy, I’m only increasing the size of my repository by the size of the diff of my code.  As you can probably imagine, this will help prevent my application from quickly reaching the quota of Express applications.

The steps for configuring a maven project to build server side are quite simple.  Using the ‘kitchensink’ sample app that is available from the JBoss Application Server 7 downloads page, I’ll walk through making the required changes to support server side building.

Step 1
In order to deploy an OpenShift application, one must first be created:

rhc-create-app -a kitchensink -t jbossas-7.0

Step 2
We’ll remove the stock application so we can copy over our ‘kitchensink’ app:

cd kitchensink<br />git rm -rf pom.xml src

Step 3
You can download the ‘kitchensink’ application from the ‘Quick Starts’ from http://www.jboss.org/jbossas/downloads/ and extract it somewhere on your computer.  The path might be something like /home/[username]/Downloads/jboss-as-quickstarts-7.0.0.Final/kitchensink (we’ll call it SOURCE below):

cp -R SOURCE/kitchensink/pom.xml .<br />cp -R SOURCE/kitchensink/src .

Step 4
Before committing/pushing this application, we have to add a Maven profile called ‘openshift’ so that the project gets built properly.  Maven support passing in the profile on the command line and this is what OpenShift Express does when it detects a pom.xml file in your Git repository.  We’ll edit ‘pom.xml’ and add the following to the end just before </profiles>:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;profile&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;!-- When built in OpenShift the &#39;openshift&#39; profile will be used when<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; invoking mvn. --&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;!-- Use this profile for any OpenShift specific customization your app<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; will need. --&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;!-- By default that is to put the resulting archive into the &#39;deployments&#39;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; folder. --&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;!-- <a href="http://maven.apache.org/guides/mini/guide-building-for-different-environments.html" title="http://maven.apache.org/guides/mini/guide-building-for-different-environments.html">http://maven.apache.org/guides/mini/guide-building-for-different-environ...</a> --&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;id&gt;openshift&lt;/id&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;build&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;finalName&gt;tweetstream&lt;/finalName&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;plugins&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;plugin&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;artifactId&gt;maven-war-plugin&lt;/artifactId&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;version&gt;2.1.1&lt;/version&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;configuration&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;outputDirectory&gt;deployments&lt;/outputDirectory&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;warName&gt;ROOT&lt;/warName&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/configuration&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/plugin&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/plugins&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/build&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/profile&gt;

 

Step 5
Now that OpenShift will be able to run the ‘openshift’ profile at build time, we’re ready to add, commit, and push the code.

git add &ndash;A<br />git commit -a -m &quot;first deployment of Kitchensink&quot;<br />git push

What will happen at this step is your code will be pushed up to the OpenShift Express cloud,  OpenShift will detect your pom.xml, a Maven build will be executed (mvn clean package -Popenshift -DskipTests), and assuming that the build is successful JBoss AS 7 will be started and your server side built war file deployed.

If you use the Kitchensink example, you should see the following when you browse to http://kitchensink-[domain].rhlcoud.com

 

Tags:

if you create and exploded war file (in fact a war directory) git is smart enough ro only upload deltas of changed files...

I tested it with a rhc-snapshot and the .git folder doesn't grow with each push

The problem I've found is the tmp folder, I have a

/jbossas-7.0/standalone/tmp

folder which seems to be versioning every deploy I make...

do you know hoe to clean up the tmp folder?

thanks a lot

rhc app tidy -a {appName}
That will clean out /tmp (among other things)

More from this author