How do set environment variable in gradle via task?

本小妞迷上赌 提交于 2019-12-30 03:50:10

问题


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.


回答1:


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.

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

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

@echo off
echo %FOO%

Running the build using the command gradle secondTask will yield:

> Task :firstTask
bar

> Task :secondTask
ECHO ist ausgeschaltet (OFF).

So the environment variable is not there anymore for 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-type the method environment is explicitly defined for the Exec-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.




回答2:


As far as I know, you cannot set system environment variable from 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:

tasks.withType(JavaCompile) {
  compileTask -> compileTask.dependsOn MyTask
}



回答3:


There is an easier solution

tasks.withType(org.gradle.api.tasks.testing.Test) {
    systemProperty 'host', 'DEV'
}



回答4:


Combining with Gradle Environment variables. Load from file question, for Java plugin it would be..

tasks.withType(JavaExec) {
    file('.env').readLines().each() {
        def (key, value) = it.tokenize('=')
        environment key, value
    }
}



回答5:


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".

tasks.withType(Exec) {
    environment "DOCKER_HOST", "tcp://localhost:2375"
}


来源:https://stackoverflow.com/questions/22605947/how-do-set-environment-variable-in-gradle-via-task

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!