简体   繁体   English

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

[英]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. 自从升级到OSX Mountain Lion后,我在为eclipse和maven设置环境变量方面遇到了一些问题。

My goal is to run a maven command in Eclipse. 我的目标是在Eclipse中运行maven命令。 This command needs to download artefacts (resolve dependencies) from a remote repository. 此命令需要从远程存储库下载伪像(解析依赖项)。 The repository is authenticated via HTTPS. 存储库通过HTTPS进行身份验证。

I've followed the Guide to Remote repository access through authenticated HTTPS and added the lines below to my .bash_profil . 通过经过身份验证的HTTPS跟踪了远程存储库访问指南 ,并将下面的行添加到我的.bash_profil中。 If I'm running maven in the terminal everythings works fine. 如果我在终端中运行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. 但是Apple已经改变了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? ). 我已经阅读了environment.plist不再受支持,新方法是编辑.app本身的Info.plist( Mountain Lion中设置的系统环境变量在哪里? )。 It seems you have to add a LSEnvironment dictionary containing all you variables. 看来你必须添加一个包含所有变量的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. 如您所见,我更改了Eclipse.app的Info.plist。 But this did not work. 但这没效果。 I start maven within Eclipse. 我在Eclipse中启动maven。 But maven is not able to download the artefacts, because the remote repository is not trusted. 但maven无法下载文物,因为远程存储库不受信任。 I think Eclipse does not use the environment variables I defined in the Info.plist 我认为Eclipse不使用我在Info.plist中定义的环境变量

Do you have any suggestions how to solve this problem? 您有什么建议如何解决这个问题?

Thanks for your answers! 谢谢你的回答!

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 ,请创建/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. 这对我来说设置了一个全局环境变量,可以由OS X 10.8.2上的IntelliJ IDEA CE 12.0继承。 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. 或者,您可以在Terminal.app中设置环境变量,然后从命令行启动要从中访问环境变量的App。 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中,设置环境变量并使用open -a "App Name"等命令启动另一个应用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

From here : https://stackoverflow.com/a/10374886/325742 从这里: 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. 然后,使用http://mathiasbynens.be/notes/shell-script-mac-apps上的步骤将上述脚本转换为可以保留在扩展坞上的应用程序。

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

Steps of commands on terminal window: 终端窗口上的命令步骤:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM