3

there. I'm new to Java Spring Boot and I'm trying to set environment variables in application.yml.

I've added dotenv maven dependency:

<!-- https://mvnrepository.com/artifact/io.github.cdimascio/java-dotenv --> <dependency> <groupId>io.github.cdimascio</groupId> <artifactId>java-dotenv</artifactId> <version>5.1.3</version> </dependency> 

I've set variables in the .env file:

SPRING_DATABASE_URL = jdbc://db_url SPRING_DATABASE_USERNAME = username SPRING_DATABASE_PASSWORD = password 

And in my application.yml:

spring: datasource: url: ${SPRING_DATABASE_URL} username: ${env.SPRING_DATABASE_USERNAME} password: ${env.SPRING_DATABASE_PASSWORD} 

While running application I'm getting jdbc error:

java.lang.RuntimeException: Driver org.postgresql.Driver claims to not accept jdbcUrl, ${SPRING_DATABASE_URL}

I've tried some solutions like:

export SPRING_DATABASE_URL = jdbc://db_url 

or in application.yml:

url: ${env.SPRING_DATABASE_URL} 

or

url: ${env.SPRING.DATABASE.URL} 

or

url: ${SPRING.DATABASE.URL} 

Am I doing something wrong or missing? I appreciate your help, thank you.

7
  • 3
    Why not just set, e.g, SPRING_DATSOURCE_URL as the environment variable and skip the mapping between variable names? Commented Mar 29, 2021 at 21:48
  • 1
    how are you running your app? Commented Mar 29, 2021 at 21:50
  • @BoristheSpider I've just tried right now and it gave me same error, again. Commented Mar 29, 2021 at 21:51
  • @eis in vscode, from terminal Commented Mar 29, 2021 at 21:52
  • Then the environment variable is not being set. Without more details about what you’re trying to do it’s not clear how to help. For example I’m not sure what the Maven plug-in does given I presume you want the variables at runtime not build time. Commented Mar 29, 2021 at 21:52

2 Answers 2

1

I recently ran in a similar issue and wanted to set environment variables via .env with application.yml - here is what I found out:

First, as you mentioned you have to add the java-dotenv dependency to pom.xml:

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

Then create your .env file in the root of your project (where pom.xml is located) and write your environment variables like e.g. ENV_PORT=8081.

Before you can use this environment variable, you have to "bind" the content of the .env file with Spring Boot when you start the app to make it globally available. According to this thread, this can be achieved by simply altering your Main entry point of spring (where you init the framework) like so:

@SpringBootApplication public class MySpringApplication { public static void main(String[] args) { Map<String, Object> env = Dotenv.load() .entries() .stream() .collect( Collectors.toMap(DotenvEntry::getKey, DotenvEntry::getValue)); new SpringApplicationBuilder(MySpringApplication.class) .environment(new StandardEnvironment() { @Override protected void customizePropertySources(MutablePropertySources propertySources) { super.customizePropertySources(propertySources); propertySources.addLast(new MapPropertySource("dotenvProperties", env)); } }).run(args); } } 

That's it, now you can reference to your environment variables in application.yml like so:

server: port: ${ENV_PORT} 

Hope this helps! If you are interested, here is also a full working example where I am using this approach.

Sign up to request clarification or add additional context in comments.

1 Comment

You should not need the source code to read the env, it should work by default
0

java-dotenv you use has been renamed to kotlin-dotenv. If you look at its documentation, you'll see how it's used:

With Java

import io.github.cdimascio.dotenv.Dotenv;

Dotenv dotenv = Dotenv.load();

dotenv.get("MY_ENV_VAR1")

That's not what spring boot does - spring boot will not use any dotenv to fetch environment variables, so naturally the plugin won't work for your use case so .env files will not work.

If you start your app defining the environment variable when starting it app, it will work. To see how environment variables are passed using vscode, see this thread.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.