bash 在 OSX Mavrerick 中找不到 mvn 命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21028872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 00:35:54  来源:igfitidea点击:

mvn command not found in OSX Mavrerick

macosbashmaventerminal

提问by eagertoLearn

Before marking this as duplicate, I went through these posts, but nothing helped. 'mvn' is not recognized as an internal or external command,Getting -bash: mvn: command not found, Can't access mvn command from command line?

在将其标记为重复之前,我浏览了这些帖子,但没有任何帮助。 'mvn' 未被识别为内部或外部命令,正在获取 -bash: mvn: command not found无法从命令行访问 mvn 命令?

Some are specific to windows and did not help. A couple of them on Mac OS X gave suggestions, that I tried but did not help.

有些是特定于 Windows 的,并没有帮助。Mac OS X 上的几个人提出了建议,我尝试过但没有帮助。

What I tried (this is exactly what Mavensuggests):

我试过的(这正是Maven暗示的):

Extract the distribution archive, i.e. apache-maven-3.1.1-bin.tar.gz to the directory you wish to install Maven 3.1.1. These instructions assume you chose /usr/local/apache-maven. The subdirectory apache-maven-3.1.1 will be created from the archive. In a command terminal, add the M2_HOME environment variable, e.g. export M2_HOME=/usr/local/apache-maven/apache-maven-3.1.1. Add the M2 environment variable, e.g. export M2=$M2_HOME/bin. Optional: Add the MAVEN_OPTS environment variable to specify JVM properties, e.g. export MAVEN_OPTS="-Xms256m -Xmx512m". This environment variable can be used to supply extra options to Maven. Add M2 environment variable to your path, e.g. export PATH=$M2:$PATH. Make sure that JAVA_HOME is set to the location of your JDK, e.g. export JAVA_HOME=/usr/java/jdk1.5.0_02 and that $JAVA_HOME/bin is in your PATH environment variable. Run mvn --version to verify that it is correctly installed.

将分发存档,即 apache-maven-3.1.1-bin.tar.gz 解压缩到您希望安装 Maven 3.1.1 的目录。这些说明假设您选择了 /usr/local/apache-maven。子目录 apache-maven-3.1.1 将从存档中创建。在命令终端中,添加 M2_HOME 环境变量,例如 export M2_HOME=/usr/local/apache-maven/apache-maven-3.1.1。添加M2环境变量,例如export M2=$M2_HOME/bin。可选:添加 MAVEN_OPTS 环境变量以指定 JVM 属性,例如 export MAVEN_OPTS="-Xms256m -Xmx512m"。此环境变量可用于为 Maven 提供额外的选项。将 M2 环境变量添加到您的路径中,例如 export PATH=$M2:$PATH。确保将 JAVA_HOME 设置为 JDK 的位置,例如 export JAVA_HOME=/usr/java/jdk1.5。0_02 并且 $JAVA_HOME/bin 在您的 PATH 环境变量中。运行 mvn --version 以验证它是否已正确安装。

I see that on the terminalthat I used for installation, it works fine. I do not have this issue. but when I tried on a new terminal, I get command not found.

我看到在terminal我用于安装的那个上,它工作正常。我没有这个问题。但是当我试穿新的时terminal,我得到了command not found

I also added export PATH=$M2to my .bashrc, I did sourceand then restarted the terminal, still it did not help.

我还添加export PATH=$M2了我的.bashrc, 我做了source然后重新启动了终端,仍然没有帮助。

can someone suggest how to make it available in all sessions of terminal?

有人可以建议如何在终端的所有会话中使用它吗?

Thanks

谢谢

回答by brain storm

Try following these if these might help:

如果这些可能有帮助,请尝试执行以下操作:

Since your installation works on the terminal you installed, all the exportsyou did, work on the current bash and its child process. but is not spawned to new terminals.

由于您的安装在您安装的终端上运行,因此exports您所做的所有工作都在current bash and its child process. 但不会产生到 n​​ew terminals

envvariables are lost if the session is closed; using .bash_profile, you can make it available in all sessions, since when a bashsession starts, it 'runs' its .bashrc and .bash_profile

env如果会话关闭,变量将丢失;使用.bash_profile,您可以使其在所有会话中可用,因为当bash会话开始时,它会“运行”它的.bashrc and .bash_profile

Now follow these steps and see if it helps:

现在按照以下步骤操作,看看是否有帮助:

  1. type env | grep M2_HOMEon the terminal that is working. This should give something like

    M2_HOME=/usr/local/apache-maven/apache-maven-3.1.1

  2. typing env | grep JAVA_HOMEshould give like this:

    JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_40.jdk/Contents/Home

  1. 键入env | grep M2_HOME正在工作在终端上。这应该给出类似的东西

    M2_HOME=/usr/local/apache-maven/apache-maven-3.1.1

  2. 打字env | grep JAVA_HOME应该是这样的:

    JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_40.jdk/Contents/Home

Now you have the PATH for M2_HOMEand JAVA_HOME.

现在你有 PATHM2_HOMEJAVA_HOME

If you just do ls /usr/local/apache-maven/apache-maven-3.1.1/bin, you will see mvnbinary there. All you have to do now is to point to this location everytime using PATH. since bashsearches in all the directory path mentioned in PATH, it will find mvn.

如果你只是这样做ls /usr/local/apache-maven/apache-maven-3.1.1/bin,你会mvn在那里看到二进制文件。你现在要做的就是每次使用 PATH 指向这个位置。由于bash在 中提到的所有目录路径中搜索PATH,它会找到mvn.

  1. now open .bash_profile, if you dont have one just create one

    vi ~/.bash_profile

  1. 现在打开.bash_profile,如果你没有一个就创建一个

    vi ~/.bash_profile

