Deploying a properties file next to your jar on OpenShift

Deploying a properties file next to your jar on OpenShift

I’ve recently had the situation where I deployed a Spring Boot application on OpenShift where a certain dependency needed a properties file that couldn’t be found. The problem was that this dependency didn’t scan the classpath for the file, but just opened a FileInputStream relative to the current path.

In this blogpost I will guide you through the process of deploying a text file next to a jar in an OpenShift container. I’ve reproduced an
example scenario, but you can skip these steps and go right to the solution if you want to.

Example Scenario

To reproduce the problem just described we need a jar with some Java code reading a file located next to the jar.

The code:

The code just opens a FileInputStream for which the location is taken from the environment property PROPS_LOCATION.
The contents of the file are then printed to the OutputStream.

 

The input file

I’ve created a properties.example file containing: welcome bob

If you want to run the code locally you’ll need to set the PROPS_LOCATION env var:

 

 

 

Creating a jar

First off, we need to compile the code:

 

then add a MANIFEST.MF with the main class to run, in the same output dir:

and then to create the jar:

Place the jar in a specific dir, we’ll need it when creating the image on OpenShift.

 

Creating and deploying an image from the jar

To create and deploy an image we need to specify the objects we need to OpenShift.
We need a BuildConfig, ImageStream and a DeploymentConfig.

I’ve specified the needed objects in yaml format in an OpenShift template:

 

In the container spec I’ve added a shell command to execute to keep the container alive.
It’s a simple infinite loop so that the container doesn’t become idle and won’t get killed by openshift.

 

Starting the Build

To upload the locally created jar and start the just configured build run this command:

 

Creating and mounting the input file on OpenShift

To create and mount an input file we need to add a ConfigMap to our template:

 

 

And change our DeploymentConfig to add a volume with our ConfigMap, and mount this volume to a specific path

 

All we need now is to add an environment variable to our deployment, pointing to the mounted file from our ConfigMap

 

Verifying the deployment

If all went well your deployment should look like this

deployment

 

Running the jar

In the OpenShift console go to your deployment and click on the created pod and enter the terminal,
here you’ll find the jar and mounted input file in the /deployments dir.

Here is the output result:

 

Good luck and have fun with your properties files on OpenShift!