1

I am trying to write gradle task which do the following:

  1. Executes script to obtain some data (let's say there is curl inside)
  2. Sets a environment variable with this data.

I would like to set this env variable so other gradle tasks can use it for further processing.

My code looks like this:

task myTask(type: Exec) {
    executable "sh"
    args "-c", "export", "myVar=\$(sh ./script.sh)"
    doLast {
       println System.getenv("myVar") 
    }
}

However when executing I see in the console all my env variables being printed (export KEY=VALUE) and System.getenv("myVar") prints null.

What am I doing wrong? Maybe there is better way to achieve my goal?

AndrewM
  • 69
  • 5

1 Answers1

1

Dynamically updating/settings environment variables is something easily done or possible based on the answers in these questions (many more):

I think a better solution is to write to temporary file of the data you need, then read it as part of your Gradle task:

task myTask(type: Exec) {
    executable "sh"
    args "sh ./script.sh"
    doLast {
       new File("my-file.data")
    }
}
Cisco
  • 20,972
  • 5
  • 38
  • 60