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

2024-01-20

自从升级到 OSX Mountain Lion 后,我在设置 eclipse 和 maven 的环境变量时遇到了一些问题。

我的目标是在 Eclipse 中运行 Maven 命令。此命令需要从远程存储库下载工件(解决依赖关系)。存储库通过 HTTPS 进行身份验证。

我已经关注了通过经过身份验证的 HTTPS 远程存储库访问指南 http://maven.apache.org/guides/mini/guide-repository-ssl.html并将以下行添加到我的 .bash_profile 中。如果我在终端中运行 Maven,一切都会正常。

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

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

~/.MacOSX/environment.plist

(也可以看看在 Mac OS X Lion 上设置环境变量 https://stackoverflow.com/questions/7501678/set-environment-variables-on-mac-os-x-lion)这对于 OSX Lion 来说非常有效。

但苹果在 Mountain Lion 上改变了这种行为。我读到environment.plist不再受支持,新方法是编辑.app本身的Info.plist(Mountain Lion 中的系统环境变量在哪里设置? https://apple.stackexchange.com/questions/57385/where-are-system-environment-variables-set-in-mountain-lion)。看来你必须添加一个LS环境 http://developer.apple.com/library/ios/#documentation/general/Reference/InfoPlistKeyReference/Articles/LaunchServicesKeys.html包含所有变量的字典。

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

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

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

感谢您的回答!


不幸的是,这似乎是在 OS X 10.8.x Mountain Lion 中设置全局环境变量的最佳选择:

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

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

launchctl setenv MYVARIABLE value

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

setenv MYVARIABLE value

这有助于我设置一个全局环境变量,该变量可以由 OS X 10.8.2 上的 IntelliJ IDEA CE 12.0 继承。不是很优雅,但是很有效。

或者,您可以在 Terminal.app 中设置环境变量,然后启动要从命令行访问环境变量的应用程序。启动的应用程序将从您的终端会话继承环境。在 Terminal.app 中,设置环境变量并使用如下命令启动另一个应用程序open -a "App Name":

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

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 OSX Mountain Lion 上为应用程序设置环境变量? 的相关文章

随机推荐