Java
Maven
Programming
Software Development
Version Control

How to set a specific Java version for Maven

Codemia Knowledge Hub ·

Maven builds with the JDK that the JAVA_HOME environment variable points to, or the java on your PATH if JAVA_HOME is not set. So to change Maven's Java version, point JAVA_HOME at the JDK you want. Inside a project, set the language level with maven-compiler-plugin, and use Maven toolchains when different projects need different JDKs without touching JAVA_HOME.

Set JAVA_HOME

Windows:

  1. Right-click My Computer and select Properties.
  2. Open Advanced system settings and click Environment Variables.
  3. Under System Variables, click New and add:
    • Name: JAVA_HOME
    • Value: the JDK path (e.g., C:\Program Files\Java\jdk1.8.0_261)

Linux/Mac:

  1. Open Terminal.
  2. Add this line to your shell configuration file (.bashrc, .bash_profile, .zshrc, etc.):
bash
   export JAVA_HOME=/usr/lib/jvm/java-8-oracle
  1. Reload it with source ~/.bashrc or open a new terminal session.

Set source and target in maven-compiler-plugin

For a project-level setting, configure maven-compiler-plugin in pom.xml:

xml
1<project>
2  ...
3  <build>
4    <plugins>
5      <plugin>
6        <groupId>org.apache.maven.plugins</groupId>
7        <artifactId>maven-compiler-plugin</artifactId>
8        <version>3.8.1</version>
9        <configuration>
10          <source>1.8</source> <!-- Java version to compile source files -->
11          <target>1.8</target> <!-- Java version for compiled classes -->
12        </configuration>
13      </plugin>
14    </plugins>
15  </build>
16  ...
17</project>

source sets the Java language level the code is compiled as, and target sets the bytecode version of the compiled classes. These do not switch the JDK: the compilation still runs on the JDK Maven itself runs on (from JAVA_HOME), so that JDK must be able to produce the requested level.

Use toolchains for multiple JDKs

Maven toolchains let a project pick a JDK without changing JAVA_HOME each time. Define the JDKs in toolchains.xml, usually in the Maven .m2 directory:

xml
1<toolchains>
2    <toolchain>
3        <type>jdk</type>
4        <provides>
5            <version>1.8</version>
6            <vendor>oracle</vendor>
7        </provides>
8        <configuration>
9            <jdkHome>/path/to/jdk1.8.0</jdkHome>
10        </configuration>
11    </toolchain>
12</toolchains>

Then reference it from pom.xml with the compiler plugin's jdkToolchain parameter (maven-compiler-plugin 3.6 or later):

xml
1<project>
2  ...
3  <build>
4    <pluginManagement>
5      <plugins>
6        <plugin>
7          <groupId>org.apache.maven.plugins</groupId>
8          <artifactId>maven-compiler-plugin</artifactId>
9          <configuration>
10            <jdkToolchain>
11              <version>1.8</version> <!-- Matches <provides> in toolchains.xml -->
12            </jdkToolchain>
13          </configuration>
14        </plugin>
15      </plugins>
16    </pluginManagement>
17  </build>
18  ...
19</project>

Which approach to use

MethodWhat it doesWhen to use it
JAVA_HOME environment variablePoints Maven at the desired JDK, system-wide or per session.A single project, or several projects that need the same Java version.
maven-compiler-plugin in pom.xmlSets the source and target Java versions in the POM.Pinning the project's language level and bytecode version, independent of each developer's setup.
Maven toolchainsConfigures multiple JDKs in toolchains.xml and references them from the project.Projects that must compile against different JDKs without changing environment variables.

Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms