Java 如何通过任务在gradle中设置环境变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22605947/
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
How do set environment variable in gradle via task?
提问by
I have gradle application. And my main gradle-file include some tasks (groovy). I need when start a my task - environment variable "LANG" set encoding = ru_RU.koi8-r (for windows, linux), and after the completion of the my task - environment variable contains the initial value (en_US.UTF-8). How do it? Help me, please.
我有 gradle 应用程序。我的主要 gradle 文件包括一些任务(常规)。我需要在启动我的任务时 - 环境变量“LANG”设置编码 = ru_RU.koi8-r(对于 windows,linux),并且在我的任务完成后 - 环境变量包含初始值(en_US.UTF-8) . 怎么办?请帮帮我。
回答by Martin Rajniak
As far as I know, you cannot set system environment variable from Gradle Task.
据我所知,您无法从 Gradle Task 设置系统环境变量。
However it is possible to set environment variable for that process. Thus if you need to set environment variable just for the build use this:
但是,可以为该进程设置环境变量。因此,如果您需要仅为构建设置环境变量,请使用以下命令:
task MyTask(type: Exec) {
environment 'ENVIRONMENT_VARIABLE_NAME', 'environment_variable_value'
// run or build code that needs that environment variable
}
You can also make compile depend on that task so if you build your code you set environment variable before you compile:
您还可以使 compile 依赖于该任务,因此如果您构建代码,则在编译之前设置环境变量:
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn MyTask
}
回答by java4africa
There is an easier solution
有一个更简单的解决方案
tasks.withType(org.gradle.api.tasks.testing.Test) {
systemProperty 'host', 'DEV'
}
回答by Fencer
I had to do some research in this topic and will try to clarify some open issues. I would do that in two comments instead, but I do not have enough reputation yet. Since I found Igor Ganapolsky's comment also on other Websites but without an answer everytime I feel the need to write another answer here even if the question actually is already answered.
我不得不在这个主题上做一些研究,并将尝试澄清一些悬而未决的问题。我会在两条评论中做到这一点,但我还没有足够的声誉。因为我在其他网站上也发现了 Igor Ganapolsky 的评论,但每次我都觉得有必要在这里写另一个答案,即使问题实际上已经得到了回答。
As Martin Rajniak stated you can set an environment variable like he showed. But that variable is only valid for the task it is defined in or the corresponding process respectively. That means you cannot use it in another following task. I verified that by defining two depending tasks like so:
正如 Martin Rajniak 所说,您可以像他展示的那样设置环境变量。但该变量仅对它分别定义的任务或相应的流程有效。这意味着您不能在其他后续任务中使用它。我通过定义两个相关任务来验证这一点:
task('firstTask', type:Exec) {
environment "FOO", "bar"
workingDir '.'
commandLine 'cmd', '/c', 'print.bat'
}
task ('secondTask', type:Exec) {
dependsOn firstTask
workingDir '.'
commandLine 'cmd', '/c', 'print.bat'
}
The command print.bat
does only echo the environment variable:
该命令print.bat
只回显环境变量:
@echo off
echo %FOO%
Running the build using the command gradle secondTask
will yield:
使用命令运行构建gradle secondTask
将产生:
> Task :firstTask
bar
> Task :secondTask
ECHO ist ausgeschaltet (OFF).
So the environment variable is not there anymore for secondTask
.
所以环境变量不再存在于secondTask
.
So much for the actual topic, but there is another important thing, which may be the cause for Igor's problem:
The method environment
is not available for every Gradle task. As you can see in the documentation of the Exec
-task-typethe method environment
is explicitly defined for the Exec
-task-type.
实际主题就这么多,还有一个重要的事情,这可能是导致 Igor 问题的原因:该方法environment
并非适用于每个 Gradle 任务。正如您在Exec
-task-type的文档中所见,该方法environment
是为Exec
-task-type显式定义的。
To be complete I want to mention that you can pass variables to a java process by using the JavaExec
-task-typeand its method systemProperty
. But you can not use environment
here, because that method is not defined for the JavaExec
-task-type.
为了完整JavaExec
起见,我想提一下,您可以使用-task-type及其方法将变量传递给 java 进程systemProperty
。但是你不能environment
在这里使用,因为该方法没有为JavaExec
-task-type定义。
However, I myself am still looking for a way to define an environment variable that is valid for the whole build without defining it directly via the operating system.
但是,我自己仍在寻找一种方法来定义对整个构建有效的环境变量,而无需直接通过操作系统进行定义。
回答by Sujay
To set an environment variable for a particular task type, you can use the following code snippet. The environment variable "DOCKER_HOST" would be set to all tasks of the type "Exec".
要为特定任务类型设置环境变量,您可以使用以下代码片段。环境变量“DOCKER_HOST”将设置为“Exec”类型的所有任务。
tasks.withType(Exec) {
environment "DOCKER_HOST", "tcp://localhost:2375"
}
回答by Keenan Gebze
Combining with Gradle Environment variables. Load from filequestion, for Java plugin it would be..
结合Gradle 环境变量。从文件加载问题,对于 Java 插件,它将是..
tasks.withType(JavaExec) {
file('.env').readLines().each() {
def (key, value) = it.tokenize('=')
environment key, value
}
}
回答by thkapasi
Try using the the Gradle Wrapper; commit the new files (gradlew.sh
& gradlew.bat
) after calling gradle wrapper
in the project root folder, then edit them as follows:
尝试使用Gradle Wrapper;在项目根文件夹中调用后提交新文件(gradlew.sh
& gradlew.bat
)gradle wrapper
,然后按如下方式编辑它们:
- In
gradlew.sh
and add:
e.g.export NODE_OPTIONS=--max_old_space_size=1536
before:exec "$JAVACMD" "$@"
In
gradlew.bat
and add:
e.g.set NODE_OPTIONS=--max_old_space_size=1536
(or something like that).
before:@rem Execute Gradle "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
Modify any scripts, jenkins/travis files and IDE that calls
gradle
togradlew
Commit so no one has to feel the grief again.
- 在
gradlew.sh
并添加:
例如export NODE_OPTIONS=--max_old_space_size=1536
之前:exec "$JAVACMD" "$@"
输入
gradlew.bat
并添加:
例如set NODE_OPTIONS=--max_old_space_size=1536
(或类似的东西)。
前:@rem Execute Gradle "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
修改调用
gradle
到的任何脚本、jenkins/travis 文件和 IDEgradlew
承诺这样就没有人再次感受到悲伤。
Some may consider this dirty, others an ok hack - either way it does work and allows me to go to the next problem ;-)
有些人可能认为这很脏,其他人可能会认为这是一个不错的黑客 - 无论哪种方式它都可以工作并允许我转到下一个问题 ;-)
回答by Sayeef Rahim
I have a possible work around
我有一个可能的解决方法
If you have a something like in build.gradle(generating java class from avro schema for a file)
如果您在build.gradle 中有类似的东西 (从文件的 avro 模式生成 java 类)
task generateJavaFromSchema(type:JavaExec) { main = 'org.apache.avro.tool.Main' args 'compile' args 'schema' args "${fileName}" args './src/main/java/' classpath = sourceSets.main.compileClasspath }
Then gradle expects something like "-PfileName=./src/main/resources/A.avsc", otherwise gradle will complain fileName not defined
然后gradle需要类似“-PfileName=./src/main/resources/A.avsc”的东西,否则gradle会抱怨fileName not defined
./gradlew generateJavaFromSchema -PfileName=./src/main/resources/A.avsc
Work around:set the value in gradle.properties
解决方法:在gradle.properties 中设置值
<pre>
fileName=./src/main/resources/A.avsc
</pre>
Then the following works , but it will use the default value gradle.properties file in the following case
然后下面的工作,但它会在以下情况下使用默认值 gradle.properties 文件
./gradlew generateJavaFromSchema
You can still override if need be in the command line
如果需要在命令行中,您仍然可以覆盖
./gradlew generateJavaFromSchema -PfileName=./src/main/resources/B.avsc
Hope this helps
希望这可以帮助