Horje
Setting the Java Version in Maven

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.

Current Maven and Java Version

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.

Application Runs with updated Java Version




Reffered: https://www.geeksforgeeks.org


Advance Java

Related
Why Maven Doesn’t Find JUnit Tests to Run? Why Maven Doesn’t Find JUnit Tests to Run?
Load Balancing for WebSockets in Spring Boot Load Balancing for WebSockets in Spring Boot
How to Resolve &quot;Spring Security hasRole() not working&quot;? How to Resolve &quot;Spring Security hasRole() not working&quot;?
Circuit Breaker Pattern in Spring WebFlux Circuit Breaker Pattern in Spring WebFlux
Features of Maven Features of Maven

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
15