Using dotenv files with Spring Boot
Asked Answered
F

3

20

I'd like to use dotenv files to configure my Spring Boot application.

What is the best way to do this?

In Ruby or Node world, I just creating .env file and it loads all stuff from there to application environment.

I don't like to create separate profiles for my app etc. I just want to load any environment variables I specified in file into my app.

Factfinding answered 24/10, 2019 at 21:29 Comment(0)
P
17

I have built a proper integration between Spring and dotenv.

Latest releases:

Follow this thread to understand the motivation. And then review the library:

Check out the spring-dotenv library here:
https://github.com/paulschwarz/spring-dotenv

The library includes a sample application to show you how to use it, and there you see that the integration with Spring is very natural:

https://github.com/paulschwarz/spring-dotenv/tree/master/application/src/main/resources

I stuck to two principles in designing this library:

  1. https://12factor.net/config
  2. Allow your code to be completely unaware of dotenv so that you continue to reference your application.yml/application.properties files using the normal Spring techniques. No funny business.
Phelia answered 5/3, 2020 at 20:45 Comment(2)
Today I published 2.5.3 which addresses the vulnerability found recently in Spring. Please update to the latest. github.com/paulschwarz/spring-dotenv/releasesPhelia
Latest releases: 3.0.0 for Java 8 4.0.0 for Java 11 github.com/paulschwarz/spring-dotenv/releasesPhelia
L
25

In spring boot just do that in application.yml

---
spring:
    config:
      import: optional:file:.env[.properties]

username: ${USERNAME}

or if you use application.properties

spring.config.import=optional:file:.env[.properties]
username=${USERNAME}

Then @value and all other stuff will work

Lollipop answered 26/12, 2021 at 3:11 Comment(2)
spring.config.import=file:/my/path/to/passwords.env[.properties] Here passwords.env does not have to be on the classpath and is required (no optional: modifier before file:)Homologue
Where do you place your .env file with this solution? I'm new to all this, but I would guess you can put it anywhere and then you have to add that location to your classpath or something. Is there any way of doing this though that you people would recommend? Assuming you made your project using Spring Initializr maybe.Hershberger
P
17

I have built a proper integration between Spring and dotenv.

Latest releases:

Follow this thread to understand the motivation. And then review the library:

Check out the spring-dotenv library here:
https://github.com/paulschwarz/spring-dotenv

The library includes a sample application to show you how to use it, and there you see that the integration with Spring is very natural:

https://github.com/paulschwarz/spring-dotenv/tree/master/application/src/main/resources

I stuck to two principles in designing this library:

  1. https://12factor.net/config
  2. Allow your code to be completely unaware of dotenv so that you continue to reference your application.yml/application.properties files using the normal Spring techniques. No funny business.
Phelia answered 5/3, 2020 at 20:45 Comment(2)
Today I published 2.5.3 which addresses the vulnerability found recently in Spring. Please update to the latest. github.com/paulschwarz/spring-dotenv/releasesPhelia
Latest releases: 3.0.0 for Java 8 4.0.0 for Java 11 github.com/paulschwarz/spring-dotenv/releasesPhelia
K
3

There's actually a java port of 'dotenv' tool.

https://github.com/cdimascio/dotenv-java

Kakalina answered 25/10, 2019 at 10:51 Comment(2)
Well it doesn't does the same job as dotenv guys in ruby and node world. First, it can't inject env variables into the process, the reason is simple—JVM prohibits this (github.com/cdimascio/java-dotenv#faq) Second, It doesn't have integration with Spring Boot. I'll write workaround for this and will edit answer with the complete solution a bit later. And by the way, It doesn't support different .env files for different environments (development, production, etc). Java world is tough.Factfinding
note that you can use a different .env file per environment by using java-dotenv's "filename" property. that being said, the wider dotenv community recommends using a single .env file per environmentHaunch

© 2022 - 2024 — McMap. All rights reserved.