197

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.

2
  • 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, 2017 at 8: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 ... ? Feb 24, 2018 at 13:08

4 Answers 4

302

Well; this works as well:

home = "$System.env.HOME"

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

6
  • 20
    or to your usage: into "${System.env.HOME}/something/plugins"
    – JoeG
    Sep 17, 2013 at 13:00
  • 12
    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. Apr 12, 2016 at 11:42
  • 5
    or just home = System.env.HOME ? Apr 12, 2017 at 15:53
  • 25
    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 4, 2018 at 17:23
  • 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. Apr 7, 2019 at 21:53
138

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.

3
  • 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 2, 2015 at 19:52
  • 2
    Did you use single quotes instead of double by mistake, perhaps? Oct 1, 2015 at 10:29
  • 12
    this is better than the answer: stackoverflow.com/a/9856769/689223, because it returns null instead of "null" in case it does not exist. Sep 2, 2016 at 13:40
20

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")
13

In android gradle 0.4.0 you can just do:

println System.env.HOME

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

1
  • 16
    thoredge said that 4 years before your answer! Feb 24, 2018 at 13:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.