Openshift! Create free account and deploy your Java app in less than 5 minutes

On a previous blog entry here, I analyzed the steps to create a basic Java RESTful app in less than 5 minutes. Most probably after creating a RESTful app you will need to put it on cloud in order to be accessible for clients to use. In case that your app is not going to have too much load it is ok to use one of the free PaaS services that exists out there.

There are quite a few of them helping developers to bootstrap their projects.
Openshift (https://www.openshift.com/), Google Cloud Platform (https://cloud.google.com/appengine/docs) and Cloud Foundry are some of them.
The scope of this tutorial is to analyze the steps creating account and deploy your Java project in Openshift.

To begin with it, you should navigate to https://www.openshift.com/ and click on Signup for free. After filling in your info you will receive an email to validate your mail account. Click on the link to validate login to Openshift.

The first thing it will ask you to do is to add an application and this is done in 3 steps.

Step1. Choose a type of application.
Select Do it Yourself.

Step2. Configure the application.
On field “Public URL” you will need to provide the application name and a domain name. The app name and the domain name will be on the final URL of your app so if you give”myapp” as app name and “mydomain” as domain name then you app’s url will be http://myapp-mydomain.rhcloud.com/ .
On field  “Source Code” you should provide your Github’s URL of your project see here for more info. Hit create application.

Step3. Next Steps.
Click on “Continue to the application overview page.”

Now that your application is created you will need to create an RSA key to connect with SSH. Click on settings and add your RSA key. If you don’t have any you can read this guide  that will help you create one.

Next, click on Applications and select your app. Click on “Want to log in to your application?”.
This will expand some text that have a code to connect with SHH. Copy the code and use it to connect (in case that you have Windows you can use putty).

Assuming that you have managed so far to login you should run the following command to navigate to you data folder:
cd $OPENSHIFT_DATA_DIR

Then run the following command:
echo -e  “<settings>n   <localRepository>$OPENSHIFT_DATA_DIR</localRepository>n</settings>n” > settings.xml 
This commands will setup maven local repository on a folder that is actually writable.

Then run the following to change folder to your repository:
 cd $OPENSHIFT_REPO_DIR

and to build your project:
mvn install -s $OPENSHIFT_DATA_DIR/settings.xml 

Finally you  run the following to actually run the project:
java -jar target/*.jar –server.port=${OPENSHIFT_DIY_PORT} –server.address=${OPENSHIFT_DIY_IP}

That’s it you are done. You can now navigate to http://myapp-mydomain.rhcloud.com/  and see the results.

Leave a Reply

Your email address will not be published. Required fields are marked *