jhipster – Reading configuration properties from environment variables

abril 30, 2015

Heroku uses environment variables to store and read application configuration, including sensitive information (i.e. passwords). It also has the benefit of being able to run the application on different contexts, development and production, without changing config files or even source code.
Jhipster uses configuration files, application-dev.yml and application-prod.yml, to store datasource configuration -passwords included- This files are subject to source control, this is not acceptable.
I was trying to figure out how to read properties from environment variables in a jhipster generated application, and it happens that it already does since it uses Spring Boot which provides a feature to read external configurations with plenty of options -included environment variables-

To configure the datasource I created an .env file (heroku convention) which exports the environment variables

export SPRING_DATASOURCE_URL=jdbc:postgresql://localhost/devdb
export SPRING_DATASOURCE_USERNAME=scott
export SPRING_DATASOURCE_PASSWORD=secret

notice how each variables is mapped to its counterpart in the yml file

spring:
    datasource:
        url:
        username:
        password:

finally you only need to /source the .env file prior to running mvn spring-boot:run with the command source .env or . .env

sources:
https://github.com/jhipster/generator-jhipster/issues/846
http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html
http://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html

Anuncios

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s

A %d blogueros les gusta esto: