How to set environment variables to an application on OSX Mountain Lion?

Loki picture Loki · Aug 28, 2012 · Viewed 32k times · Source

since the upgrade to OSX Mountain Lion I‘ve got some problems with setting the environment variables for eclipse and maven.

My goal is to run a maven command in Eclipse. This command needs to download artefacts (resolve dependencies) from a remote repository. The repository is authenticated via HTTPS.

I‘ve followed the Guide to Remote repository access through authenticated HTTPS and added the lines below to my .bash_profil . If I‘m running maven in the terminal everythings works fine.

export MAVEN_OPTS="-Xmx512m -Djavax.net.ssl.trustStore=/Users/myUser/.knowncerts/trust.jks -Djavax.net.ssl.trustStorePassword=trustPwd"

But this does only work for the terminal and not for applications. On previous OSX-Versions you had to add the MAVEN_OPTS variable to

~/.MacOSX/environment.plist

(see also Set environment variables on Mac OS X Lion) This worked for OSX Lion perfectly.

But Apple has changed this behaviour on Mountain Lion. I‘ve read the environment.plist is no longer supported and the new way is to edit the Info.plist of the .app itself (Where are system environment variables set in Mountain Lion?). It seems you have to add a LSEnvironment dictionary containing all you variables.

<?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>LSEnvironment</key>
    <dict>
        <key>M2_HOME</key>
        <string>/usr/share/maven</string>
        <key>MAVEN_OPTS</key>
        <string>-Xmx512m -Djavax.net.ssl.trustStore=/Users/myUser/.knowncerts/trust.jks -Djavax.net.ssl.trustStorePassword=trustPwd</string>
    </dict>
    <key>CFBundleExecutable</key>
    <string>eclipse</string>
    <key>CFBundleGetInfoString</key>
    <string>Eclipse 3.8 for Mac OS X, Copyright IBM Corp. and others 2002, 2011. All rights reserved.</string>
    <key>CFBundleIconFile</key>
    <string>Eclipse.icns</string>
    <key>CFBundleIdentifier</key>
    <string>org.eclipse.eclipse</string>
    <key>CFBundleInfoDictionaryVersion</key>
    <string>6.0</string>
    <key>CFBundleName</key>
    <string>Eclipse</string>
    <key>CFBundlePackageType</key>
    <string>APPL</string>
    <key>CFBundleShortVersionString</key>
    <string>3.8</string>
    <key>CFBundleSignature</key>
    <string>????</string>
    <key>CFBundleVersion</key>
    <string>3.8</string>
    <key>CFBundleDevelopmentRegion</key>
    <string>English</string>
    <key>CFBundleLocalizations</key>
    <array>
        <string>ar</string>
        <string>cs</string>
        <string>da</string>
        <string>el</string>
        <string>en</string>
        <string>es</string>
        <string>de</string>
        <string>fi</string>
        <string>fr</string>
        <string>hu</string>
        <string>it</string>
        <string>iw</string>
        <string>ja</string>
        <string>ko</string>
        <string>nl</string>
        <string>no</string>
        <string>pl</string>
        <string>pt_BR</string>
        <string>pt</string>
        <string>ru</string>
        <string>sv</string>
        <string>tr</string>
        <string>zh_HK</string>
        <string>zh_TW</string>
        <string>zh</string>
    </array>
    <key>Eclipse</key>
    <array>
        <string>-keyring</string>
        <string>~/.eclipse_keyring</string>
        <string>-showlocation</string>
    </array>
</dict>
</plist>

As you can see I changed the Info.plist of my Eclipse.app. But this did not work. I start maven within Eclipse. But maven is not able to download the artefacts, because the remote repository is not trusted. I think Eclipse does not use the environment variables I defined in the Info.plist

Do you have any suggestions how to solve this problem?

Thanks for your answers!

Answer

Steve HHH picture Steve HHH · Jan 11, 2013

Unfortunately, this seems to be the best option for setting a global environment variable in OS X 10.8.x Mountain Lion:

For temporary environment variables, run this command in Terminal.app, and restart any apps that need to access the variable:

launchctl setenv MYVARIABLE value

To make an environment variable persistent across reboots, create /etc/launchd.conf and add a line like this for each variable, then reboot your entire system:

setenv MYVARIABLE value

This worked for me to set a global environment variable that could be inherited by IntelliJ IDEA CE 12.0 on OS X 10.8.2. Not very elegant, but it works.

Alternatively, you can set the environment variable in Terminal.app, then launch the App from which you want to access the environment variable from the command-line. The launched app will inherit the environment from your terminal session. In Terminal.app, set the environment variable and launch another app with a command like open -a "App Name":

export MYVARIABLE=value
open -a "IntelliJ IDEA 12 CE"

This opens IntelliJ IDEA, and my code can access $MYVARIABLE in its environment.