Java – how to set environment variables through tasks?

I have a graduation application My main graduation document includes some tasks (groovy)

Solution

As far as I know, you cannot set system environment variables from gradle task

However, you can set environment variables for the process Therefore, if you need to set environment variables just to build and 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 compilation dependent on this task, so if you build code that you set environment variables before compilation:

tasks.withType(JavaCompile) {
  compileTask -> compileTask.dependsOn MyTask
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>