简体   繁体   中英

How do set environment variable in gradle via task?

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.

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.

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
}

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

There is an easier solution

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

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:

  1. In gradlew.sh and add:
    eg export NODE_OPTIONS=--max_old_space_size=1536
    before:
    exec "$JAVACMD" "$@"
  2. In gradlew.bat and add:
    eg 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%

  3. Modify any scripts, jenkins/travis files and IDE that calls gradle to gradlew

  4. Commit so no one has to feel the grief again.

Some may consider this dirty, others an ok hack - either way it does work and allows me to go to the next problem ;-)

I have a possible work around

If you have a something like in build.gradle (generating java class from avro schema for a file)

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

./gradlew generateJavaFromSchema -PfileName=./src/main/resources/A.avsc

Work around:set the value in 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

./gradlew generateJavaFromSchema

You can still override if need be in the command line

./gradlew generateJavaFromSchema -PfileName=./src/main/resources/B.avsc

Hope this helps

This might not be directly helpful for the exact question, but in case you are for eg running gradle or gradle wrapper tasks directly from the terminal of IntelliJ IDEA, you can set the needed variable before running a gradle task directly in the terminal. The variable will be in memory for as long as the same terminal is still open.

So if you need to set a environment variable that will then be used in your gradle build somewhere, you can do it like this directly in the terminal:

set VAR_NAME=var_value
gradle clean build

after the VAR_NAME variable is set, the build (or any other gradle task you run in the same terminal) will use the value you set into the VAR_NAME variable (given the task uses it somewhere)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM