Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

dotenv for Java and the JVM

DZone's Guide to

dotenv for Java and the JVM

Wish configuring your microservices was easy? Well, while it started with Ruby, dotenv has a Java port that brings configuration into a single method.

· Java Zone ·
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

When working within the context of a modern microservice architecture, one often encounters services written in a variety of languages using a variety of frameworks. There are numerous benefits to microservice architectures, however, configuring each service can be quite complex. Each framework may offer their own configuration method, e.g. property files, YAML, JSON, etc. As the number of microservices piles up, this quickly becomes a DevOps nightmare.

To avoid this problem, we need a single method for configuration. Fortunately, there is one, dotenv, and most languages already offer a port. dotenv is a library originating from the Ruby community. It offers a simple, consistent, 12-factor compliant method to configure an application using environment variables.

The dotenv Ruby project describes it as such:

Storing configuration in the environment is one of the tenets of a twelve-factor app. Anything that is likely to change between deployment environments–such as resource handles for databases or credentials for external services–should be extracted from the code into environment variables.
But it is not always practical to set environment variables on development machines or continuous integration servers where multiple projects are run. dotenv loads variables from an   .env file into ENV when the environment is bootstrapped.

Ultimately, by choosing dotenv, we can ensure the configuration of all of our microservices is handled in exactly the same way! This includes those running in our development, test, staging, and production environments. Just provide a new `.env` file (or simply rely on the host’s existing environment variables).

As noted, most languages now offer such a port. Here are some:

This post focuses on Java and JVM languages, so let’s see java-dotenv in action. (Note: java-dotenv also offers a Kotlin DSL).

Here goes:

Create a .env file in the root of your project (environment variables defined in the host environment override those in .env):

 # formatted as key=value
 MY_ENV_VAR1=some_value
 MY_EVV_VAR2=some_value


Using Java, initialize dotenv with its defaults and fetch an env var:

import io.github.cdimascio.dotenv.Dotenv;
Dotenv dotenv = Dotenv.load();
dotenv.get(“MY_ENV_VAR1”);


Or, using Kotlin, initialize dotenv with its defaults and fetch an env var:

import io.github.cdimascio.dotenv.dotenv
val dotenv = dotenv()
dotenv[“MY_ENV_VAR1”]


To customize the dotenv configuration with Java, specify some options:

Dotenv dotenv = Dotenv.configure()
  .directory("./some/path")
  .ignoreIfMalformed()
  .ignoreIfMissing()
  .load();


Or with Kotlin, use the DSL to configure dotenv’s options:

val dotenv = dotenv {
  directory = "./some/path"
  ignoreIfMalformed = true
  ignoreIfMissing = true
}


For more usage details, visit https://github.com/cdimascio/java-dotenv

Installation

Maven:

<dependency>
    <groupId>io.github.cdimascio</groupId>
    <artifactId>java-dotenv</artifactId>
    <version>3.0.0</version>
</dependency>


Gradle:

compile 'io.github.cdimascio:java-dotenv:3.0.0'


For more information, API docs, FAQ, and more, visit https://github.com/cdimascio/java-dotenv.

Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat

Topics:
java ,kotlin ,environment variables ,dotenv ,microservice architecture ,tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}