How to change mvn version for MAC - rahul00773/JavaConcepts GitHub Wiki

Recently installed Java8. Once I did this and tried running existing projects which use java8.

To my surprise, maven began using java14 as its default java version, even though my JAVA_HOME is set to use java8 — /usr/libexec/java_home -v 1.8 So confused and confounded, I turn to Googling…

From Googling, I learn maven runs using the latest java installation. Now this is configured in M2_HOME/bin/mvn Given, my installation was done using brew — Brew is simply a package manager for Mac OS, my M2_HOME was automatically set up. This is usually in /usr/local/Cellar/maven/3.5.4 With this new found knowledge, I run nano /usr/local/Cellar/maven/3.5.4/bin/mvn

The content was #!/bin/bash JAVA_HOME="${JAVA_HOME:-$(/usr/libexec/java_home)}" exec "/usr/local/Cellar/maven/3.5.4/libexec/bin/mvn" "$@"

Important bit being "${JAVA_HOME:-$(/usr/libexec/java_home)}"

To resolve this, you will need to specify the java version you need maven to default to. So in my case, I needed java8.

Update /usr/local/Cellar/maven/3.5.4/bin/mvnfile as follows

#!/bin/bash JAVA_HOME="${JAVA_HOME:-$(/usr/libexec/java_home -v 1.8)}" exec "/usr/local/Cellar/maven/3.5.4/libexec/bin/mvn" "$@"

Now run mvn -v, you should see maven uses java8 as its default JDK.