如何在gradle java构建脚本中访问环境变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27916294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 05:18:05  来源:igfitidea点击:

How to access environment variables in gradle java build script

javagroovybuildgradle

提问by tech developer

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

如何在 gradle java 构建脚本中访问环境变量(用户级别或系统级别)。
我是 gradle 的新手,我正在尝试使用 gradle 构建我的项目。
目前我已经硬编码了第三方 jars 的路径,如下面的脚本所示

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

I have created an environment variable "Third_Party" in enviroment 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

我如何使用这个变量,即“Third_Party”,而不是在脚本中硬编码库路径。
请说出确切的语法

采纳答案by abdotalaat

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

试试这个办法System.getenv("env var name")

home = System.getenv('Third_Party')

task env_read << {
    println "$System.env.HOME"
    //Other way of accessing the environment variable.
    println  System.getenv('Third_Party')
}