How to switch between maven version in mac - macos

I have maven3.1.1 and maven3.2.5 installed on my mac.
When I do mvn -version it shows maven3.1.1
How can I switch to the other maven version 3.2.5 ?

Your Current bash profile would be pointing to the maven version 3.1.1 as shown below
~/.bash_profile
export M2_HOME=/Users/yourusername/apache-maven-3.1.1 (Path where you have installed maven 3.1.1)
export PATH=$PATH:$M2_HOME/bin
Change the bash profile as shown below
~/.bash_profile
export M2_HOME=/Users/yourusername/apache-maven-3.2.5 (Maven 3.2.5 path)
export PATH=$PATH:$M2_HOME/bin
Restart the terminal and hit command mvn -version and now it will be pointing to the latest version.

Using SDKMan might be a simpler way to switch between multiple Maven versions.
1) Install SDKMan which is a one-off thing: http://sdkman.io/install.html
2) Do sdk install maven yourVersion
3) To switch: sdk install maven anotherVersion
4) To switch again: sdk use maven yourVersion
SDKMan should simplify such operations for several other software frameworks like Java, Groovy, Spring, Gradle, etc.

Another way to create a soft link. Suppose you have two version like -
maven-v1 and
maven-v2
Now if you want to use maven-v1 then create a soft link -
ln -s /installation/directory/of/maven-v1 maven
Then in your .bash_profile export M2_HOME pointing this directory -
export M2_HOME=/path/to/maven
export PATH=$PATH:$M2_HOME/bin
If you are doing this at first time then you need to restart your terminal.But after that you just need to change the soft link to you expected version. Suppose, now you want to use maven-v2; just change the existing soft link -
ln -sf /installation/directory/of/maven-v2 maven
Now you don't need to restart the terminal.

In your ~/.bash_profile
Download the tar of the maven version you want to use and extract the contents you want to use. Example Maven 3.1.1
export M2_HOME=/Users/<path to maven>/apache-maven-3.1.1
export PATH=$PATH:$M2_HOME/bin
If you have zsh, then change the ~/.zprfile

do not use M2_HOME. directly go for export PATH=$PATH:/usr/{maven...path}/bin

I've developed a function for ~/.zshrc and ~/.bashrc files to choose a different maven version according to the project without having to install more maven versions but taking advantage of docker.
So I use my maven installed in my machine with: mvn clean install
And I use my maven in docker with: maven jdk8 clean install or maven jdk11 package for example.
Here the function:
# executes a containerized version of maven so you won't have to install to your computer.
# you need to have docker installed and running.
# you can specify 'jdk8' or 'jdk11' as first argument to switch jdk.
# if not specified, jdk11 is the default.
# usage example:
# /your/maven/project/directory - $ maven jdk8 clean install
# /your/maven/project/directory - $ maven clean package
maven() {
if [[ "$1" == "jdk8" ]]; then
docker run --rm -v $PWD:/usr/src/app -v $HOME/.m2:/root/.m2 -w /usr/src/app maven:3.8-adoptopenjdk-8 mvn "${#:2}"
elif [[ "$1" == "jdk11" ]]; then
docker run --rm -v $PWD:/usr/src/app -v $HOME/.m2:/root/.m2 -w /usr/src/app maven:3.8-eclipse-temurin-11 mvn "${#:2}"
else
docker run --rm -v $PWD:/usr/src/app -v $HOME/.m2:/root/.m2 -w /usr/src/app maven:3.8-eclipse-temurin-11 mvn "$#"
fi
}

Related

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.

Cannot build dockerfile with sdkman

