Setting the Java Version in Maven

Last Updated : 8 Nov, 2025

Setting the Java version in Maven ensures that your project compiles with the desired Java compiler level. This is crucial for maintaining compatibility across environments and taking advantage of the features of the selected Java version.

By configuring the Maven Compiler Plugin in the pom.xml file, you can specify both the source and target Java versions that Maven should use during compilation.

Checking Current Java Version in Maven

Before changing the Java version, verify the currently used version with the command:

mvn -version

Sample Output:

m1
Command Prompt

If your project currently uses Java 17 and you want to upgrade to Java 21, you can update this configuration in your pom.xml file using the Maven Compiler Plugin.

Changing Java Version in Maven Using Compiler Plugin

Follow these steps to update the Java version in a Maven project.

Step 1: Create or Open a Maven Project

Open your existing Maven project or create a new one. Then, navigate to the pom.xml file located in your project directory.

Example structure of pom.xml:

XML
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="https://maven.apache.org/POM/4.0.0"
         xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.6</version>
        <relativePath/>
    </parent>

    <groupId>com.app</groupId>
    <artifactId>mavencommands</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>mavencommands</name>
    <description>Spring Reactive</description>

    <properties>
        <java.version>17</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Step 2: Locate the Java Version Property

Inside your pom.xml, locate the <properties> section.

XML
<properties>
    <java.version>17</java.version>
</properties>

Step 3: Update the Java Version

Change the Java version to the desired version. For example, to use Java 21, modify it as follows:

XML
<properties>
    <java.version>21</java.version>
</properties>

You can also explicitly configure the Maven Compiler Plugin for clarity and consistency across environments:

XML
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.11.0</version>
            <configuration>
                <source>21</source>
                <target>21</target>
            </configuration>
        </plugin>
    </plugins>
</build>

Step 4: Update and Verify the Project

After saving changes:

  • Perform a Maven update using your IDE or by running the command below:

mvn clean install

  • Once the build completes, check the console logs. Maven should now compile the project using Java 21.
server
Starting Maven Command
Comment
Article Tags:

Explore