如何为 OSX Mountain Lion 上的应用程序设置环境变量? [英] How to set environment variables to an application on OSX Mountain Lion?

查看:29
本文介绍了如何为 OSX Mountain Lion 上的应用程序设置环境变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自从升级到 OSX Mountain Lion 以来,我在为 eclipse 和 maven 设置环境变量时遇到了一些问题.

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

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

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.

我已经按照 通过已认证的 HTTPS 访问远程存储库的指南 并将以下行添加到我的 .bash_profil 中.如果我在终端中运行 maven 一切正常.

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"

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

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

(另见 在 Mac OS X Lion 上设置环境变量) 这对 OSX Lion 非常有效.

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

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

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>

如您所见,我更改了 Eclipse.app 的 Info.plist.但这没有用.我在 Eclipse 中启动了 Maven.但是 maven 无法下载人工制品,因为远程存储库不受信任.我认为 Eclipse 没有使用我在 Info.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?

感谢您的回答!

推荐答案

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

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

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

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

launchctl setenv MYVARIABLE value

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

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

这对我有用,可以在 OS X 10.8.2 上设置可由 IntelliJ IDEA CE 12.0 继承的全局环境变量.不是很优雅,但很管用.

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.

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

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"

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

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

这篇关于如何为 OSX Mountain Lion 上的应用程序设置环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