Is there a possibility to set an environment variable in init script?


I have a gradle project that needs some environment variable, however there’s some collision with existing environment variable for other projects, so I cannot set a global environment variable in the shell profile (like a $HOME/.zshrc).

It is possible to add a hook in zsh for example to perform these tasks, but it only works in the terminal. IntelliJ for example won’t see the update.

This can fail the project import because the variables are not yet set when IJ starts, or if set they will be plain wrong for the other project.

So I was wondering of init script had the ability to set/override an environment variable (depending on the project) ?
I fear I already know the answer as System does not offer a way to change environment variables after being started.

Exactly. At least not without very deep JRE-specific reflection hackery which I would not recommend.

At least not without very deep JRE-specific reflection hackery which I would not recommend.

Clearly not my intent. But I was hoping maybe gradle had a possible support for this, maybe through the provider API ?

Regardless, I opened a ticket on IJ to support this:

1 Like

That’s for getting them, not setting them. :slight_smile:

I was thinking there could a hidden API like casting to a specific type, or maybe a tooling API that could work around that. I didn’t found anything in IJ, but I recon I was lazy and didn’t check the gradle code base to ensure such API does not exists.

Gradle tries to update daemon environment variables for a build it is starting, but even that is not always working properly, see Pass-through Environment Variables to Gradle Daemon Processes · Issue #10483 · gradle/gradle · GitHub.

We ditched env vars for this reason. You should rely on Gradle properties which are customizable via -P (could override per build) or via ~/.gradle/ (could set for your OS account).

Just give unique prefix for your settings per project - you you won’t collide with others.

Unfortunately I’m not the owner of the project, and from I understand the software the project relies onto uses env vars.
I was looking for ways to isolate the settings for this project in particular with the current state of the code base.

Also are the gradle properties modifiable in the init scripts. (Writing on my phone so didn’t check the code yet).

Thank you for looking up the daemon bootstrap by the way.

No, unfortunately there is also no way to set project properties, let alone gradle properties (there is a slight difference, but you probably meant project properties), during runtime.

If the one reading it uses something like project.getProperty and similar, you can overwrite a project property by setting the according ext / extra property. But if the one reading the value for example uses providers.gradleProperty to read the project property, you cannot change the value at runtime.

OK that’s what I thought.
So I guess IDE support would be welcome in this angle.
Thanks for discussing the topic.

1 Like