Search code examples
javamavenfish

Why does Maven use the wrong JDK version when running from Fish?


I run macOS Sierra, and have installed the Fish shell (https://fishshell.com/).

When I try to build a Maven project, it now gives me

Fatal error compiling: invalid target release: 1.8 ->

... which means that it has picked up an older JDK which it tries to use to build my Java 8 project with.

This is made obvious by running mvn -version:

...
Java version: 1.6.0_65, vendor: Apple Inc.
Java home: /Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
...

But I have JAVA_HOME set correctly, echo $JAVA_HOME in both shells give me:

/Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home

and java -version gives me

java version "1.8.0_112"
Java(TM) SE Runtime Environment (build 1.8.0_112-b16)

and javac -version:

javac 1.8.0_112

echo $PATHgives the same result in both shells (except that in Bash, the directories are separated by :; in Fish, a space ):

/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/Server.app/Contents/ServerRoot/usr/bin:/Applications/Server.app/Contents/ServerRoot/usr/sbin

I have configured maven-compiler-plugin to for 1.8:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <version>3.6.0</version>
  <configuration>
    <source>1.8</source>
    <target>1.8</target>
    <encoding>${project.build.sourceEncoding}</encoding>
  </configuration>
</plugin>

Why does Maven insist on using the 1.6 JDK? The same project builds fine, with the correct 1.8 JDK, when running through Bash.


Solution

  • If you are running an old version of maven, please try updating to the latest version (3.5.0), e.g. via homebrew (brew install maven).