简体   繁体   English

无法从 build.gradle 中的 System.getenv() 访问 mac 上的环境变量

[英]Cannot access environment variable on mac from System.getenv() in build.gradle

So I need to customized dependencies in gradle based on a environment variable.所以我需要根据环境变量在 gradle 中自定义依赖项。 For that I created a variable using the export ENV=test command.为此,我使用export ENV=test命令创建了一个变量。 I do a printenv and I see that as listed in the console on mac.我做了一个 printenv,我看到它在 mac 上的控制台中列出。 However, when I do a System.getenv() from the build.gradle file it is not visble.但是,当我从 build.gradle 文件中执行 System.getenv() 时,它不可见。 Suggestions?建议?

I am using Gradle 6.3 as a deamon.我正在使用 Gradle 6.3 作为守护进程。 The following works perfectly for me:以下内容非常适合我:

println System.getenv()

To get your environment with the key of EXAMPLE, you can do the following:要使用EXAMPLE 键获取您的环境,您可以执行以下操作:

println System.getenv("EXAMPLE")

it works for gradle with version 5.5 as well.它也适用于版本 5.5 的 gradle。

I had similar issue.我有类似的问题。
In my case the gradle daemon cached environment variables.在我的例子中,gradle 守护进程缓存了环境变量。
For reset the cache I stopped demon gradle.为了重置缓存,我停止了恶魔 gradle。

You can use next command for it.您可以使用下一个命令。

gradle --status
gradle --stop

./gradlew --status
./gradlew --stop

status show runnabled demons. status显示可运行的恶魔。 if you don't have, you can to try to stop by using wrapper gradle.如果没有,您可以尝试使用包装器 gradle 来停止。

And after it during next start gradle have my env variables.之后在下一次启动 gradle 有我的环境变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM