Java
Maven
Programming
Software Development
Version Control

How to set specific Java version to Maven?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Working with Java applications often requires different versions of Java due to dependency requirements or newer features of the language. Maven, a powerful build tool used primarily for Java projects, does not set the Java version directly but uses the one specified by the JAVA_HOME environment variable or the one on the system path. However, controlling the Java version used by Maven can be critical, especially when working on multiple projects that may require different Java versions. This article provides a comprehensive guide on how to set a specific Java version for Maven builds.

Understanding JAVA_HOME and PATH

Maven uses the Java version pointed to by the JAVA_HOME environment variable. If JAVA_HOME is not set, it defaults to the Java version available on the system path. The JAVA_HOME variable is crucial for developers because it dictates which JDK Maven will use to compile your projects.

Setting JAVA_HOME on Different Operating Systems

Windows:

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

Linux/Mac:

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

Configuring Maven to Use a Specific Java Version

While the JAVA_HOME variable sets the JDK for the terminal session or system-wide, Maven also allows for project-specific JDK configurations through the pom.xml file using build plugins.

Maven Compiler Plugin

The most common approach is to configure the maven-compiler-plugin in your pom.xml. This plugin specifically allows you to set the source and target Java versions for your project. Example configuration:

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>

This setup ensures that Maven uses Java 8 to compile the project, regardless of the global JAVA_HOME setting.

Using Toolchains to Manage Multiple JDKs

For projects requiring different JDKs, Maven's Toolchains plugin allows you to configure multiple JDKs without changing the JAVA_HOME each time. Define your toolchains in a toolchains.xml file typically located 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, modify your pom.xml to use the toolchain:

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            <compilerVersion>1.8</compilerVersion>
11            <fork>true</fork> <!-- Important for using toolchains -->
12          </configuration>
13        </plugin>
14      </plugins>
15    </pluginManagement>
16  </build>
17  ...
18</project>

Summary Table

MethodDescriptionUse Case
JAVA_HOME Environment VariableSet the system-wide or session-specific JAVA_HOME to point to the desired JDK.Suitable when working on a single project or when multiple projects require the same Java version.
maven-compiler-plugin in pom.xmlSet the Java source and target versions in the project's POM file.Best for ensuring the project uses a specific Java version, independent of the system’s JAVA_HOME.
Toolchains in MavenConfigure multiple JDKs via a toolchains.xml file and reference them in your project.Ideal for projects that need to compile against different JDKs without changing environment variables.

This article has explored various methods to control the Java version used in Maven builds, enabling more fine-grained control over the compilation environment, which is essential for maintaining and managing Java-based projects efficiently.


Course illustration
Course illustration

All Rights Reserved.