8

I wrote a task in gradle that grabs an environment variable.

task run(type:Exec) {
    def SYS_ENV = System.getenv("SYSTEM_ENV")
    // do something with SYS_ENV 
}

If I run this task with ./gradlew :taskName it's all fine, but if I run the same task from IntelliJ IDEA, (with the https://www.jetbrains.com/img/webhelp/run.png button, or from the gradle panel) the env variable comes out as null.

I tried restarting IntelliJ, I also tried the Invalidate Caches/Restart option, but nothing changes.

Isn't IntelliJ IDEA supposed to run the Gradle script exactly like I run it form the command? What can I do to grab an env variable from Grade so that the script doesn't fail when run form IntelliJ?

EDIT: I did a bunch of trials, and I could make lanchctl setenv MY_PATH MY_VALUE work, but it's not permanent, and adding setenv MY_PATH MY_VALUE to /etc/lanchd.conf does not make it so.

doplumi
  • 2,938
  • 4
  • 29
  • 45
  • Which OS are you using, and where exactly is the SYSTEM_ENV variable set? – yole Dec 30 '14 at 22:11
  • I'm on OSX Yosemite and I set the variable in ~/.bash_profile – doplumi Dec 30 '14 at 22:17
  • 2
    I believe that is your issue. Environment variables set in *~/.bash_profile* won't be available to GUI applications. See [this answer](http://stackoverflow.com/a/588442/957630) for more info. – Mark Vieira Dec 30 '14 at 22:43
  • @Mark Vieira Thanks for that. I'll try as soon as I get to the PC and report back. – doplumi Dec 30 '14 at 22:49
  • @MarkVieira I tried and it doesn't work on my machine, even thought it seems it should from other posts. Maybe in IntelliJ IDEA 14 something changed, I have no idea. (Yes, I restarted the PC after editing launch.conf) – doplumi Dec 31 '14 at 14:52
  • `launchct setenv MY_PATH MY_VALUE` works, but it's not permanent... – doplumi Dec 31 '14 at 15:17
  • I give all the research up. This problem has [precedents](http://emmanuelbernard.com/blog/2012/05/09/setting-global-variables-intellij/), but no solutions. Please, feel free to post an answer if you find one. – doplumi Dec 31 '14 at 15:29
  • For future reference, [here](http://stackoverflow.com/questions/135688/setting-environment-variables-in-os-x) is another related question I found. – doplumi Dec 31 '14 at 15:44
  • As a workaround you could try launching IntelliJ from the terminal. – Mark Vieira Dec 31 '14 at 16:14
  • It's not really a solution since the goal here is portability (which is what env variables are there for, after all). In other words, it's better to ask a user to hard-code the variable in the script than to ask him to run the IDE form the terminal, I think. – doplumi Dec 31 '14 at 16:20

1 Answers1

0

A relatively old question, but the following worked for me on Yosemite (10.10) so answering here in case others have the issue.

Support for launchd.conf was removed in Yosemite. LaunchAgents can be used in OSX to start per-user processes. You can use these to re-run the launchctl step in your question effectively persisting the environment variable

  1. Create a user Launch Agent to set your variables (edit the environment variables and run the following in a terminal window)

    cat << EOF > ~/Library/LaunchAgents/user.environment.plist
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
      <dict>
        <key>Label</key>
        <string>user.environment</string>
        <key>ProgramArguments</key>
        <array>
          <string>sh</string>
          <string>-c</string>
          <string>launchctl setenv MYPATH MY_VALUE</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
      </dict>
    </plist>
    EOF
    
  2. Restart your mac

  3. Make sure you restart any open applications (e.g. IntelliJ). Just restarting didn't work for me, I needed to close and re-launch the apps.

References:

Community
  • 1
  • 1
lemoncurd
  • 376
  • 3
  • 7