In Gradle, is there a better way to get Environment Variables?
Asked Answered
P

4

202

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.

Plume answered 24/3, 2012 at 18:8 Comment(2)
Be aware that before environment variable can be seen and used by Gradle, it needs to be exported, ex. $ export FOO=bar.Aldred
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 ... ?Selfrighteous
L
305

Well; this works as well:

home = "$System.env.HOME"

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

Libelous answered 24/3, 2012 at 23:58 Comment(6)
or to your usage: into "${System.env.HOME}/something/plugins"Secular
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.Azure
or just home = System.env.HOME ?Pastiness
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".Potts
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 ... ?Selfrighteous
@Potts 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.Sissel
T
143

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.

Typhon answered 3/10, 2014 at 17:14 Comment(4)
Not sure why but I could only get the System.getenv('HOME') version to work for me. The other version kept returning nullExtranuclear
Did you use single quotes instead of double by mistake, perhaps?Pastiness
this is better than the answer: https://mcmap.net/q/127134/-in-gradle-is-there-a-better-way-to-get-environment-variables, because it returns null instead of "null" in case it does not exist.Eldridgeeldritch
As of new Gradle versions, it's better to use a provider, specially if you're doing it on the initialization or configuration phase: def homeProvider = providers.environmentVariable('HOST')Fading
J
22

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")
Jerald answered 9/2, 2022 at 15:32 Comment(0)
J
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

Jezabel answered 4/5, 2016 at 12:46 Comment(1)
thoredge said that 4 years before your answer!Selfrighteous

© 2022 - 2024 — McMap. All rights reserved.