Robert
Robert

Reputation: 8571

Gradle: how can I make a task depend on an environment variable?

For example: I generate a file with build information (SVN branch, revision, build date) and include that in my build. Now I'd like to include the value of an environment variable in that file and have Gradle recognize that variable as a dependency. How can I do that?

I can always write the variable to a file and depend on that. Is there an easier way without the extra file?

Upvotes: 4

Views: 1403

Answers (1)

Mark Vieira
Mark Vieira

Reputation: 13466

Same way you would in any Java (or Groovy) code.

System.getenv().get("ENV_VAR_NAME")

When you say have a task "depend on" a variable I assume you mean in terms of incremental build support. If you want to have your task run again if the environment variable changes, you can specify it as a task input.

task myTask {
    inputs.property 'myVar', System.getenv().get("MY_VAR")
}

Upvotes: 6

Related Questions