Add the following:

添加以下内容:

#set JAVA_HOME
JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_40.jdk/Contents/Home
export JAVA_HOME


M2_HOME=/usr/local/apache-maven/apache-maven-3.1.1
export M2_HOME

PATH=$PATH:$JAVA_HOME/bin:$M2_HOME/bin
export PATH
  1. save the file and type source ~/.bash_profile. This steps executes the commands in the .bash_profilefile and you are good to go now.

  2. open a new terminal and type mvnthat should work.

  1. 保存文件并键入source ~/.bash_profile. 此步骤执行.bash_profile文件中的命令,现在您可以开始了。

  2. 打开一个新终端并输入mvn应该可以工作的内容。

回答by limitlessriver

I did this on macOS Sierra Version 10.12.6. Solutions above are good but they require ~/.bash_profile. /usr/local/binis already in the $PATHand it can be confirmed by doing echo $PATH. Download mavenand run the following commands -

我在macOS Sierra Version 10.12.6上做了这个。上面的解决方案很好,但它们需要 ~/.bash_profile。/usr/local/bin已经在$PATH 中,可以通过执行echo $PATH. 下载 maven并运行以下命令 -

$ cd ~/Downloads
$ tar xvf apache-maven-3.5.3-bin.tar.gz
$ mv apache-maven-3.5.3 /usr/local/
$ cd /usr/local/bin
$ sudo ln -s ../apache-maven-3.5.3/bin/mvn mvn
$ mvn -version
$ which mvn

Note:The version of apache maven would be the one you will download.

注意:您将下载 apache maven 的版本。

EDITED:This also works on macOS Mojave Version 10.14.14.

编辑:这也适用于macOS Mojave 版本 10.14.14

回答by Oleg K

Here is what worked for me.

这对我有用。

First of all I checked if M2_HOME variable is set env | grep M2_HOME. I've got nothing.

首先,我检查是否设置了 M2_HOME 变量env | grep M2_HOME。我什么都没有

I knew I had Maven installed in the folder "/usr/local/apache-maven-3.2.2", so executing the following 3 steps solved the problem for me:

我知道我在文件夹“/usr/local/apache-maven-3.2.2”中安装了Maven,因此执行以下3个步骤为我解决了问题:

  1. Set M2_HOME env variable
  1. 设置 M2_HOME 环境变量

M2_HOME=/usr/local/apache-maven-3.2.2

M2_HOME=/usr/local/apache-maven-3.2.2

  1. Set M2 env variable
  1. 设置 M2 环境变量

M2=$M2_HOME/bin

M2=$M2_HOME/bin

  1. Update the PATH
  1. 更新路径

export PATH=$M2:$PATH

export PATH=$M2:$PATH

As mentioned above you can save that sequence in the .bash_profilefile if you want it to be executed automatically.

如上所述,.bash_profile如果您希望它自动执行,您可以将该序列保存在文件中。

回答by Manisha Malik

steps to install maven :

安装maven的步骤:

  1. download the maven file from http://maven.apache.org/download.cgi
  2. $tar xvf apache-maven-3.5.4-bin.tar.gz
  3. copy the apache folder to desired place $cp -R apache-maven-3.5.4 /Users/locals
  4. go to apache directory $cd /Users/locals/apache-maven-3.5.4/
  5. create .bash_profile $vim ~/.bash_profile
  6. write these two command : export M2_HOME=/Users/manisha/apache-maven-3.5.4 export PATH=$PATH:$M2_HOME/bin7 save and quit the vim :wq!
  7. restart the terminal and type mvn -version
  1. http://maven.apache.org/download.cgi下载 maven 文件
  2. $tar xvf apache-maven-3.5.4-bin.tar.gz
  3. 将 apache 文件夹复制到所需位置$cp -R apache-maven-3.5.4 /Users/locals
  4. 转到 apache 目录$cd /Users/locals/apache-maven-3.5.4/
  5. 创建 .bash_profile $vim ~/.bash_profile
  6. 写这两个命令: export M2_HOME=/Users/manisha/apache-maven-3.5.4 export PATH=$PATH:$M2_HOME/bin7 保存并退出vim :wq!
  7. 重新启动终端并输入mvn -version

回答by jareto

I followed brain storm's instructions and still wasn't getting different results - any new terminal windows would not recognize the mvn command. I don't know why, but breaking out the declarations in smaller chunks .bash_profile worked. As far as I can tell, I'm essentially doing the same thing he did. Here's what looks different in my .bash_profile:

我遵循了头脑风暴的指示,但仍然没有得到不同的结果——任何新的终端窗口都无法识别 mvn 命令。我不知道为什么,但是将声明分成较小的块 .bash_profile 工作。据我所知,我基本上和他做的一样。以下是我的 .bash_profile 中的不同之处:

JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home
export PATH JAVA_HOME
J2=$JAVA_HOME/bin
export PATH J2
M2_HOME=/usr/local/apache-maven/apache-maven-2.2.1
export PATH M2_HOME
M2=$M2_HOME/bin
export PATH M2

回答by Sun

I got same problem, I tried all above, noting solved my problem. Luckely, I solved the problem this way:

我遇到了同样的问题,我尝试了以上所有方法,注意到解决了我的问题。幸运的是,我通过这种方式解决了这个问题:

echo $SHELL

Output

输出

/bin/zsh
OR 
/bin/bash

If it showing "bash" in output. You have to add env properties in .bashrc file (.bash_profile i did not tried, you can try) or else
It is showing 'zsh' in output. You have to add env properties in .zshrc file, if not exist already you create one no issue.