Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 515
Tim Avatar asked Nov 15 '19 00:11

Tim


People also ask

How do I set environment variables in Gradle properties?

Gradle can also set project properties when it sees specially-named system properties or environment variables. If the environment variable name looks like ORG_GRADLE_PROJECT_prop=somevalue , then Gradle will set a prop property on your project object, with the value of somevalue .

What is Gradle properties for?

Gradle project properties provide an easy way to customise builds which may need to run differently in certain situations. In this article you'll learn the most effective ways to use and set properties, along with some common scenarios you might come across in your Gradle project.


2 Answers

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.

like image 130
Francisco Mateo Avatar answered Nov 15 '22 04:11

Francisco Mateo


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.

like image 42
ivInMtl Avatar answered Nov 15 '22 04:11

ivInMtl