In this tutorial, we’ll explain why Maven might use a different version of Java than the default one set in the system. Additionally, we’ll show where Maven’s configuration files are located. Then, we’ll explain how to configure the Java version in Maven.
2. Maven Configuration
First, let’s have a look at a possible system configuration, where Maven uses a different version of Java than the default one set in the system. The Maven configuration returns:
$ mvn -v
Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-10T17:41:47+01:00)
Maven home: C:\Users\test\apps\maven\3.3.9
Java version: 11.0.10, vendor: Oracle Corporation
Java home: C:\my\java\jdk-11.0.10
Default locale: pl_PL, platform encoding: Cp1250
OS name: "windows 10", version: "10.0", arch: "amd64", family: "dos"
As we can see, it returns the Maven version, Java version, and OS information. The Maven tool uses JDK version 11.0.10.
Let’s now have a look at the Java version set in our system:
$ java -version
java version "13.0.2" 2020-01-14
Java(TM) SE Runtime Environment (build 13.0.2+8)
Java HotSpot(TM) 64-Bit Server VM (build 13.0.2+8, mixed mode, sharing)
The default JDK is set to 13.0.2. In the following sections, we’ll explain why it doesn’t match the one used by Maven.
3. Setting Up JAVA_HOME Globally
Let’s have a look at the default setup. Above all, the JAVA_HOME variable is a mandatory Maven configuration. Moreover, when it’s not set up, the mvn command returns an error message:
Error: JAVA_HOME not found in your environment.
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation.
On Linux, we set the system variable with the export command. Windows has dedicated system variable settings for that purpose. When it is set globally, Maven uses the default version of Java set in the system.
4. Maven Configuration Files
Let’s now have a quick look at where to find Maven configuration files. There are a few places where the configuration can be provided:
- .mavenrc/mavenrc_pre.cmd – user-defined script located in user’s home directory
- settings.xml – file located in ~/.m2 directory, which contains configuration across projects
- .mvn – directory with configuration located within the project
Additionally, we may use MAVEN_OPTS environment variable to set up JVM startup parameters.
5. Setting Up JAVA_HOME Only for Maven
We saw that the Java version differs from the one used by Maven. In other words, Maven overwrites the default configuration provided with the JAVA_HOME variable.
It can be set in a user-defined script executed at the beginning of the mvn command. In Windows, we set it in the %HOME%\mavenrc_pre.bat or %HOME%\mavenrc_pre.cmd file. Maven supports both ‘.bat’ and ‘.cmd’ files. In the file, we simply set the JAVA_HOME variable:
On the other hand, Linux has the $HOME/.mavenrc file for the same purpose. Here, we set the variable almost the same way:
Thanks to that setup, Maven uses JDK 11, even though the default one in the system is JDK 13.
We can skip the execution of the user-defined script with the MAVEN_SKIP_RC flag.
Additionally, we could set the variable directly in Maven’s executable file. However, such an approach is not recommended as it would not be applied automatically in case we upgrade Maven to a higher version.
In this short article, we explained how Maven might use a different Java version than the default one. Then, we showed where Maven’s configuration files are located. Finally, we explained how to set up the Java version for Maven.