Recently installed Java10 to see whats new and fun about it. Once I did this and tried running existing projects which use java8.
To my surprise, maven began using java10 as its default java version, even though my JAVA_HOME is set to use java8 — /usr/libexec/java_home -v 1.8
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/mvn
file 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" "$@"