NAMAN GUPTA
NAMAN GUPTA

Reputation: 101

Set environment variable path for maven (mvn) in mac for Catolina or higher version

When I am trying to set environment variables for mvn in MAC, I am able to set for one session but if I open another terminal and try mvn -version, it doesn't work. Commands I followed:

vi $HOME/.z_profile

--Added this path in .z_profile file

export M2_HOME=/Users/namangupta/Downloads/apache-maven-3.6.3
export PATH=$PATH:$M2_HOME/bin/

source .z_profile

mvn -version

--Output:
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /Users/namangupta/Downloads/apache-maven-3.6.3
Java version: 15, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk-15.jdk/Contents/Home
Default locale: en_IN, platform encoding: UTF-8
OS name: "mac os x", version: "10.15.7", arch: "x86_64", family: "mac"

After these steps when I open another terminal and try mvn -version I get zsh: mvn cmd not found and on running printenv the path gets reset. Can anyone please tell me where I am wrong in setting the path?

Upvotes: 0

Views: 15150

Answers (3)

DBDemigod
DBDemigod

Reputation: 1

You had everything right except the filename - it should be $HOME/.zprofile (with no underscore). That's why it worked when you manually sourced from .z_profile, but didn't work when starting a new terminal.

Upvotes: 0

Yasas
Yasas

Reputation: 1

For MacOS Monterey (version 12.5)

-> open ~/.zshenv ->Add following to .zshenv File

export JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk1.8.0_291.jdk/Contents/Home" export M2_HOME="/Users/namangupta/Workspace/setup_files/mvn/apache-maven-3.8.1" export PATH=$PATH:$M2_HOME/bin:$JAVA_HOME/bin

->Save file ->mvn -version

This solution worked for me.Thank you :)

Upvotes: 0

NAMAN GUPTA
NAMAN GUPTA

Reputation: 101

For MacOS Catolina or higher version, since you have zsh instead of bash, you will need to change the path to another file instead of .bash_profile (used for previous versions) as shown below:

--to open the file

nano ~/.zshenv

--paste the following lines and append path for anything else if you want to, just ensure you enter the proper version u have

export JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk1.8.0_291.jdk/Contents/Home"
export M2_HOME="/Users/namangupta/Workspace/setup_files/mvn/apache-maven-3.8.1"
export PATH=$PATH:$M2_HOME/bin:$JAVA_HOME/bin

--save the file

Ctrl+x, then 'Y' to save and then Enter

--to save the environment variables path

source .zshenv

And you are good to go. To check you can try printenv to check or mvn -version or java -version or echo $PATH

Upvotes: 5

Related Questions