Saturday, 29 November 2025

How to Upgrade your legacy Jdk 8 project to more mordern JDK 21

 When you use Maven archetype + JDK + Maven 3.9, to create a java project… the generated project will still default to Java 1.7/1.8 syntax level in the POM unless you update it.

This creates a very old-style project structure designed for Java 1.7–1.8.
The project WILL compile but may not be compatible to Java 21 and its features unless we modernize it.

Which means you may get build failures using Newer modern versions of java like jdk 17/21




Why You Should Use JDK 21 for New Projects?

You get:

✔ Better security
✔ More memory efficiency
✔ Better garbage collection
✔ Faster builds
✔ Support for sealed classes, records, virtual threads (Loom)
✔ Long-term stability (LTS release)


📌 Upgrade LEGACY Projects to JDK 21 — In 5 Steps 

🛑 Legacy apps were built with JDK 8 or below, so you must modernize Gradually, not blindly Here is the shortest upgrade path:

🔧 Step-1 — Open old pom.xml replace Java version Find old:

 <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> 



Replace with: <maven.compiler.release>21</maven.compiler.release> 



Step-2 — Update plugins (old plugins break on Java 21) 

<plugin>
 <groupId>org.apache.maven.plugins</groupId> 
 <artifactId>maven-compiler-plugin</artifactId> 
 <version>3.11.0</version> <!-- important -->
 </plugin>


 <plugin> 
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-surefire-plugin</artifactId>
 <version>3.2.5</version>
</plugin>

 If it's a webapp, also refresh WAR plugin: 

 <plugin>
 <groupId>org.apache.maven.plugins</groupId> 
 <artifactId>maven-war-plugin</artifactId>
 <version>3.4.0</version>
 </plugin>


 Step-3 — Build using Java 21 mvn clean package

Step-4 — Fix libraries that no longer exist 
JDK 21 removed Java EE packages like: javax.servlet.* javax.xml.bind.* javax.activation.* If using them, add modern equivalents: 
 <dependency>
 <groupId>jakarta.servlet</groupId> 
 <artifactId>jakarta.servlet-api</artifactId>
 <version>6.0.0</version> 
 <scope>provided</scope> 
</dependency> 

Step-5 — Run in Jenkins with JDK 21 Jenkins → Manage Jenkins → Global Tool Configuration → JDK → Add (JAVA_HOME pointing to JDK21)


LAB EXERCISE(Upgrade your legacy jdk 8 project to jdk21)

Open your pom.xml and replace it with the below

<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
                             http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.mywebapp</groupId>
    <artifactId>mywebapp</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>MyWebApp</name>

    <properties>
        <maven.compiler.source>21</maven.compiler.source>
        <maven.compiler.target>21</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <!-- Example dependency for unit tests -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.10.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.10.0</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <finalName>MyWebApp</finalName>
        <plugins>
            <!-- JDK 21 Maven Compiler -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.11.0</version>
                <configuration>
                    <release>21</release>
                </configuration>
            </plugin>

            <!-- WAR packaging support -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.4.0</version>
            </plugin>
        </plugins>
    </build>

</project>


Save and Run your build in Jenkins

No comments:

Post a Comment

How to Upgrade your legacy Jdk 8 project to more mordern JDK 21

 When you use  Maven archetype + JDK + Maven 3.9 , to  create a java project … the generated project will still default to Java 1.7/1.8 synt...