18

How can I access environment variables (either user level or system level) in gradle java build script.
I am new to gradle and I am trying to build my project using the gradle.
Presently I have hardcoded the path of third party jars as shown in the script below

repositories {
   flatDir  {
           dirs 'D:\\lib'
   }
}

I have created an environment variable "Third_Party" in enviroment variable:

Third_Party=D:\lib

How can I use this variable i.e. "Third_Party" instead of hardcoding the library path in script.
Please tell the exact syntax

1 Answer 1

36

try this way System.getenv("env var name") like

home = System.getenv('Third_Party')

task env_read << {
    println "$System.env.HOME"
    //Other way of accessing the environment variable.
    println  System.getenv('Third_Party')
}
Sign up to request clarification or add additional context in comments.

3 Comments

Pls tell me how can I refer to this "home" inside "repositories" repositories { flatDir { dirs 'D:\lib' } }
Why not? It should be the following piece of code: repositories { flatDir { dirs "${System.env.HOME}\lib" } }
The first method worked for me to populate a manifest key from an env var. The second method produced: > The value of a manifest attribute must not be null (Key=Implementation-Version).

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.