193

In several Tasks, I reference jars in my home folder.

Is there a better way to get Environment Variables than

ENV = System.getenv()
HOME = ENV['HOME']

task copyToServer(dependsOn: 'jar', type: Copy) {

 from 'build/libs/'
 into HOME + "/something/plugins/"
}

This sets $HOME but I was hoping that I missed some magic from the documentation.

John Smith
  • 7,243
  • 6
  • 49
  • 61
Nicholas Marshall
  • 2,981
  • 2
  • 26
  • 22
  • 1
    Be aware that before environment variable can be seen and used by Gradle, it needs to be exported, ex. `$ export FOO=bar`. – luka5z May 31 '17 at 08:33
  • There's something I'm missing here... I have to put `def HOME`: Gradle 4.4.1 `Project` does not have a `HOME` property or an `ENV` property. Could be something which dropped out ... ? – mike rodent Feb 24 '18 at 13:08

4 Answers4

299

Well; this works as well:

home = "$System.env.HOME"

It's not clear what you're aiming for.

thoredge
  • 12,237
  • 1
  • 40
  • 55
  • 20
    or to your usage: into "${System.env.HOME}/something/plugins" – JoeG Sep 17 '13 at 13:00
  • 11
    Be aware that "$System.env.FOO" returns String with value "null", if the environment variable FOO is not defined as a system environment variable. It might be confusing since logging a String with value "null" to console will print the same output as null variable. – Gökhan Barış Aker Apr 12 '16 at 11:42
  • 5
    or just home = System.env.HOME ? – Alice Purcell Apr 12 '17 at 15:53
  • 24
    If you are trying to get an environment variable that might not be set, it would be better to use `System.getenv('VAR')` which returns null if not assigned. If you use `"$System.env.VAR"` then it will return the string `"null"`. – chrish Jan 04 '18 at 17:23
  • There's something I'm missing here... I have to put `def home`: Gradle 4.4.1 `Project` does not have a `home` property. Could be something which dropped out ... ? – mike rodent Feb 24 '18 at 13:07
  • 4
    @chrish You can also use `System.env.VAR`. The reason `"$System.env.VAR"` returns (the string) `"null"` rather than `null` is because of string interpolation, not because of using `System.env` in place of `System.getenv`. Using `"${System.getenv('VAR')}"` has the exact same problem. – Laurence Gonsalves Apr 07 '19 at 21:53
136

I couldn't get the form suggested by @thoredge to work in Gradle 1.11, but this works for me:

home = System.getenv('HOME')

It helps to keep in mind that anything that works in pure Java will work in Gradle too.

Jarett Millard
  • 5,802
  • 4
  • 41
  • 48
  • Not sure why but I could only get the `System.getenv('HOME')` version to work for me. The other version kept returning `null` – Kip Jun 02 '15 at 19:52
  • 2
    Did you use single quotes instead of double by mistake, perhaps? – Alice Purcell Oct 01 '15 at 10:29
  • 12
    this is better than the answer: http://stackoverflow.com/a/9856769/689223, because it returns `null` instead of `"null"` in case it does not exist. – Ricardo Freitas Sep 02 '16 at 13:40
16

This is for Kotlin DSL (build.gradle.kts):

val myVariable = System.getenv("MY_VARIABLE_NAME") ?: "my default value"

OR

val myVariable = System.getenv("MY_VARIABLE_NAME") ?: error("Env variable not found")

OR

val environment = System.getenv()
val myVariable = environment["MY_VARIABLE_NAME"] ?: "my default value"
// OR val myVariable = environment["MY_VARIABLE_NAME"] ?: error("Env variable not found")
Mahozad
  • 18,032
  • 13
  • 118
  • 133
14

In android gradle 0.4.0 you can just do:

println System.env.HOME

classpath com.android.tools.build:gradle-experimental:0.4.0

Yochai Timmer
  • 48,127
  • 24
  • 147
  • 185