Tuesday 29 July 2014

mvn command is not found on mac

Some times you might already fixed environment variables but after session out.. it will disappear.. in that case you have to do like this.
 Here I explained how to setup environment variable for Maven, Java

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:
  1. type env | grep M2_HOME on 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_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.
  1. now open .bash_profile on Terminal  if you don't have one just create 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
After this click on esc button and : and wq so that your file will save.
Now type
source ~/.bash_profile   click enter
This steps executes the commands in the .bash_profile file and you are good to go now. 
Now type mvn --version 

No comments:

Post a Comment