如何通过任务在 gradle 中设置环境变量? [英] How do set environment variable in gradle via task?

查看:44
本文介绍了如何通过任务在 gradle 中设置环境变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 gradle 应用程序.我的主要 gradle 文件包括一些任务(常规).我需要在开始我的任务时 - 环境变量LANG"设置编码 = ru_RU.koi8-r(对于 windows,linux),并且在我的任务完成后 - 环境变量包含初始值(en_US.UTF-8).怎么办?请帮帮我.

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.

推荐答案

我不得不在这个主题上做一些研究,并将尝试澄清一些悬而未决的问题.我会在两条评论中做到这一点,但我还没有足够的声誉.因为我也在其他网站上发现了 Igor Ganapolsky 的评论,但每次我都觉得有必要在这里写另一个答案,即使问题实际上已经得到了回答.

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.

正如 Martin Rajniak 所说,您可以像他展示的那样设置环境变量.但该变量只对它分别定义的任务或相应的流程有效.这意味着您不能在另一个后续任务中使用它.我通过定义两个依赖的任务来验证这一点:

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:

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'
}

命令 print.bat 只回显环境变量:

The command print.bat does only echo the environment variable:

@echo off
echo %FOO%

使用命令 gradle secondTask 运行构建将产生:

Running the build using the command gradle secondTask will yield:

> Task :firstTask
bar

> Task :secondTask
ECHO ist ausgeschaltet (OFF).

所以 secondTask 的环境变量不再存在.

So the environment variable is not there anymore for secondTask.

实际主题就这么多,但还有一个重要的事情,这可能是导致 Igor 问题的原因:方法 environment 不适用于每个 Gradle 任务.正如您在 Exec 的文档中所见-task-type 方法 environment 是为 Exec-task-type 明确定义的.

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-type the method environment is explicitly defined for the Exec-task-type.

为了完整起见,我想提一下,您可以使用 JavaExec-task-type 及其方法 systemProperty.但是你不能在这里使用 environment,因为该方法没有为 JavaExec-task-type 定义.

To be complete I want to mention that you can pass variables to a java process by using the JavaExec-task-type and its method systemProperty. But you can not use environment here, because that method is not defined for the 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.

这篇关于如何通过任务在 gradle 中设置环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