gradle.properties 是否可以访问环境变量

Does gradle.properties have access to environment variables

我环顾四周,在我的 gradle.properties 中找不到任何访问环境变量的方法。

我能做什么: 在我的 build.gradle 中,我可以像这样访问环境变量 System.getenv("MY_VAR")。我想在我的 gradle.properties.

中做同样的事情

我想做的事的例子: 在我的 gradle.properties 中用 build.foo=System.getenv("BAR")

替换 build.foo=bar

到目前为止,我从 gradle.properties 访问环境变量的所有尝试都失败了。

任何对此事的见解都会很棒,谢谢!

could not find any way of accessing environment variables in my gradle.properties.

你不能。 gradle.properties 不是什么花哨的东西,它是 standard properties file.

您将需要 post 以某种方式处理属性,如 this 中所述。目前,Gradle 不向 "custom load" gradle.properties 提供任何此类功能。

并非所有环境变量都对 gradle 可见。如果您可以使用特殊前缀 ORG_GRADLE_PROJECT_ 来命名它们,它们将可用。你可以阅读它 here

ORG_GRADLE_PROJECT_foo 可以作为 foo 在 gradle 中访问。

来自https://docs.gradle.org/current/userguide/build_environment.html “...但是如果一个选项在多个位置配置,第一个获胜:

系统属性,例如当在命令行上设置 -Dgradle.user.home 时。"

在 Unix 中你可以这样做:-Dgradle.user.home=$GRADLE_USER_HOME