如何在OSX Mountain Lion上为环境变量设置应用程序?

时间:2021-11-23 10:32:02

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

自从升级到OSX Mountain Lion后,我在为eclipse和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.

我的目标是在Eclipse中运行maven命令。此命令需要从远程存储库下载伪像(解析依赖项)。存储库通过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.

我通过经过身份验证的HTTPS跟踪了远程存储库访问指南,并将下面的行添加到我的.bash_profil中。如果我在终端中运行maven,那么每件事都可以。

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

但这只适用于终端而不适用于应用程序。在以前的OSX版本中,您必须添加MAVEN_OPTS变量

~/.MacOSX/environment.plist

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

(另请参阅在Mac OS X Lion上设置环境变量)这对OSX Lion完美有效。

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.

但是Apple已经改变了Mountain Lion的这种行为。我已经阅读了environment.plist不再受支持,新的方法是编辑.app本身的Info.plist(Mountain Lion中设置的系统环境变量在哪里?)。看来你必须添加一个包含所有变量的LSEnvironment字典。

<?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

如您所见,我更改了Eclipse.app的Info.plist。但这没效果。我在Eclipse中启动maven。但maven无法下载文物,因为远程存储库不受信任。我认为Eclipse不使用我在Info.plist中定义的环境变量

Do you have any suggestions how to solve this problem?

你对如何解决这个问题有什么建议吗?

Thanks for your answers!

谢谢你的回答!

4 个解决方案

#1


20  

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

不幸的是,这似乎是在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:

对于临时环境变量,请在Terminal.app中运行此命令,然后重新启动需要访问该变量的任何应用程序:

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:

要在重新启动后使环境变量保持不变,请创建/etc/launchd.conf并为每个变量添加这样的行,然后重新启动整个系统:

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.

这对我来说设置了一个全局环境变量,可以由OS X 10.8.2上的IntelliJ IDEA CE 12.0继承。不是很优雅,但它的工作原理。

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":

或者,您可以在Terminal.app中设置环境变量,然后从命令行启动要从中访问环境变量的App。启动的应用程序将从您的终端会话继承环境。在Terminal.app中,设置环境变量并使用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.

这将打开IntelliJ IDEA,我的代码可以在其环境中访问$ MYVARIABLE。

#2


1  

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

从这里:https://*.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.

然后,使用http://mathiasbynens.be/notes/shell-script-mac-apps上的步骤将上述脚本转换为可以保留在扩展坞上的应用程序。

#3


1  

You can set env variable directly in eclipse under maven "Debug Configurations" -> "Environment" tab

您可以在maven“Debug Configurations” - >“Environment”选项卡下直接在eclipse中设置env变量

#4


1  

Steps of commands on terminal window:

终端窗口上的命令步骤:

  1. vi ~/.bash_profile
  2. vi~ / .bash_profile
  3. Press i (to bring the vi editor in edit mode)
  4. 按i(使vi编辑器进入编辑模式)
  5. Enter the environment variable in the vi editor window
    • e.g. export JAVA_HOME=/Users/Shared/Jenkins/Home/tools/hudson.model.JDK/java8
    • 例如export JAVA_HOME = / Users / Shared / Jenkins / Home / tools / hudson.model.JDK / java8
  6. 在vi编辑器窗口中输入环境变量,例如export JAVA_HOME = / Users / Shared / Jenkins / Home / tools / hudson.model.JDK / java8
  7. esc key followed by :wq
  8. esc键后跟:wq
  9. source ~/.bash_profile
  10. source~ / .bash_profile

#1


20  

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

不幸的是,这似乎是在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:

对于临时环境变量,请在Terminal.app中运行此命令,然后重新启动需要访问该变量的任何应用程序:

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:

要在重新启动后使环境变量保持不变,请创建/etc/launchd.conf并为每个变量添加这样的行,然后重新启动整个系统:

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.

这对我来说设置了一个全局环境变量,可以由OS X 10.8.2上的IntelliJ IDEA CE 12.0继承。不是很优雅,但它的工作原理。

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":

或者,您可以在Terminal.app中设置环境变量,然后从命令行启动要从中访问环境变量的App。启动的应用程序将从您的终端会话继承环境。在Terminal.app中,设置环境变量并使用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.

这将打开IntelliJ IDEA,我的代码可以在其环境中访问$ MYVARIABLE。

#2


1  

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

从这里:https://*.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.

然后,使用http://mathiasbynens.be/notes/shell-script-mac-apps上的步骤将上述脚本转换为可以保留在扩展坞上的应用程序。

#3


1  

You can set env variable directly in eclipse under maven "Debug Configurations" -> "Environment" tab

您可以在maven“Debug Configurations” - >“Environment”选项卡下直接在eclipse中设置env变量

#4


1  

Steps of commands on terminal window:

终端窗口上的命令步骤:

  1. vi ~/.bash_profile
  2. vi~ / .bash_profile
  3. Press i (to bring the vi editor in edit mode)
  4. 按i(使vi编辑器进入编辑模式)
  5. Enter the environment variable in the vi editor window
    • e.g. export JAVA_HOME=/Users/Shared/Jenkins/Home/tools/hudson.model.JDK/java8
    • 例如export JAVA_HOME = / Users / Shared / Jenkins / Home / tools / hudson.model.JDK / java8
  6. 在vi编辑器窗口中输入环境变量,例如export JAVA_HOME = / Users / Shared / Jenkins / Home / tools / hudson.model.JDK / java8
  7. esc key followed by :wq
  8. esc键后跟:wq
  9. source ~/.bash_profile
  10. source~ / .bash_profile