How to install Maven in Mac Catalina? - maven

I just installed Maven via Mac Catalina via command line and mvn -version was showing output. then it went away. Please help.

May be a long post, but in the end you will have a convenient tool to install packages and understanding of how to configure them.
Why maven "disappeared" I have no idea of, so what I would do in this situation:
First of all, I would delete maven manually (find the directory and remove it along with any other files that were setup during maven installation)
Then I would highly recommend using Homebrew - a package manager for macOS
To install Homebrew, open terminal and execute the following command:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
Wait for a couple of minutes (a less) until it is installed
Now, that Homebrew is installed, you have a very convenient tool to install stuff on your mac
So, you want to install Maven. You can find Maven on Homebrew
Now you just have to open your terminal again and execute
brew install maven
After a few seconds brew will display the directory it installed Maven in (by default, all Homebrew packages are installed in /usr/local/Cellar/)
You are almost there, now you need to set the environment variable.
As described by Apache, the environment variable needs to be added to the PATH environment variable.
Here I have some uncertainty in terms of what file to use: .bash_profile or zprofile. The confusion is caused by the fact that in latest macOS update (maybe several latest updates) Apple decided to use zsh shell, and so .bash_profile became somewhat functionally equivalent to zprofile. There are many articles on the Internet about what file to use, so you better check them out before proceeding (sorry for not providing links).
On my Catalina 10.15.6 I use .zprofile and everything looks to work perfectly.
Once you decide what file to use, execute the following in the terminal:
nano .zprofile
An editor will open, write this:
export PATH=/usr/local/Cellar/maven/*version*/bin:$PATH
Press Control + X to exit, then Y to save changes and hit Enter to exit the editor
After that you will again be in terminal, where execute:
source .zprofile
To load it. Then close the terminal, open again and check that PATH variable is edited by executing:
env
That's it! Good luck coding!

For MacOs Catalina, below is what worked for me.
Terminal > Vim .zprofile
Add:
JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home
export JAVA_HOME
M2_HOME=/Users/username/apache-maven-3.8.1 (This is where my maven folder is.)
export M2_HOME
PATH=$PATH:$JAVA_HOME/bin:$M2_HOME/bin
export PATH
Terminal > source ~/.bash_profile
Restart Terminal > mvn -version
Output:
Apache Maven 3.8.1 (05c21c65bdfed0f71a2f2ada8b84da59348c4c5d)
Maven home: /Users/username/apache-maven-3.8.1
Java version: 1.8.0_281, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home/jre
Default locale: en_IN, platform encoding: UTF-8
OS name: "mac os x", version: "10.15.7", arch: "x86_64", family: "mac"
username#C02F ~ % java -version
java version "1.8.0_281"
Java(TM) SE Runtime Environment (build 1.8.0_281-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.281-b09, mixed mode)

Did you try executing:
source ~/.zshrc

Related

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?
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
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 :)
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.

macbook 10.15.5 install maven failed: NB: JAVA_HOME should point to a JDK not a JRE

