11

I looked around and could not find any way of accessing environment variables in my gradle.properties.

What I can do: In my build.gradle I can access environment variables like this System.getenv("MY_VAR"). I would like to do the same in my gradle.properties.

Example of what I want to do: In my gradle.properties replace build.foo=bar with build.foo=System.getenv("BAR")

So far all my attempts to access environment variables from gradle.properties have failed.

Any insight into the matter would be great, thanks!

Tim
  • 1,606
  • 2
  • 21
  • 42

4 Answers4

12

Not all environment variables are visible to gradle. If you can name them with the special prefix ORG_GRADLE_PROJECT_ these will be available. You can read about it here

ORG_GRADLE_PROJECT_foo can be accessed as foo within gradle.

ivInMtl
  • 151
  • 1
  • 6
  • 5
    Your statement is true in that only the specially named env variables get autoloaded into gradle's internal properties. But you can explicitly use System.getEnv('ENV_VAR") to load any envrionment variable into gradle scripts. – Lee Meador Oct 08 '20 at 19:00
9

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

You can't. gradle.properties is not anything fancy, it is a standard properties file.

You would need to post-process the properties somehow as indicated in this. Currently, Gradle does not offer any such functionality to "custom load" gradle.properties.

Cisco
  • 20,972
  • 5
  • 38
  • 60
2

According this Gradle documentation documentation. Environment Variable that follows this name pattern "ORG_GRADLE_PROJECT_foo=bar" will be added as a gradle property with the name "foo"

0

From https://docs.gradle.org/current/userguide/build_environment.html "... but if an option is configured in multiple locations, the first one wins:

system properties, e.g. when -Dgradle.user.home is set on the command line."

In Unix you could do like this: -Dgradle.user.home=$GRADLE_USER_HOME