Open In App

Setting the Java Version in Maven

Last Updated : 02 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Setting the Java version in Maven is important for ensuring compatibility and proper compilation of our project. By configuring the Maven Compiler Plugin in the pom.xml file, we can specify both the source and target Java versions. This ensures that our project is compiled using the desired Java version. It allows us to leverage its features while maintaining compatibility with our target environment.

Before changing the Java version in Maven, it's essential to check the current version of Java in Maven. For that, we have Maven commands.

mvn -version

Output:

Below, we provide an output image showing the current Maven version with the Java version.

[caption width="800"]Current Maven and Java Version [/caption]

We have current maven version 3.8.8 with java version 17. Now we are change java version to 17 to 21.X for this we need specify the required java version in the compiler plugin in the pom.xml file. We changing java version in the maven by using Compiler Plugin.


Changing Java Version in Maven Using Compiler Plugin

By using compiler plugin we change the Java version in the maven. Below is the step-by-step implementation.

Step 1: Create Maven Project

First create a maven project, once project is successfully created then open pom.xml file from your project folder.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://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/> <!-- lookup parent from repository -->
</parent>
<groupId>com.app</groupId>
<artifactId>mavencommends</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mavencommends</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 Configuration Code

Now locate the below configuration code in the pom.xml file.

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


Step 3: Modify Java Version

Currently we have Java version 17. Now, we change this version to 21.X by modifying the version of Java to the desired version by updating the value within the <java.version> tag.

<properties>
<java.version>21.1</java.version>
</properties>


Step 4: Update and Run the Project

Now, update the project by using maven update project option, and run this project. After that observe the Java version in the Java console with logs.

[caption width="800"]Application Runs with updated Java Version [/caption]




Next Article
Article Tags :

Similar Reads