paulschwarz/spring-dotenv

Configure `.env.properties` `directory` Property To Root Directory

taljacob2 opened this issue · 3 comments

Hi,
my .env file is located in the root directory of my project.

It is important for me to place it in the root directory, because it shares variables from docker and other platforms to the spring application.

I'd like to configure the directory property of the .env.properties file to be the root directory, but so far, the only way I've found to do this is by passing the absolute path of the root directory:

image
image

But this solution is not good for me, because I need to deploy the project on another server, and then the path will change.
Which is why I need to use the relative path to the root directory.

Is there a way to do this directly from the .env.properties file?
If not, could you please create a new feature for it?

Thank you!

Hi @taljacob2,

The purpose of a .env file is for you, as a developer working locally, to have a convenient place to emulate environment variables. Instead of updating the environment of your local machine every time you want to make a change, you just update the .env file. Also, you keep that .env file out of source control; it's yours.

But when you start to talk about deploying to a server, this is when you should be configuring the server's actual environment variables.

The server shouldn't be reading from the .env file. Instead, the variables configured in your environment are available to the running application.

Does that make sense?

@paulschwarz
Thank you for answering.

Honestly, I did not fully understand the explanation.

But I have found a way to import the .env file that is located in the root directory, to the application.properties file.

The root directory can be accessed with ${user.dir}/.. or ${user.dir}, so in the application.properties add the following line:

spring.config.import=optional:file:${user.dir}/../.env[.properties],optional:file:${user.dir}/.env[.properties]

So you can import the .env file natively with spring boot.

@taljacob2 thanks for your screenshot I found that .env.properties files have to be in resources 😀