Tim
Tim

Reputation: 1826

Does gradle.properties have access to environment variables

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!

Upvotes: 19

Views: 41601

Answers (4)

Funyinoluwa Kashimawo
Funyinoluwa Kashimawo

Reputation: 534

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"

Upvotes: 5

JustinB
JustinB

Reputation: 1

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

Upvotes: 0

ivInMtl
ivInMtl

Reputation: 221

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.

Upvotes: 19

Cisco
Cisco

Reputation: 22952

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.

Upvotes: 8

Related Questions