I know there are a lot of questions/answers about this question, but all were failed in my case.
I installed jdk 1.8 and then installed maven3.6.3, java runs well but mvn command didn't work, see my command output and env:
java -version
java version "1.8.0_251"
Java(TM) SE Runtime Environment (build 1.8.0_251-b08)
Java HotSpot(TM) 64-Bit Server VM (build 25.251-b08, mixed mode)
mvn -v
The JAVA_HOME environment variable is not defined correctly
This environment variable is needed to run this program
NB: JAVA_HOME should point to a JDK not a JRE
mac OS version: 10.15.5
echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk1.8.0_251.jdk/Contents/Home
echo $PATH
/usr/local/apache-maven-3.6.3/bin:/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:.
config in ~/.bash_profile
export M2_HOME=/usr/local/apache-maven-3.6.3
PATH=$M2_HOME/bin:$JAVA_HOME/bin:$PATH:.
#Mac OSX 10.15.5 or later version need this configure
export JAVA_HOME=$(/usr/libexec/java_home)
export CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:.
export PATH
I also source ~./bash_profile to make configure effective.
I would suggest revert all the changes you have made in ~/.bash_profile. Checkout my way to make the maven work:
Download latest maven zip package apache-maven-<version>-bin.zip from Apache maven download page.
Unzip it to any place, e.g: ~/Develop/apache-maven-3.6.0
Create a link with command sudo ln -sf ~/Develop/apache-maven-3.6.0/bin/mvn /usr/local/bin/mvn (change version number per your case, if the local bin folder missing, just create it mkdir -p /usr/local/bin).
Check the installation mvn --version
Hope it works for you.
After hundreds updating configure file, now mvn -version can works, share my update and hope it can help you.
fill or update vim ~/.mavenrc(in the beginning, this file isn't exist)
# my MacOS is 10.15+
export Java_HOME=$(/usr/libexec/Java_home)
and then source ~/.mavenrc to let system env effective.
Summary:for my mac system, seems maven couldn't get JAVA_HOME from .bash_profile or .profile, so if you confirm your JAVA_HOME is set correctly and MAVEN_HOME configured, then you can have a try in this way.

Permanent Maven Installation

How can I make the maven installation permanent? After downloading the apache-maven to /usr/local, I need to print the series of "export" command lines every single time I want to use it. Even after I got the maven to work, maven doesn't work when I restart the terminal.
Added this to ~/.bash_profile:
export M2_HOME=/usr/local/apache-maven/apache-maven-3.2.1/
export PATH=$PATH:$M2_HOME/bin
Command-line:
$ export M2_HOME=/usr/local/apache-maven/apache-maven-3.2.1/
$ export M2=$M2_HOME/bin
$ export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/home
$ export PATH=$M2:$JAVA_HOME/bin:$PATH
$ mvn –-version
Apache Maven 3.2.1 (ea8b2b07643dbb1b84b6d16e1f08391b666bc1e9; 2014-02-14T12:37:52-05:00)
Maven home: /usr/local/apache-maven/apache-maven-3.2.1
Java version: 1.6.0_65, vendor: Apple Inc.
Java home: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
Default locale: en_US, platform encoding: MacRoman
OS name: "mac os x", version: "10.9.3", arch: "x86_64", family: "mac"
After restarting command-line:
$ mvn –-version
-bash: mvn: command not found
Instead of adding export PATH=$PATH:$M2_HOME/bin to ~/.bash_profile, I also tried adding the other export command-lines that sets M2, JAVA_HOME and PATH. This doesn't work.
After moving the application directory to /usr/local
Make a soft link in /usr/bin for universal access of mvn
sudo ln -s /usr/local/apache-maven-X.X.X/bin/mvn /usr/bin/mvn
Verify mvn installation
mvn --version
Since you are using Mac, I recommend that you install Homebrew (a package manager for OS X). Given that, you can install Maven using the brew command, e.g.
$ brew install maven
You need to make /etc/profile.d/maven.sh with the following content:
export M2_HOME=/opt/apache-maven-3.2.1
export M2=$M2_HOME/bin
export PATH=$M2:$PATH
Mac OSX
I found two neat tutorials:
1) mkyong
2) journaldev
Win7 You need to set up environment variables. In Win 7, in Start menu you can type Edit the system environment variables, then click Environment variables. In System variables, add 2 New variables:
M2_HOME=C:\Program Files\Apache Software Foundation\apache-maven-2.2.1 (this one is custom)
M2=%M2_HOME%\bin
After that, among the list of variables, find the one called Path, click Edit, and in the end, after this ; add %M2%;
If you did everything right, and if you have JAVA_HOME set up, then Maven should be permanent.
Do you try putting it in a .profile file instead of .bash_profile?
See What's the difference between .bashrc, .bash_profile, and .environment?
.profile is handled at login, so you need to logout and login for changes to become active.
In the Mac terminal, type below to open text editor
touch ~/.bash_profile
open ~/.bash_profile
Then in the editor, type:
export MAVEN_HOME=~/apache-maven-3.8.1
export PATH=$PATH:$MAVEN_HOME/bin
Close the editor and the contents will be saved. If you check the maven version, you will get response:
nisha$ mvn -version
Apache Maven 3.8.1 (05c21c65bdfed0f71a2f2ada8b84da59348c4c5d)
Maven home: /Users/nisha/apache-maven-3.8.1
Java version: 16.0.1, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk-16.0.1.jdk/Contents/Home
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "11.4", arch: "x86_64", family: "Mac"
Now, even if you close and open the terminal, you will be able to get the above output when you hit mvn -version

