Spring STS not loading environment variables but running maven works

天涯浪子 提交于 2021-01-29 11:38:47

问题


I'm using Mac OS Mojave with Spring STS version 3.9.7.

So I'm trying to load some environment variables such as database username & password into my application.properties file but Spring fails to load them into.

I'v used this command to set the username environment variable:

export ABC_DB_UNAME=some_username

and when I do echo $ABC_DB_UNAME or printenv I can see the value.

In my application.properties file I set the username as below:

spring.datasource.username=${ABC_DB_UNAME}

Now when running my spring sts from the IDE itself, it is not able to pick up the values and it throws an exception that

access is denied for ABC_DB_UNAME@database_endpoint.

It clearly shows that spring sts is not picking up my environment variables.

What makes it more twisting for me is that when I run the spring app via maven using the below command, it picks up the environment variables and it works just fine.

mvn spring-boot:run

but using the mvn command I don't know how I can make the IDE to trigger the breakpoints for me to debug as well.

EDIT: Testing and trying to print the environment variable using the code System.getenv("ABC_DB_UNAME") and System.getProperty("ABC_DB_UNAME") also returns null but maven still works.


回答1:


I think the environmental variable is set temporarily in the terminal. Hence the mvn spring-boot:run works. You can run it in different terminal, it should fail

You need to set the ${ABC_DB_UNAME} in the bashrc (mac) to persist it permanently. or you can pass the environmental variable as argument during running the app.



来源:https://stackoverflow.com/questions/56782840/spring-sts-not-loading-environment-variables-but-running-maven-works

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!