如何设置M2_HOME和PATH env。 OS X上的变量永久保留Maven,以防止在重新启动终端后删除它们

时间:2014-06-29 10:56:57

标签: maven

我遇到了无法设置M2_HOMEPATH env的问题。 OS X上的变量永久为Maven。每次重新启动终端时都会删除它们。怎么处理呢?

终端重启后我有:

echo $PATH
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin

并且$M2_HOME

没有任何内容

编辑: 我按照官方Maven安装页面中的步骤操作,并使用以下

创建〜/ .bash_profile
export M2_HOME=$(/usr/local/Cellar/maven/3.2.2/libexec)
export M2=$M2_HOME/bin
export PATH=$M2:$PATH
export JAVA_HOME=$(/usr/libexec/java_home)
export PATH=$JAVA_HOME/bin

但它根本不起作用。 echo $ M2_HOME或任何其他变量给出空行。

3 个答案:

答案 0 :(得分:8)

您需要在bash_profile文件中添加maven路径

在终端中键入以下内容:

nano ~/.bash_profile

在打开的窗口中添加路径为:

export M2_HOME=/usr/local/apache-maven/apache-maven-3.1.1
export PATH=$PATH:$M2_HOME/bin

按CTRL_X然后按Y保存。按Enter键。

在终端类型:

source ~/.bash_profile
echo $M2_HOME

您将看到路径似乎已设置。

答案 1 :(得分:0)

我只是让它与众不同。我将apache-maven-3.2.2-bin.tar.gz解压缩到/usr/share/java/apache-maven-3.2.2并在/usr/share中创建了一个链接:

lrwxr-xr-x    1 root   wheel    34 Jun 29 00:38 maven -> /usr/share/java/apache-maven-3.1.1

并在我的.profile文件(我的主文件夹)中添加/更改以下行

PATH=/usr/share/maven/bin:$PATH 

这使我有机会通过更改上述链接来更改基础Maven版本。

答案 2 :(得分:0)

这是一篇很棒的文章,介绍如何使用brew设置MAVNE的HOME:

https://wilsonmar.github.io/maven-on-macos/

To install the latest version:  
brew update
brew install maven

mvn -version

Apache Maven 3.5.2 (138edd61fd100ec658bfa2d307c43b76940a5d7d; 2017-10-18T03:58:13-04:00)
Maven home: /usr/local/Cellar/maven/3.5.2/libexec
Java version: 1.8.0_25, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.11.6", arch: "x86_64", family: "mac"

Confirm the path:


Confirm the path. Substitute the version (3.5.2) with the version you just installed:

cd /usr/local/Cellar/maven/3.5.2/libexec/bin
ls

mvn files should be listed:

m2.conf     mvn      mvnDebug mvnyjp

Open using a text editor, substituting subl (for Sublime) with atom or vim:

cd $HOME
subl .bash_profile

Add the following to under export PATH=, changing the version (3.5.2) to whatever appears above:

export M2_HOME=/usr/local/Cellar/maven/3.5.2/libexec
export M2=$M2_HOME/bin
export PATH=$PATH:$M2_HOME/bin