DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Java Virtual Threads and Scaling
  • Java’s Next Act: Native Speed for a Cloud-Native World
  • The Energy Efficiency of JVMs and the Role of GraalVM
  • Understanding Root Causes of Out of Memory (OOM) Issues in Java Containers

Trending

  • Intro to RAG: Foundations of Retrieval Augmented Generation, Part 2
  • Using Java Stream Gatherers To Improve Stateful Operations
  • Implementing API Design First in .NET for Efficient Development, Testing, and CI/CD
  • Agile’s Quarter-Century Crisis
  1. DZone
  2. Coding
  3. Java
  4. dotenv for Java and the JVM

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.

By 
Carmine DiMascio user avatar
Carmine DiMascio
·
Jan. 03, 18 · Tutorial
Likes (14)
Comment
Save
Tweet
Share
44.3K Views

Join the DZone community and get the full member experience.

Join For Free

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:

  • Node.js https://github.com/motdotla/dotenv
  • Go https://github.com/joho/godotenv
  • Python https://github.com/theskumar/python-dotenv
  • Ruby (the trailblazer) https://github.com/bkeepers/dotenv
  • Java https://github.com/cdimascio/java-dotenv

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.

Java (programming language) Java virtual machine

Opinions expressed by DZone contributors are their own.

Related

  • Java Virtual Threads and Scaling
  • Java’s Next Act: Native Speed for a Cloud-Native World
  • The Energy Efficiency of JVMs and the Role of GraalVM
  • Understanding Root Causes of Out of Memory (OOM) Issues in Java Containers

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!