Deploy a Java 8 Spring Boot application on a DIY Openshift cartridge
09 Dec 2015
As at the time of this writing Openshift DIY cartridge comes with OpenJDK Java 7 installed. During the migration of JSONDoc from AppFog to Openshift, I wanted to use Oracle Java 8 instead, so here are the steps I took to do that.
Have a Spring Boot application
I just created a very simple Spring Boot project with name bootjava8 in my workspace and added a rest controller in it.
Create a DIY app on OpenShift
Precondition for this is that you have the rhc client installed. It's very easy to manage your applications with that. So, once you are ready, open a terminal, move to the /tmp directory and create an DIY application:
You should see something like this on your shell:
Now move to the the Spring application directory and add the remote OpenShift repository in this way:
OpenShift will update the repository:
Now you can merge from the OpenShift repository like this:
And as result in the project directory, there should be an .openshift directory, that is important, and some other directories (diy, misc) that you can delete or put into .gitignore.
Create OpenShift action hooks
Next step is to create three action hooks, that OpenShift will execute every time something is pushed to its git repository. I'll call them in this way:
build: to install Oracle Java 8 and Maven
start: to start the application
stop: to stop the application
Those action hooks will be created in the .openshift/action_hooks directory.
The build action hook
Copy the start action hook. Call it build.
Edit the build action hook and put there the instructions to install Maven and Oracle Java 8:
With these Maven and Oracle Java 8 will be installed in the $OPENSHIFT_DATA_DIR directory. The installation will run only the first time the project is pushed to the OpenShift git repository.
The start action hook
Next, edit the start action hook. This will set some environment variables and start the executable Spring Boot jar.
Create a file named settings.xml in the .openshift directory and put this content into it:
This will tell OpenShift to use the $OPENSHIFT_DATA_DIR as Maven repository.
The stop action hook
Last action hook is the stop one. It contains instructions on how to stop the java process.
Push to the OpenShift remote
This is it. Whenever you push something on the openshift remote, the action hooks will be executed and the application will be stopped, built and started. Just create a .gitignore file. Mine contains:
And then add, commit and push to the openshift remote:
The application will build, run its tests and start. It is possible to tail the logs by doing:
That will output something like:
At the end the application will be available on http://bootjava8-fabiomaffioletti.rhcloud.com/