I am entirely new to the concept of dockers. I am creating the following Dockerfile as an exercise.
FROM ubuntu:latest
MAINTAINER kesarling
RUN apt update && apt upgrade -y
RUN apt install nginx curl zip unzip -y
RUN apt install openjdk-14-jdk python3 python3-doc clang golang-go gcc g++ -y
RUN curl -s "https://get.sdkman.io" | bash
RUN bash /root/.sdkman/bin/sdkman-init.sh
RUN sdk version
RUN yes | bash -c 'sdk install kotlin'
CMD [ "echo","The development environment has now been fully setup with C, C++, JAVA, Python3, Go and Kotlin" ]
I am using SDKMAN! to install Kotlin. The problem initially was that instead of using RUN bash /root/.sdkman/bin/sdkman-init.sh, I was using RUN source /root/.sdkman/bin/sdkman-init.sh. However, it gave the error saying source not found. So, I tried using RUN . /root/.sdkman/bin/sdkman-init.sh, and it did not work. However, RUN bash /root/.sdkman/bin/sdkman-init.sh seems to work, as in does not give any error and tries to run the next command. However, the docker then gives error saying sdk: not found
Where am I going wrong?
It should be noted that these steps worked like charm for my host distribution (The one on which I'm running docker) which is Pop!_OS 20.04
Actually the script /root/.sdkman/bin/sdkman-init.sh sources the sdk
source is a built-in to bash rather than a binary somewhere on the filesystem.
source command executes the file in the current shell.
Each RUN instruction will execute any commands in a new layer on top of the current image and commit the results.
The resulting committed image will be used for the next step in the Dockerfile.
Try this:
FROM ubuntu:latest
MAINTAINER kesarling
RUN apt update && apt upgrade -y
RUN apt install nginx curl zip unzip -y
RUN apt install openjdk-14-jdk python3 python3-doc clang golang-go gcc g++ -y
RUN curl -s "https://get.sdkman.io" | bash
RUN /bin/bash -c "source /root/.sdkman/bin/sdkman-init.sh; sdk version; sdk install kotlin"
CMD [ "echo","The development environment has now been fully setup with C, C++, JAVA, Python3, Go and Kotlin" ]
SDKMAN in Ubuntu Dockerfile
tl;dr
the sdk command is not a binary but a bash script loaded into memory
Shell sessions are a "process", which means environment variables and declared shell function only exist for the duration that shell session exists; which lasts only as long as the RUN command.
Manually tweak your PATH
RUN apt-get update && apt-get install curl bash unzip zip -y
RUN curl -s "https://get.sdkman.io" | bash
RUN source "$HOME/.sdkman/bin/sdkman-init.sh" \
&& sdk install java 8.0.275-amzn \
&& sdk install sbt 1.4.2 \
&& sdk install scala 2.12.12
ENV PATH=/root/.sdkman/candidates/java/current/bin:$PATH
ENV PATH=/root/.sdkman/candidates/scala/current/bin:$PATH
ENV PATH=/root/.sdkman/candidates/sbt/current/bin:$PATH
Full Version
Oh wow this was a journey to figure out. Below each line is commented as to why certain commands are run.
I learnt a lot about how unix works and how sdkman works and how docker works and why the intersection of the three give very unusual behaviour.
# I am using a multi-stage build so I am just copying the built artifacts
# from this stage to keep final image small.
FROM ubuntu:latest as ScalaBuild
# Switch from `sh -c` to `bash -c` as the shell behind a `RUN` command.
SHELL ["/bin/bash", "-c"]
# Usual updates
RUN apt-get update && apt-get upgrade -y
# Dependencies for sdkman installation
RUN apt-get install curl bash unzip zip -y
#Install sdkman
RUN curl -s "https://get.sdkman.io" | bash
# FUN FACTS:
# 1) the `sdk` command is not a binary but a bash script loaded into memory
# 2) Shell sessions are a "process", which means environment variables
# and declared shell function only exist for
# the duration that shell session exists
RUN source "$HOME/.sdkman/bin/sdkman-init.sh" \
&& sdk install java 8.0.275-amzn \
&& sdk install sbt 1.4.2 \
&& sdk install scala 2.12.12
# Once the real binaries exist these are
# the symlinked paths that need to exist on PATH
ENV PATH=/root/.sdkman/candidates/java/current/bin:$PATH
ENV PATH=/root/.sdkman/candidates/scala/current/bin:$PATH
ENV PATH=/root/.sdkman/candidates/sbt/current/bin:$PATH
# This is specific to running a minimal empty Scala project and packaging it
RUN touch build.sbt
RUN sbt compile
RUN sbt package
FROM alpine AS production
# setup production environment image here
COPY --from=ScalaBuild /root/target/scala-2.12/ $INSTALL_PATH
ENTRYPOINT ["java", "-cp", "$INSTALL_PATH", "your.main.classfile"]
Generally you want to avoid using "version manager" type tools in Docker; it's better to install a specific version of the compiler or runtime you need.
In the case of Kotlin, it's a JVM application distributed as a zip file so it should be fairly easy to install:
FROM openjdk:15-slim
ARG KOTLIN_VERSION=1.3.72
# Get OS-level updates:
RUN apt-get update \
&& apt-get install --no-install-recommends --assume-yes \
curl \
unzip
# and if you need C/Python dependencies, those too
# Download and unpack Kotlin
RUN cd /opt \
&& curl -LO https://github.com/JetBrains/kotlin/releases/download/v${KOTLIN_VERSION}/kotlin-compiler-${KOTLIN_VERSION}.zip \
&& unzip kotlin-compiler-${KOTLIN_VERSION}.zip \
&& rm kotlin-compiler-${KOTLIN_VERSION}.zip
# Add its directory to $PATH
ENV PATH=/opt/kotlinc/bin:$PATH
The real problem with version managers is that they heavily depend on the tool setting environment variables. As #JeevanRao notes in their answer, each Dockerfile RUN command runs in a separate shell in a separate container, and any environment variable settings within that command get lost for the next command.
# Does absolutely nothing: environment variables do not stay set
RUN . /root/.sdkman/bin/sdkman-init.sh
Since an image generally contains only one application and its runtime, you don't need the ability to change which version of the runtime or compiler you're using. My Dockerfile example passes it as an ARG, so you can change it in the Dockerfile or pass a docker build --build-arg KOTLIN_VERSION=... option to use a different version.

install maven on mac

i'm trying to install maven on my mac first i downloaded apache-maven-3.3.9 and then i entered this code on my bash:
export M2_HOME=$(/usr/local/apache-maven/apache-maven-3.3.9)
and
export PATH=${PATH}:/Users/sabrine/ant/bin:$JAVA_HOME/bin:$ANT_HOME/bin:$M2_HOME/bin:
but it doesn't work i got(i tried with mvn -v)
-bash: mvn: command not found
where is the problem? what should i do?
Please follow below steps on terminal
Create bash profile (will be created under home folder and will be hidden file)
$ touch .bash_profile
Edit bash profile in editor and put next 2 commands in file, save file.
export M2_HOME=/Users/srajp/apache-maven-3.5.3
export PATH=$PATH:/Users/srajp/apache-maven-3.5.3/bin
Then check Maven version
$ mvn --version
If needed please see detailed steps on my blog on How to install Maven on MAC OS?
Don't add $( ) around the paths, just use:
export M2_HOME=/usr/local/apache-maven/apache-maven-3.3.9
Followed by yours:
export PATH=${PATH}:/Users/sabrine/ant/bin:$JAVA_HOME/bin:$ANT_HOME/bin:$M2_HOME/bin:
The above assumes that your maven installation is in /usr/local/apache-maven/apache-maven-3.3.9, and ls -ld /usr/local/apache-maven/apache-maven-3.3.9 gives non-error output.
You can install maven on mac using this command.
brew install maven

Couldn't get maven version

I'm using Ubuntu and new to maven i started to install maven 3 with
wget http://archive.apache.org/dist/maven/maven-3/
3.1.0/binaries/apache-maven-3.1.0-bin.tar.gz
i need to install this version to be convenient with JDK6 after that i extracted this in /opt
then edited in .bashrc
export MVN_HOME="/tmp/apache-maven-3.1.0"
export PATH="$PATH:$MVN_HOME/bin"
i tested mvn -version but got
The program 'mvn' can be found in the following packages:
* maven
* maven2
Try: sudo apt-get install <selected package>
Have you checked which directories are already in $PATH and contain a mvn ?
Change your export statement to
export PATH="$MVN_HOME/bin:$PATH"
in order to make sure that your mvn comes first. Besides that you might want to scan for variants of mvn that are already on your system:
find / -name mvn

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