Configuring Environment Variables in a play application

Posted By : Gursahib Singh | 29-Jun-2018

Many times it is required to keep the value of variables different for a different environment like the database connection URLs, SSL configurations, the HTTP port and so on, for which configuration of the environment is required. Most of the configurations of the play framework project are done in the application.conf file which can be found in the "conf/application.conf" path in the project. By default, there is only one application.conf file available per project. This application.conf file is loaded at the runtime from the classpath.

 

The runtime configurations can be overridden in several ways. One of the ways of introducing different environment variables is by using the alternative ".conf" files. In general, the application.conf file is kept common for all the common configurations and is inherited in the other ".conf" files where the required configurations are either overridden or added.

 

Let us suppose, we are required to set different configuration for the different runtime environment like development, staging, and production. To do so, we can write the various .conf files as follows.

For development, the name of the file is "development.conf" including the following variables.

      host = http://localhost:8080
      username = abc
      password =  1234
        

For staging, the name of the file is "staging.conf" which includes the following variables.

      host = 10.11.11.11:8080
      username = abc
      password = 534gsu7
      
        

Similarly, for production, the name of the file is "production.conf" which includes the following variables.

      host = 10.11.11.11:8080
      username = xyz
      password = 773bhbgss        
        

The configurations of the application.conf file can be inherited in the customized .conf files using the following statement in our .conf files:
include "application.conf"

To load differently.conf file at runtime, we can use the following command:

play run -Dconfig.file=/opt/conf/name-of-.conf-file
ex- play run -Dconfig.file=/opt/conf/production.conf

This will load that particular .conf file at the runtime. The use of the alternate .conf files is one of the ways of configuring the environment in a play application, which ensures us dynamic configuration settings for each of the environment.

About Author

Author Image
Gursahib Singh

Gursahib is a software developer having key skills in J2SE and J2EE. His hobbies are playing chess, reading and learning new softwares.

Request for Proposal

Name is required

Comment is required

Sending message..