Issue
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?
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.
What I tried (this is exactly what Maven
suggests):
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.
I see that on the terminal
that 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
.
I also added export PATH=$M2
to my .bashrc
, I did source
and then restarted the terminal, still it did not help.
can someone suggest how to make it available in all sessions of terminal?
Thanks
Solution
Try following these if these might help:
Since your installation works on the terminal you installed, all the exports
you did, work on the current bash and its child process
. but is not spawned to new terminals
.
env
variables are lost if the session is closed; using .bash_profile
, you can make it available in all sessions, since when a bash
session starts, it 'runs' its .bashrc and .bash_profile
Now follow these steps and see if it helps:
type
env | grep M2_HOME
on the terminal that is working. This should give something likeM2_HOME=/usr/local/apache-maven/apache-maven-3.1.1
typing
env | grep JAVA_HOME
should give like this:JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_40.jdk/Contents/Home
Now you have the PATH for M2_HOME
and JAVA_HOME
.
If you just do ls /usr/local/apache-maven/apache-maven-3.1.1/bin
, you will see mvn
binary there.
All you have to do now is to point to this location everytime using PATH. since bash
searches in all the directory path mentioned in PATH
, it will find mvn
.
now open
.bash_profile
, if you dont have one just create onevi ~/.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
save the file and type
source ~/.bash_profile
. This steps executes the commands in the.bash_profile
file and you are good to go now.open a new terminal and type
mvn
that should work.
Answered By - brain storm
Answer Checked By - David Goodson (JavaFixing Volunteer)