Maven not found in Mac OSX mavericks

After upgrading my Mac OSX 10.8 to 10.9, Maven not found in the /usr/share path but its installed in 10.8.
when I try this command:
$ maven -version
got this result
-bash: mvn: command not found
but it worked fine on 10.8? Any idea?
Maven is not installed any more by default on Mac OS X 10.9. You need to install it yourself, for example using Homebrew.
The command to install Maven using Homebrew is
brew install maven
if you don't want to install homebrew (or any other package manager) just for installing maven, you can grab the binary from their site:
http://maven.apache.org/download.cgi
extract the content to a folder (e.g. /Applications/apache-maven-3.1.1) with
$ tar -xvf apache-maven-3.1.1-bin.tar.gz
and finally adjust your ~/.bash_profile with any texteditor you like to include
export M2_HOME=/Applications/apache-maven-3.1.1
export PATH=$PATH:$M2_HOME/bin
restart the terminal and test it with
$ mvn -version
Apache Maven 3.1.1 (0728685237757ffbf44136acec0402957f723d9a; 2013-09-17 17:22:22+0200)
Maven home: /Applications/apache-maven-3.1.1
Java version: 1.6.0_65, vendor: Apple Inc.
Java home: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
Default locale: de_DE, platform encoding: MacRoman
OS name: "mac os x", version: "10.9", arch: "x86_64", family: "mac"
brew install maven
Please ensure that you've installed the latest Xcode and Command Line tools.
xcode-select --install
Download Maven from here.
Extract the tar.gz you just downloaded to the location you want (ex:/Users/admin/Maven).
Open the Terminal.
Type " cd " to go to your home folder.
Type "touch .bash_profile".
Type "open -e .bash_profile" to open .bash_profile in TextEdit.
Type the following in the TextEditor
alias mvn='/[Your file location]/apache-maven-x.x.x/bin/mvn'
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdkx.x.x_xx.jdk/Contents/Home/
(Make sure there are no speech marks or apostrophe's)
8. Make sure you fill the required data (ex your file location and version number).
Save your changes
Type ". .bash_profile" to reload .bash_profile and update any functions you add. (*make sure you separate the dots with a single space).
Type mvn -version
If successful you should see the following:
Apache Maven 3.1.1
Maven home: /Users/admin/Maven/apache-maven-3.1.1
Java version: 1.7.0_51, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.9.1", arch: "x86_64", family: "mac"
This solution could seem very long, but it's not. I just included many examples so that everything was clear. It worked for me in Mavericks OS.
Note: I combined and edited some of the answers shown above, added some examples and format and posted the result, so the credit goes mostly to the creators of the original posts.
Download Maven from here.
Open the Terminal.
Extract the file you just downloaded to the location you want, either manually or by typing the following lines in the Terminal (fill the required data):
mv [Your file name] [Destination location]/
tar -xvf [Your file name]
For example, if our file is named "apache-maven-3.2.1-bin.tar" (Maven version 3.2.1) and we want to locate it in the "/Applications" directory, then we should type the following lines in Terminal:
mv apache-maven-3.2.1-bin.tar /Applications/
tar -xvf apache-maven-3.2.1-bin.tar
If you don't have any JDK (Java Development Kit) installed on your computer, install one.
Type "java -version" in Terminal. You should see something like this:
java version "1.8.0"
Java(TM) SE Runtime Environment (build 1.8.0-b132)
Java HotSpot(TM) 64-Bit Server VM (build 25.0-b70, mixed mode)
Remember your java version (in the example, 1.8.0).
Type "cd ~/" to go to your home folder.
Type "touch .bash_profile".
Type "open -e .bash_profile" to open .bash_profile in TextEdit.
Type the following in TextEdit (copy everything and replace the required data):
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk[Your Java version].jdk/Contents/Home
export M2_HOME=[Your file location]/apache-maven-[Your Maven version]/
export PATH=$PATH:$M2_HOME/bin
alias mvn='$M2_HOME/bin/mvn'
For example, in our case we would replace "[Your Java version]" with "1.8.0" (value got in step 5), "[Your file location]" with "/Applications" (value used as "Destination Location" in step 3) and "[Your Maven version]" with "3.2.1" (Maven version observed in step 3), resulting in the following code:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home
export M2_HOME=/Applications/apache-maven-3.2.1/
export PATH=$PATH:$M2_HOME/bin
alias mvn='$M2_HOME/bin/mvn'
Save your changes
Type "source .bash_profile" to reload .bash_profile and update any functions you add.
Type mvn -version. If successful you should see the following:
Apache Maven [Your Maven version] ([Some weird stuff. Don't worry about this])
Maven home: [Your file location]/apache-maven-[Your Maven version]
Java version: [You Java version], vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk[Your Java version].jdk/Contents/Home/jre
[Some other stuff which may vary depending on the configuration and the OS of the computer]
In our example, the result would be the following:
Apache Maven 3.2.1 (ea8b2b07643dbb1b84b6d16e1f08391b666bc1e9; 2014-02-14T18:37:52+01:00)
Maven home: /Applications/apache-maven-3.2.1
Java version: 1.8.0, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0</b>.jdk/Contents/Home/jre
Default locale: es_ES, platform encoding: UTF-8
OS name: "mac os x", version: "10.9.2", arch: "x86_64", family: "mac"
For me trying above techniques did work so I opened .bash_profile file and added following line in new line to connect to maven using short cmd :
alias mvn=/opt/apache-maven-3.6.3/bin/mvn
Restart your terminal and hit mvn clean install cmd
I am not allowed to comment to pkyeck's response which did not work for a few people including me, so I am adding a separate comment in continuation to his response:
Basically try to add the variable which he has mentioned in the .profile file if the .bash_profile did not work. It is located in your home directory and then restart the terminal. that got it working for me.
The obvious blocker would be that you do not have an access to edit the .profile file, for which use the "touch" to check the access and the "sudo" command to get the access
touch .profile
vi .profile
Here are the variable pkyeck suggests that we added as a solution which worked with editing .profile for me:
export M2_HOME=/apache-maven-3.3.3
export PATH=$PATH:$M2_HOME/bin
For me I had a AdoptOpenJDK 8 installed, instead of SE JDK 8. For which it was not able to recognize JAVA_HOME or mvn commands.
Check your java_home
/usr/libexec/java_home -V
and use JAVA SE SDK if it is different.
Then follow the above steps to install maven and check again

Maven Install on Mac OS X

I'm trying to install maven through the terminal by following these instructions.
So far I got this:
export M2_HOME=/user/apple/apache-maven-3.0.3
export M2=$M2_HOME/bin
export PATH=$M2:$PATH
export JAVA_HOME=/usr/java/jdk1.6.0_22
Where do you go to find this?
Alternatively, I recommend installing Homebrew for these kinds of utilities.
Then you just install Maven using:
brew install maven
PS: If you got a 404 error, try doing a brew update just before
Disclaimer: Here is a complete answer taking the last version of OS X (10.9 AKA Mavericks) into account. I am aware that everything I compiled in this answer is already present in the page, but having it clearly in one answer makes it a lot clearer.
First of all, with previous versions of OS X, Maven is installed by default. If Java is missing running you#host:~ $ java in a terminal will prompt you for the Java installation.
With Mac OS X 10.9 (Mavericks), Maven is not installed by default anymore. Different options are then possible:
Using Homebrew:
you#host:~$ brew install maven will install latest Maven (3.5.2 on 02/01/2018)
you#host:~$ brew install maven30 will install Maven 3.0 if needed
Using Macports: (I did not test this)
you#host:~$ sudo port install maven will install latest Maven (?)
or:
you#host:~$ sudo port install maven3 will Install Maven 3.0
you#host:~$ sudo port select --set maven maven3 selects that version of Maven
Installing by hand:
Download Maven from its homepage
Follow the installation instructions:
Extract the distribution archive, i.e.apache-maven-3.3.9-bin.tar.gz to the directory you wish to install Maven 3.3.9. The subdirectory apache-maven-3.3.9 will be created from the archive.
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.
Make sure that JAVA_HOME is set to the location of your JDK, e.g. export JAVA_HOME=$(/usr/libexec/java_home -v 1.8) and that $JAVA_HOME/bin is in your PATH environment variable (although that might not be necessary with the latest Mac OS X versions and the Oracle JDK).
Add extracted apache-maven-3.3.9/bin to your $PATH
Run mvn --version to verify that it is correctly installed.
OS X prior to Mavericks (10.9) actually comes with Maven 3 built in.
If you're on OS X Lion, you won't have java installed by default. Run java by itself and it'll prompt you to install it.
Assuming qualifications are met, run mvn -version and see some output like this:
Apache Maven 3.0.3 (r1075438; 2011-02-28 12:31:09-0500)
Maven home: /usr/share/maven
Java version: 1.6.0_29, vendor: Apple Inc.
Java home: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
Default locale: en_US, platform encoding: MacRoman
OS name: "mac os x", version: "10.7.2", arch: "x86_64", family: "mac"
When I upgraded recently to OS X Mavericks and my maven builds start failing. So I needed to install maven again as it doesn't come built in. Then I tried with the command:
brew install maven
it works, but it installs the version 3.1.1 of maven which causes some problems for a few users like (https://cwiki.apache.org/confluence/display/MAVEN/AetherClassNotFound). So if you're running into the same issue you will probably want to install the earlier Maven version, the 3.0.5. To do that with Homebrew, you have to execute the following command:
brew install https://raw.github.com/Homebrew/homebrew-versions/master/maven30.rb
That's it, it will then use a different Homebrew's formulae which will give you the maven 3.0.5 instead.
macOS Sierra onwards
brew install maven
To install Maven on OS X, go to the Apache Maven website and download the binary zip file.
You can then shift the apache-maven-3.0.5 folder in your Downloads folder to wherever you want to keep Maven; however as the rest of the process involves the command line, I recommend you do everything from there.
At the command line, you would run something like:
mv ~/Downloads/apache-maven-3.0.5 ~/Development/
This is just my personal preference - to have a "Development" directory in my home directory. You can choose something else if you wish.
Next, edit ~/.profile in the editor of your choice, and add the following:
export M2_HOME="/Users/johndoe/Development/apache-maven-3.0.5"
export PATH=${PATH}:${M2_HOME}/bin
The first line is important to Maven (and must be a full explcit path); the second line is important to the shell, in order to run the "mvn" binary. If you have a variation of that second line already in .profile, then simply add ${M2_HOME}/bin to the end of it.
Now open a second terminal window and run
mvn -version
which should give output like...
Apache Maven 3.0.5 (r01de14724cdef164cd33c7c8c2fe155faf9602da; 2013-02-19 13:51:28+0000)
Maven home: /Users/johndoe/Development/apache-maven-3.0.5
Java version: 1.7.0_40, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.7.0_40.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.9", arch: "x86_64", family: "mac"
Couple of things to note:
If you've installed the Oracle JDK 1.7, then you may find Maven reports JDK 1.6 in the above output. To solve this, add the following to your ~/.profile:
export JAVA_HOME=$(/usr/libexec/java_home)
As some have pointed out, Maven has historically been supplied either with OS X itself, or with the optional Command Line Tools for XCode. This may cease to be the case for future versions of OS X, and in fact OS X Mavericks does not include Maven. Personal opinion: This could be because they are still in beta, or it could be that Apple have taken a look at the latest Thoughtworks Technology Radar, and spotted that Maven has been moved to "Hold".
If using MacPorts on OS X 10.9 Mavericks, you can simply do:
sudo port install maven3
sudo port select --set maven maven3
A simple approach to install Maven.
Open Terminal
Finder -> Go -> Utilities -> Terminal
Install Homebrew using the below command
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
After that install maven
brew install maven
Just a brief addition; if you want to install a specific version on MacOS using Homebrew 1.5.2, you can install it the following way:
brew update
brew search maven
This will give you maven versions available in homebrew
brew install maven#3.3
[If you want to install maven 3.3.]
Two Method
(use homebrew) Auto install:
Command:
brew install maven
Pros and cons
Pros: easy
Cons: (probably) not latest version
Manually install (for latest version):
Pros and cons
Pros: use your expected any (or latest) version
Cons: need self to do it
Steps
download latest binary (apache-maven-3.6.3-bin.zip) version from Maven offical download
uncompress it (apache-maven-3.6.3-bin.zip) and added maven path into environment variable PATH
normally is edit and add:
export PATH=/path_to_your_maven/apache-maven-3.6.3/bin:$PATH
into your startup script( ~/.bashrc or ~/.zshrc etc.)
Extra Note
how to take effect immediately and check installed correctly?
A:
source ~/.bashrc
echo $PATH
which mvn
mvn --version
here output:
➜ bin pwd
/Users/crifan/dev/dev_tool/java/maven/apache-maven-3.6.3/bin
➜ bin ll
total 64
-rw-r--r--# 1 crifan staff 228B 11 7 12:32 m2.conf
-rwxr-xr-x# 1 crifan staff 5.6K 11 7 12:32 mvn
-rw-r--r--# 1 crifan staff 6.2K 11 7 12:32 mvn.cmd
-rwxr-xr-x# 1 crifan staff 1.5K 11 7 12:32 mvnDebug
-rw-r--r--# 1 crifan staff 1.6K 11 7 12:32 mvnDebug.cmd
-rwxr-xr-x# 1 crifan staff 1.5K 11 7 12:32 mvnyjp
➜ bin vi ~/.bashrc
➜ bin source ~/.bashrc
➜ ~ echo $PATH
/Users/crifan/dev/dev_tool/java/maven/apache-maven-3.6.3/bin:xxx
➜ bin which mvn
/Users/crifan/dev/dev_tool/java/maven/apache-maven-3.6.3/bin/mvn
➜ bin mvn --version
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /Users/crifan/dev/dev_tool/java/maven/apache-maven-3.6.3
Java version: 1.8.0_112, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home/jre
Default locale: zh_CN, platform encoding: UTF-8
OS name: "mac os x", version: "10.14.6", arch: "x86_64", family: "mac"
for full detail please refer my (Chinese) post: 【已解决】Mac中安装Gradle
This command brew install maven30 didn't work for me. Was complaining about a missing FORMULA. But the following command did work. I've got maven-3.0.5 installed.
brew install homebrew/versions/maven30
This is for Mac OS X 10.9 aka Mavericks.
If you don't want to install Homebrew only for install Maven you could simply do this:
Download the binary Maven and extract the zip
Launch the Terminal and type this command:
sudo ln -s /path_to_maven_folder/bin/mvn /usr/bin/mvn
You can find more details on this post.
Open terminal
Just use brew command to install maven
brew install maven
After the download and install finished. Check for the maven version
mvn -version
Here you go !!! Now you have successfully installed maven on your mac os.
If you have tried brew install maven and were greeted with missing gcc compiler and some other dependencies, an easier approach is to install sdkman
and then run
sdk install maven
(or refer to the latest documentation for the right command)
sdkman is probably over-qualified for the job, but if you deal with multiple versions of SDKs, it's a pretty nice tool to have in general.
Credits to Ammar for the excellent tip
brew install maven31 (if you have homebrew)
for the ones that just migrated to mavericks - I used the *-ux solution;
download maven from apache maven site
put in /opt
modified .bash_profile and added:
alias mvn='/opt/apache-maven-3.1.1/bin/mvn'
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_21.jdk/Contents/Home
Open a TERMINAL window and check if you have it already installed.
Type:
$ mvn –version
And you should see:
Apache Maven 3.0.2 (r1056850; 2011-01-09 01:58:10+0100)
Java version: 1.6.0_24, vendor: Apple Inc.
Java home: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
Default locale: en_US, platform encoding: MacRoman
OS name: “mac os x”, version: “10.6.7″, arch: “x86_64″, family: “mac”
If you don't have Maven installed already, then here is how to download and install maven, and configure environment variables on Mac OS X:
http://bitbybitblog.com/install-maven-mac/
On Maverick, run in the terminal
xcode-select --install
to install the Command Line Tools.
You can use Maven Version Manager through which you can use multiple version of Maven per directory base.
Installation
Using Homebrew brew install mvnvm
Without Homebrew mkdir -p ~/bin && curl -s https://bitbucket.org/mjensen/mvnvm/raw/master/mvn > ~/bin/mvn && chmod 0755 ~/bin/mvn and add ~/bin to path.
Usage
Default Version
To set default maven version set the environment variable DEFAULT_MVN_VERSION to the maven version to be used by default.
Maven version for the folder
Create a file named mvnvm.properties in the folder and configure the maven version as follows
mvn_version=<maven version>
Two ways to install Maven
Before installing maven
check mvn -version to make sure maven is not install in system
Method 1:
brew install maven
Method 2:
go to https://maven.apache.org/download.cgi
Select any of Binary link
Unzip the link
Move to application folder
Update .bash profile with exports
run mvn -version
This worked for me:
$ vim .bash_profile
export JAVA_HOME=$(/usr/libexec/java_home)
$ source .bash_profile
credit: http://www.mkyong.com/java/maven-java_home-is-not-defined-correctly-on-mac-osx/
% sudo port selfupdate;
% sudo port upgrade outdated;
% sudo port install maven3;
% sudo port select --set maven maven3;
— add following to .zshenv -- start using zsh if you dont —
set -a
[[ -d /opt/local/share/java/maven3 ]] &&
M3_HOME=/opt/local/share/java/maven3 &&
M2_HOME=/opt/local/share/java/maven3 &&
MAVEN_OPTS="-Xmx1024m" &&
M2=${M2_HOME}/bin
set +a
You can install maven using homebrew. The command is
$ brew install maven
After installing maven using brew or manually, using macOS Catalina and using the terminal or iTerm to operate maven you will need to grant access to the apps to access user files.
System Preferences -> Privacy (button) -> Full Disk Access
And then add terminal or iTerm to that list.
You will also need to restart your application e.g. terminal or iTerm after giving them full disk access.
For those who wanna use maven2 in Mavericks, type:
brew tap homebrew/versions
brew install maven2
If you have already installed maven3, backup 3 links (mvn, m2.conf, mvnDebug) in /usr/local/bin first:
mkdir bak
mv m* bak/
then reinstall:
brew uninstall maven2(only when conflicted)
brew install maven2
This worked for me. Its simpler and cleaner.
Open Mac terminal and type:
export MAVEN_HOME=~/apache-maven-3.8.1
export PATH=$PATH:$MAVEN_HOME/bin
Now when you type
mvn -version
You get an output:
Apache Maven 3.8.1 (05c21c65bdfed0f71a2f2ada8b84da59348c4c5d)
Maven home: /Users/nisha/apache-maven-3.8.1
Java version: 16.0.1, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk-16.0.1.jdk/Contents/Home
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "11.4", arch: "x86_64", family: "Mac"
** Edited to add:
When you close the terminal and open again, you will get mvn: command not found and you will have to run the command again.
Refer this post for a permanent installation

Resources