Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 935
Loki Avatar asked Aug 28 '12 18:08

Loki


People also ask

Does OSX have environment variables?

The shell uses environment variables to store information, such as the name of the current user, the name of the host computer, and the default paths to any commands.

Where can you define your application environment variable?

An environment variable is a variable whose value is set outside the program, typically through functionality built into the operating system or microservice. An environment variable is made up of a name/value pair, and any number may be created and available for reference at a point in time.


2 Answers

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

  • https://stackoverflow.com/a/588442/705157

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.

like image 126
Steve HHH Avatar answered Oct 21 '22 22:10

Steve HHH


From here : https://stackoverflow.com/a/10374886/325742

#!/bin/sh
#
export MAVEN_OPTS=#MAVEN_OPTS_HERE#
LAUNCHER_JAR=/Applications/eclipse/plugins/org.eclipse.equinox.launcher_1.2.0.v20110502.jar

java \
-showversion \
-XX:MaxPermSize=256m \
-Xms1024m \
-Xmx1024m \
-Xdock:icon=/Applications/eclipse/Eclipse.app/Contents/Resources/Eclipse.icns \
-XstartOnFirstThread \
-Dorg.eclipse.swt.internal.carbon.smallFonts \
-Dosgi.requiredJavaVersion=1.5 \
-jar $LAUNCHER_JAR

Then, use the steps on http://mathiasbynens.be/notes/shell-script-mac-apps, to turn the above script into an application that can be kept on the dock.

like image 31
Ashutosh Jindal Avatar answered Oct 22 '22 00:10

Ashutosh Jindal