DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • OpenAPI From Code With Spring and Java: A Recipe for Your CI
  • From Repetition to Reusability: How Maven Archetypes Save Time
  • Using Python Libraries in Java
  • Using Lombok Library With JDK 23

Trending

  • The Agent Protocol Stack: MCP vs. A2A vs. AG-UI
  • From APIs to Event-Driven Systems: Modern Java Backend Design
  • 5 Security Considerations for Deploying AI on Edge Devices
  • Content Lakes: Harness Unstructured Data for Enterprise AI Readiness
  1. DZone
  2. Coding
  3. Java
  4. Guide for Supporting Multiple Versions of Java (8, 11, 14) in Your Maven Project

Guide for Supporting Multiple Versions of Java (8, 11, 14) in Your Maven Project

In this article, see a guide for supporting multiple versions of Java in your Maven project.

By 
Saša Starčević user avatar
Saša Starčević
·
Apr. 30, 20 · Tutorial
Likes (15)
Comment
Save
Tweet
Share
78.5K Views

Join the DZone community and get the full member experience.

Join For Free

The new version of Java is here! But you cannot migrate at this moment, that is not a problem, because you can do it partially. 

If this is you problem, then you are in right place. :)

In this article, you will see how to support multiple versions of Java in a Maven project. 

Prerequisites

  • Installed Maven
  • Installed multiple versions of Java

Steps for Supporting Multiple Java Versions

  • Create toolchains.xml file
  • Possible locations for toolchains.xml
  • Add Maven Toolchains dependency in parent .pom
  • Add configuration for specific JDK version in modules
  • First module, use the feature from JDK 8 and simple JUnit test
  • Second module, use the feature from JDK 11 and simple JUnit test
  • Third module, use the feature from JDK 14 and simple JUnit test
  • Execute 'mvn clean install' and check logs
  • Check Java 14 features in IntelliJ
  • Source code for this example

Install Maven

Start by updating the package index:

Shell
 




xxxxxxxxxx
1


 
1
sudo apt update



Next, install Maven by typing the following command:

Shell
 




xxxxxxxxxx
1


 
1
sudo apt install maven



Verify the installation by running the mvn -version command:

Shell
 




xxxxxxxxxx
1


 
1
mvn -version



Install Multiple Java Versions

First, update the apt package index with:

Shell
 




xxxxxxxxxx
1


 
1
sudo apt update



Once the package index is updated install the default Java OpenJDK package with:

Shell
 




xxxxxxxxxx
1


 
1
sudo apt install default-jdk



Verify the installation, by running the following command which will print the Java version:

Shell
 




xxxxxxxxxx
1


 
1
java -version



The output will look something like this:
Java
 




xxxxxxxxxx
1


 
1
openjdk version "11.0.2" 2019-01-15
2

          
3
OpenJDK Runtime Environment (build 11.0.2+9-Ubuntu-3ubuntu118.04.3)
4

          
5
OpenJDK 64-Bit Server VM (build 11.0.2+9-Ubuntu-3ubuntu118.04.3, mixed mode, sharing)



Note: Repeat process for all versions of Java, which are required for your specific case.

Steps for Supporting Multiple Java Versions

Create toolchains.xml file

This file will be used in 'maven-toolchains-plugin' and whole configuration for jdk version should be in this file.

XML
 




xxxxxxxxxx
1
33


 
1
<?xml version="1.0" encoding="UTF8"?>
2
<toolchains>
3
    <!-- JDK toolchains -->
4
    <toolchain>
5
        <type>jdk</type>
6
        <provides>
7
            <version>8</version>
8
        </provides>
9
        <configuration>
10
            <jdkHome>/usr/lib/jvm/java-8-openjdk-amd64/jre</jdkHome>
11
        </configuration>
12
    </toolchain>
13
    <toolchain>
14
        <type>jdk</type>
15
        <provides>
16
            <version>11</version>
17
        </provides>
18
        <configuration>
19
            <jdkHome>/home/sasa/Downloads/jdk11/amazon-corretto-11.0.6.10.1-linux-x64</jdkHome>
20
        </configuration>
21
    </toolchain>
22
    <toolchain>
23
        <type>jdk</type>
24
        <provides>
25
            <version>14</version>
26
        </provides>
27
        <configuration>
28
            <jdkHome>/home/sasa/Downloads/jdk11/jdk-14</jdkHome>
29
        </configuration>
30
    </toolchain>
31
    <!-- other toolchains -->
32
</toolchains>
33

          



Possible Locations for toolchains.xml

Case 1: This file can be located in your ~/.m2 when configuration is user based

Case 2: This file can be located in {maven_home}/conf

Case 3: Can be added as parameter while executing maven command

Add Maven Toolchains Dependency in parent .pom

Parent pom in this example looks like:

XML
 




xxxxxxxxxx
1
67


 
1
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3

          
4
    <modelVersion>4.0.0</modelVersion>
5

          
6
    <groupId>com.sasa.multipleversions.examples</groupId>
7
    <artifactId>multiple-java-versions</artifactId>
8
    <version>1.0.0</version>
9
    <packaging>pom</packaging>
10

          
11
    <modules>
12
        <module>module-jdk-8</module>
13
        <module>module-jdk-11</module>
14
        <module>module-jdk-14</module>
15
    </modules>
16

          
17
    <build>
18
        <plugins>
19
            <plugin>
20
                <groupId>org.apache.maven.plugins</groupId>
21
                <artifactId>maven-compiler-plugin</artifactId>
22
                <version>3.8.1</version>
23
            </plugin>
24
            <plugin>
25
                <groupId>org.apache.maven.plugins</groupId>
26
                <artifactId>maven-surefire-plugin</artifactId>
27
                <version>3.0.0-M4</version>
28
            </plugin>
29
            <plugin>
30
                <groupId>org.apache.maven.plugins</groupId>
31
                <artifactId>maven-toolchains-plugin</artifactId>
32
                <version>3.0.0</version>
33
                <executions>
34
                    <execution>
35
                        <goals>
36
                            <goal>toolchain</goal>
37
                        </goals>
38
                        <configuration>
39
                            <toolchains>
40
                                <jdk>
41
                                    <version>8</version>
42
                                </jdk>
43
                            </toolchains>
44
                        </configuration>
45
                    </execution>
46
                </executions>
47
            </plugin>
48
        </plugins>
49
    </build>
50

          
51
    <dependencies>
52
        <dependency>
53
            <groupId>org.junit.jupiter</groupId>
54
            <artifactId>junit-jupiter-api</artifactId>
55
            <version>5.4.2</version>
56
            <scope>test</scope>
57
        </dependency>
58
        <dependency>
59
            <groupId>org.junit.jupiter</groupId>
60
            <artifactId>junit-jupiter-engine</artifactId>
61
            <version>5.4.2</version>
62
            <scope>test</scope>
63
        </dependency>
64
    </dependencies>
65

          
66
</project>
67

          



Add Configuration for Specific JDK Version in Modules

Toolchains configuration in .pom file for JDK8 module:

XML
 




xxxxxxxxxx
1
67


 
1
<?xml version="1.0" encoding="UTF-8"?>
2
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4
    <modelVersion>4.0.0</modelVersion>
5

          
6
    <parent>
7
        <groupId>com.sasa.multipleversions.examples</groupId>
8
        <artifactId>multiple-java-versions</artifactId>
9
        <version>1.0.0</version>
10
    </parent>
11

          
12
    <artifactId>module-jdk-8</artifactId>
13

          
14
    <properties>
15
        <maven.compiler.source>8</maven.compiler.source>
16
        <maven.compiler.target>8</maven.compiler.target>
17
    </properties>
18

          
19
    <build>
20
        <pluginManagement>
21
            <plugins>
22
                <plugin>
23
                    <groupId>org.apache.maven.plugins</groupId>
24
                    <artifactId>maven-surefire-plugin</artifactId>
25
                    <version>3.0.0-M4</version>
26
                </plugin>
27
            </plugins>
28
        </pluginManagement>
29

          
30
        <plugins>
31
            <plugin>
32
                <groupId>org.apache.maven.plugins</groupId>
33
                <artifactId>maven-toolchains-plugin</artifactId>
34
                <executions>
35
                    <execution>
36
                        <goals>
37
                            <goal>toolchain</goal>
38
                        </goals>
39
                        <configuration>
40
                            <toolchains>
41
                                <jdk>
42
                                    <version>8</version>
43
                                </jdk>
44
                            </toolchains>
45
                        </configuration>
46
                    </execution>
47
                </executions>
48
            </plugin>
49
        </plugins>
50
    </build>
51

          
52
    <dependencies>
53
        <dependency>
54
            <groupId>javax.validation</groupId>
55
            <artifactId>validation-api</artifactId>
56
            <version>2.0.0.Final</version>
57
        </dependency>
58
        <dependency>
59
            <groupId>junit</groupId>
60
            <artifactId>junit</artifactId>
61
            <version>4.4</version>
62
            <scope>test</scope>
63
        </dependency>
64
    </dependencies>
65

          
66
</project>
67

          



Toolchains configuration in .pom file for JDK11 module:

XML
 




xxxxxxxxxx
1
67


 
1
<?xml version="1.0" encoding="UTF-8"?>
2
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4
    <modelVersion>4.0.0</modelVersion>
5

          
6
    <parent>
7
        <groupId>com.sasa.multipleversions.examples</groupId>
8
        <artifactId>multiple-java-versions</artifactId>
9
        <version>1.0.0</version>
10
    </parent>
11

          
12
    <artifactId>module-jdk-11</artifactId>
13

          
14
    <properties>
15
        <maven.compiler.source>11</maven.compiler.source>
16
        <maven.compiler.target>11</maven.compiler.target>
17
    </properties>
18

          
19
    <build>
20
        <pluginManagement>
21
            <plugins>
22
                <plugin>
23
                    <groupId>org.apache.maven.plugins</groupId>
24
                    <artifactId>maven-surefire-plugin</artifactId>
25
                    <version>3.0.0-M4</version>
26
                </plugin>
27
            </plugins>
28
        </pluginManagement>
29

          
30
        <plugins>
31
            <plugin>
32
                <groupId>org.apache.maven.plugins</groupId>
33
                <artifactId>maven-toolchains-plugin</artifactId>
34
                <executions>
35
                    <execution>
36
                        <goals>
37
                            <goal>toolchain</goal>
38
                        </goals>
39
                        <configuration>
40
                            <toolchains>
41
                                <jdk>
42
                                    <version>11</version>
43
                                </jdk>
44
                            </toolchains>
45
                        </configuration>
46
                    </execution>
47
                </executions>
48
            </plugin>
49
        </plugins>
50
    </build>
51

          
52
    <dependencies>
53
        <dependency>
54
            <groupId>javax.validation</groupId>
55
            <artifactId>validation-api</artifactId>
56
            <version>2.0.0.Final</version>
57
        </dependency>
58
        <dependency>
59
            <groupId>junit</groupId>
60
            <artifactId>junit</artifactId>
61
            <version>4.4</version>
62
            <scope>test</scope>
63
        </dependency>
64
    </dependencies>
65

          
66
</project>
67

          



Toolchains configuration in .pom file for JDK14 module:

XML
 




x
1
84


 
1
<?xml version="1.0" encoding="UTF-8"?>
2
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4
    <modelVersion>4.0.0</modelVersion>
5

          
6
    <parent>
7
        <groupId>com.sasa.multipleversions.examples</groupId>
8
        <artifactId>multiple-java-versions</artifactId>
9
        <version>1.0.0</version>
10
    </parent>
11

          
12
    <artifactId>module-jdk-14</artifactId>
13

          
14
    <properties>
15
        <maven.compiler.source>14</maven.compiler.source>
16
        <maven.compiler.target>14</maven.compiler.target>
17
    </properties>
18

          
19
    <build>
20
        <pluginManagement>
21
            <plugins>
22
                <plugin>
23
                    <artifactId>maven-compiler-plugin</artifactId>
24
                    <configuration>
25
                        <release>14</release>
26
                        <compilerArgs>
27
                            --enable-preview
28
                        </compilerArgs>
29
                    </configuration>
30
                </plugin>
31
                <plugin>
32
                    <artifactId>maven-surefire-plugin</artifactId>
33
                    <configuration>
34
                        <argLine>--enable-preview</argLine>
35
                    </configuration>
36
                </plugin>
37
                <plugin>
38
                    <artifactId>maven-failsafe-plugin</artifactId>
39
                    <configuration>
40
                        <argLine>--enable-preview</argLine>
41
                    </configuration>
42
                </plugin>
43
            </plugins>
44
        </pluginManagement>
45

          
46

          
47
        <plugins>
48
            <plugin>
49
                <groupId>org.apache.maven.plugins</groupId>
50
                <artifactId>maven-toolchains-plugin</artifactId>
51
                <executions>
52
                    <execution>
53
                        <goals>
54
                            <goal>toolchain</goal>
55
                        </goals>
56
                        <configuration>
57
                            <toolchains>
58
                                <jdk>
59
                                    <version>14</version>
60
                                </jdk>
61
                            </toolchains>
62
                        </configuration>
63
                    </execution>
64
                </executions>
65
            </plugin>
66
        </plugins>
67
    </build>
68

          
69
    <dependencies>
70
        <dependency>
71
            <groupId>javax.validation</groupId>
72
            <artifactId>validation-api</artifactId>
73
            <version>2.0.0.Final</version>
74
        </dependency>
75
        <dependency>
76
            <groupId>junit</groupId>
77
            <artifactId>junit</artifactId>
78
            <version>4.4</version>
79
            <scope>test</scope>
80
        </dependency>
81
    </dependencies>
82

          
83
</project>
84
-




In the first module, use the feature from JDK 8 and simple JUnit test.

Simple JDK8 feature:

Java
 




xxxxxxxxxx
1
17


 
1
package com.multipleversion;
2

          
3
import javax.validation.Valid;
4
import java.util.function.IntFunction;
5

          
6
public class TestJdk8 {
7

          
8
    public TestJdk8() {
9
        IntFunction<Integer> doubleIt1 = (@Valid final int x) -> x * 2;   // Compiles since Java 8
10

          
11
        // IntFunction<Integer> doubleIt2 = (@Valid final var x) -> x * 2;   // Will compile from Java 11
12

          
13
        System.out.println("TestJdk8: " + doubleIt1.apply(5));
14
    }
15

          
16
}
17

          



Simple test without any assert:

Java
 




xxxxxxxxxx
1
33


 
1
package com.multipleversion;
2

          
3
import org.junit.jupiter.api.AfterEach;
4
import org.junit.jupiter.api.BeforeAll;
5
import org.junit.jupiter.api.Test;
6

          
7
public class Test8 {
8

          
9
    @BeforeAll
10
    public static void init() {
11
        System.out.println("");
12
        System.out.println("");
13
        System.out.println("#######################################################################################");
14
        System.out.println("");
15
    }
16

          
17
    @AfterEach
18
    public void finalize() {
19
        System.out.println("");
20
        System.out.println("#######################################################################################");
21
        System.out.println("");
22
        System.out.println("");
23
    }
24

          
25
    @Test
26
    public void testJdk8() {
27
        System.out.println("Test JDK 8");
28
        System.out.println("");
29

          
30
        TestJdk8 testJdk8 = new TestJdk8();
31
    }
32

          
33
}



Second module, use the feature from JDK 11 and simple JUnit test.

Simple JDK11 feature:

Java
 




xxxxxxxxxx
1
18


 
1
package com.multipleversion;
2

          
3
import javax.validation.Valid;
4
import java.util.function.IntFunction;
5

          
6
public class TestJdk11 {
7

          
8
    public TestJdk11() {
9
        // IntFunction<Integer> doubleIt1 = (@Valid final int x) -> x * 2;   // Compiles since Java 8
10

          
11
        // We are using 'var' instead of 'int'
12
        IntFunction<Integer> doubleIt2 = (@Valid final var x) -> x * 2;   // Will compile from Java 11
13

          
14
        System.out.println("TestJdk11: " + doubleIt2.apply(5));
15
    }
16

          
17
}
18

          



Simple test without any assert:

Java
 




xxxxxxxxxx
1
34


 
1
package com.multipleversion;
2

          
3
import org.junit.BeforeClass;
4
import org.junit.jupiter.api.AfterEach;
5
import org.junit.jupiter.api.BeforeAll;
6
import org.junit.jupiter.api.Test;
7

          
8
public class Test11 {
9

          
10
    @BeforeAll
11
    public static void init() {
12
        System.out.println("");
13
        System.out.println("");
14
        System.out.println("#######################################################################################");
15
        System.out.println("");
16
    }
17

          
18
    @AfterEach
19
    public void finalize() {
20
        System.out.println("");
21
        System.out.println("#######################################################################################");
22
        System.out.println("");
23
        System.out.println("");
24
    }
25

          
26
    @Test
27
    public void testJdk11() {
28
        System.out.println("Test JDK 11");
29
        System.out.println("");
30

          
31
        TestJdk11 testJdk11 = new TestJdk11();
32
    }
33

          
34
}



Third module, use the feature from JDK 14 and simple JUnit test

Simple JDK14 feature:

Java
 




xxxxxxxxxx
1
18


 
1
package com.multipleversion;
2

          
3
public class TestJdk14 {
4

          
5
    public TestJdk14(Object obj) {
6
        if (obj instanceof String s) {      // instanceof, cast and bind variable in one line.
7
            if ("jdk14".equalsIgnoreCase(s)) {
8
                System.out.println("Equals");
9
            } else {
10
                System.out.println("Not Equals");
11
            }
12
        } else {
13
            System.out.println("Not a string");
14
        }
15
    }
16

          
17
}
18

          



Simple test without any assert:

Java
 




xxxxxxxxxx
1
37


 
1
package com.multipleversion;
2

          
3
import org.junit.jupiter.api.AfterEach;
4
import org.junit.jupiter.api.BeforeAll;
5
import org.junit.jupiter.api.Test;
6

          
7
public class Test14 {
8

          
9
    @BeforeAll
10
    public static void init() {
11
        System.out.println("");
12
        System.out.println("");
13
        System.out.println("#######################################################################################");
14
        System.out.println("");
15
    }
16

          
17
    @AfterEach
18
    public void finalize() {
19
        System.out.println("");
20
        System.out.println("#######################################################################################");
21
        System.out.println("");
22
        System.out.println("");
23
    }
24

          
25
    @Test
26
    public void testJdk14() {
27
        System.out.println("Test JDK 14");
28
        System.out.println("");
29

          
30
        TestJdk14 testJdk = new TestJdk14("jdk14");
31

          
32
        TestJdk14 testJdk2 = new TestJdk14("jdk16");
33

          
34
        TestJdk14 testJdk3 = new TestJdk14(1);
35
    }
36

          
37
}



Execute 'mvn clean install' and check logs.

After successful execution you will get logs like:

Shell
 




xxxxxxxxxx
1
14


 
1
...
2
[INFO] Reactor Summary:
3
[INFO] 
4
[INFO] multiple-java-versions ............................. SUCCESS [  0.673 s]
5
[INFO] module-jdk-8 ....................................... SUCCESS [  3.710 s]
6
[INFO] module-jdk-11 ...................................... SUCCESS [  2.918 s]
7
[INFO] module-jdk-14 ...................................... SUCCESS [  2.762 s]
8
[INFO] ------------------------------------------------------------------------
9
[INFO] BUILD SUCCESS
10
[INFO] ------------------------------------------------------------------------
11
[INFO] Total time: 10.262 s
12
[INFO] Finished at: 2020-04-04T14:57:36+02:00
13
[INFO] Final Memory: 13M/237M
14
[INFO] ------------------------------------------------------------------------
15

          



First module is working with JDK8 and this is visible in logs of successful build:

Shell
 




xxxxxxxxxx
1
63


 
1
...
2
[INFO] ------------------------------------------------------------------------
3
[INFO] Building module-jdk-8 1.0.0
4
[INFO] ------------------------------------------------------------------------
5
[INFO] 
6
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ module-jdk-8 ---
7
[INFO] 
8
[INFO] --- maven-toolchains-plugin:3.0.0:toolchain (default) @ module-jdk-8 ---
9
[INFO] Required toolchain: jdk [ version='8' ]
10
[INFO] Found matching toolchain for type jdk: JDK[/usr/lib/jvm/java-8-openjdk-amd64/jre]
11
[INFO] 
12
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ module-jdk-8 ---
13
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
14
[INFO] skip non existing resourceDirectory /home/sasa/Sasa Podaci/Podaci/Develpoment/dzone-article/test/multiple-java-versions/module-jdk-8/src/main/resources
15
[INFO] 
16
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ module-jdk-8 ---
17
[INFO] Toolchain in maven-compiler-plugin: JDK[/usr/lib/jvm/java-8-openjdk-amd64/jre]
18
[INFO] Changes detected - recompiling the module!
19
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
20
[INFO] Compiling 1 source file to /home/sasa/Sasa Podaci/Podaci/Develpoment/dzone-article/test/multiple-java-versions/module-jdk-8/target/classes
21
[INFO] 
22
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ module-jdk-8 ---
23
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
24
[INFO] skip non existing resourceDirectory /home/sasa/Sasa Podaci/Podaci/Develpoment/dzone-article/test/multiple-java-versions/module-jdk-8/src/test/resources
25
[INFO] 
26
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ module-jdk-8 ---
27
[INFO] Toolchain in maven-compiler-plugin: JDK[/usr/lib/jvm/java-8-openjdk-amd64/jre]
28
[INFO] Changes detected - recompiling the module!
29
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
30
[INFO] Compiling 1 source file to /home/sasa/Sasa Podaci/Podaci/Develpoment/dzone-article/test/multiple-java-versions/module-jdk-8/target/test-classes
31
[INFO] 
32
[INFO] --- maven-surefire-plugin:3.0.0-M4:test (default-test) @ module-jdk-8 ---
33
[INFO] Toolchain in maven-surefire-plugin: JDK[/usr/lib/jvm/java-8-openjdk-amd64/jre]
34
[INFO] 
35
[INFO] -------------------------------------------------------
36
[INFO]  T E S T S
37
[INFO] -------------------------------------------------------
38
[INFO] Running com.multipleversion.Test8
39

          
40

          
41
#######################################################################################
42

          
43
Test JDK 8
44

          
45
TestJdk8: 10
46

          
47
#######################################################################################
48

          
49

          
50
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.034 s - in com.multipleversion.Test8
51
[INFO] 
52
[INFO] Results:
53
[INFO] 
54
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
55
[INFO] 
56
[INFO] 
57
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ module-jdk-8 ---
58
[INFO] Building jar: /home/sasa/Sasa Podaci/Podaci/Develpoment/dzone-article/test/multiple-java-versions/module-jdk-8/target/module-jdk-8-1.0.0.jar
59
[INFO] 
60
[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ module-jdk-8 ---
61
[INFO] Installing /home/sasa/Sasa Podaci/Podaci/Develpoment/dzone-article/test/multiple-java-versions/module-jdk-8/target/module-jdk-8-1.0.0.jar to /home/sasa/.m2/repository/com/sasa/multipleversions/examples/module-jdk-8/1.0.0/module-jdk-8-1.0.0.jar
62
[INFO] Installing /home/sasa/Sasa Podaci/Podaci/Develpoment/dzone-article/test/multiple-java-versions/module-jdk-8/pom.xml to /home/sasa/.m2/repository/com/sasa/multipleversions/examples/module-jdk-8/1.0.0/module-jdk-8-1.0.0.pom
63
[INFO]                                                                         
64
...



From logs is visible that module-jdk-8 is using maven-toolchains-plugin and set JDK version 8:

Shell
 




xxxxxxxxxx
1


 
1
[INFO] --- maven-toolchains-plugin:3.0.0:toolchain (default) @ module-jdk-8 ---
2
[INFO] Required toolchain: jdk [ version='8' ]
3
[INFO] Found matching toolchain for type jdk: JDK[/usr/lib/jvm/java-8-openjdk-amd64/jre]



The second module is working with JDK11 and this is visible in logs of successful build:

Shell
 




xxxxxxxxxx
1
64


 
1
...
2
[INFO]                                                                         
3
[INFO] ------------------------------------------------------------------------
4
[INFO] Building module-jdk-11 1.0.0
5
[INFO] ------------------------------------------------------------------------
6
[INFO] 
7
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ module-jdk-11 ---
8
[INFO] 
9
[INFO] --- maven-toolchains-plugin:3.0.0:toolchain (default) @ module-jdk-11 ---
10
[INFO] Required toolchain: jdk [ version='11' ]
11
[INFO] Found matching toolchain for type jdk: JDK[/home/sasa/Downloads/jdk11/amazon-corretto-11.0.6.10.1-linux-x64]
12
[INFO] 
13
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ module-jdk-11 ---
14
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
15
[INFO] skip non existing resourceDirectory /home/sasa/Sasa Podaci/Podaci/Develpoment/dzone-article/test/multiple-java-versions/module-jdk-11/src/main/resources
16
[INFO] 
17
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ module-jdk-11 ---
18
[INFO] Toolchain in maven-compiler-plugin: JDK[/home/sasa/Downloads/jdk11/amazon-corretto-11.0.6.10.1-linux-x64]
19
[INFO] Changes detected - recompiling the module!
20
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
21
[INFO] Compiling 1 source file to /home/sasa/Sasa Podaci/Podaci/Develpoment/dzone-article/test/multiple-java-versions/module-jdk-11/target/classes
22
[INFO] 
23
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ module-jdk-11 ---
24
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
25
[INFO] skip non existing resourceDirectory /home/sasa/Sasa Podaci/Podaci/Develpoment/dzone-article/test/multiple-java-versions/module-jdk-11/src/test/resources
26
[INFO] 
27
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ module-jdk-11 ---
28
[INFO] Toolchain in maven-compiler-plugin: JDK[/home/sasa/Downloads/jdk11/amazon-corretto-11.0.6.10.1-linux-x64]
29
[INFO] Changes detected - recompiling the module!
30
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
31
[INFO] Compiling 1 source file to /home/sasa/Sasa Podaci/Podaci/Develpoment/dzone-article/test/multiple-java-versions/module-jdk-11/target/test-classes
32
[INFO] 
33
[INFO] --- maven-surefire-plugin:3.0.0-M4:test (default-test) @ module-jdk-11 ---
34
[INFO] Toolchain in maven-surefire-plugin: JDK[/home/sasa/Downloads/jdk11/amazon-corretto-11.0.6.10.1-linux-x64]
35
[INFO] 
36
[INFO] -------------------------------------------------------
37
[INFO]  T E S T S
38
[INFO] -------------------------------------------------------
39
[INFO] Running com.multipleversion.Test11
40

          
41

          
42
#######################################################################################
43

          
44
Test JDK 11
45

          
46
TestJdk11: 10
47

          
48
#######################################################################################
49

          
50

          
51
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.06 s - in com.multipleversion.Test11
52
[INFO] 
53
[INFO] Results:
54
[INFO] 
55
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
56
[INFO] 
57
[INFO] 
58
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ module-jdk-11 ---
59
[INFO] Building jar: /home/sasa/Sasa Podaci/Podaci/Develpoment/dzone-article/test/multiple-java-versions/module-jdk-11/target/module-jdk-11-1.0.0.jar
60
[INFO] 
61
[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ module-jdk-11 ---
62
[INFO] Installing /home/sasa/Sasa Podaci/Podaci/Develpoment/dzone-article/test/multiple-java-versions/module-jdk-11/target/module-jdk-11-1.0.0.jar to /home/sasa/.m2/repository/com/sasa/multipleversions/examples/module-jdk-11/1.0.0/module-jdk-11-1.0.0.jar
63
[INFO] Installing /home/sasa/Sasa Podaci/Podaci/Develpoment/dzone-article/test/multiple-java-versions/module-jdk-11/pom.xml to /home/sasa/.m2/repository/com/sasa/multipleversions/examples/module-jdk-11/1.0.0/module-jdk-11-1.0.0.pom
64
[INFO]                                                                         
65
...



From logs is visible that module-jdk-11 is using maven-toolchains-plugin and set JDK version 11:

Shell
 




xxxxxxxxxx
1


 
1
[INFO] --- maven-toolchains-plugin:3.0.0:toolchain (default) @ module-jdk-11 ---
2
[INFO] Required toolchain: jdk [ version='11' ]
3
[INFO] Found matching toolchain for type jdk: JDK[/home/sasa/Downloads/jdk11/amazon-corretto-11.0.6.10.1-linux-x64]



The third module is working with JDK14 and this is visible in logs of successful build:

Java
 




xxxxxxxxxx
1
65


 
1
...
2
[INFO]                                                                         
3
[INFO] ------------------------------------------------------------------------
4
[INFO] Building module-jdk-14 1.0.0
5
[INFO] ------------------------------------------------------------------------
6
[INFO] 
7
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ module-jdk-14 ---
8
[INFO] 
9
[INFO] --- maven-toolchains-plugin:3.0.0:toolchain (default) @ module-jdk-14 ---
10
[INFO] Required toolchain: jdk [ version='14' ]
11
[INFO] Found matching toolchain for type jdk: JDK[/home/sasa/Downloads/jdk11/jdk-14]
12
[INFO] 
13
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ module-jdk-14 ---
14
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
15
[INFO] skip non existing resourceDirectory /home/sasa/Sasa Podaci/Podaci/Develpoment/dzone-article/test/multiple-java-versions/module-jdk-14/src/main/resources
16
[INFO] 
17
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ module-jdk-14 ---
18
[INFO] Toolchain in maven-compiler-plugin: JDK[/home/sasa/Downloads/jdk11/jdk-14]
19
[INFO] Changes detected - recompiling the module!
20
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
21
[INFO] Compiling 1 source file to /home/sasa/Sasa Podaci/Podaci/Develpoment/dzone-article/test/multiple-java-versions/module-jdk-14/target/classes
22
[INFO] 
23
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ module-jdk-14 ---
24
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
25
[INFO] skip non existing resourceDirectory /home/sasa/Sasa Podaci/Podaci/Develpoment/dzone-article/test/multiple-java-versions/module-jdk-14/src/test/resources
26
[INFO] 
27
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ module-jdk-14 ---
28
[INFO] Toolchain in maven-compiler-plugin: JDK[/home/sasa/Downloads/jdk11/jdk-14]
29
[INFO] Changes detected - recompiling the module!
30
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
31
[INFO] Compiling 1 source file to /home/sasa/Sasa Podaci/Podaci/Develpoment/dzone-article/test/multiple-java-versions/module-jdk-14/target/test-classes
32
[INFO] 
33
[INFO] --- maven-surefire-plugin:3.0.0-M4:test (default-test) @ module-jdk-14 ---
34
[INFO] Toolchain in maven-surefire-plugin: JDK[/home/sasa/Downloads/jdk11/jdk-14]
35
[INFO] 
36
[INFO] -------------------------------------------------------
37
[INFO]  T E S T S
38
[INFO] -------------------------------------------------------
39
[INFO] Running com.multipleversion.Test14
40

          
41

          
42
#######################################################################################
43

          
44
Test JDK 14
45

          
46
Equals
47
Not Equals
48
Not a string
49

          
50
#######################################################################################
51

          
52

          
53
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.049 s - in com.multipleversion.Test14
54
[INFO] 
55
[INFO] Results:
56
[INFO] 
57
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
58
[INFO] 
59
[INFO] 
60
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ module-jdk-14 ---
61
[INFO] Building jar: /home/sasa/Sasa Podaci/Podaci/Develpoment/dzone-article/test/multiple-java-versions/module-jdk-14/target/module-jdk-14-1.0.0.jar
62
[INFO] 
63
[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ module-jdk-14 ---
64
[INFO] Installing /home/sasa/Sasa Podaci/Podaci/Develpoment/dzone-article/test/multiple-java-versions/module-jdk-14/target/module-jdk-14-1.0.0.jar to /home/sasa/.m2/repository/com/sasa/multipleversions/examples/module-jdk-14/1.0.0/module-jdk-14-1.0.0.jar
65
[INFO] Installing /home/sasa/Sasa Podaci/Podaci/Develpoment/dzone-article/test/multiple-java-versions/module-jdk-14/pom.xml to /home/sasa/.m2/repository/com/sasa/multipleversions/examples/module-jdk-14/1.0.0/module-jdk-14-1.0.0.pom
66
...



From logs is visible that module-jdk-14 is using maven-toolchains-plugin and set JDK version 14:

Java
 




xxxxxxxxxx
1


 
1
[INFO] --- maven-toolchains-plugin:3.0.0:toolchain (default) @ module-jdk-14 ---
2
[INFO] Required toolchain: jdk [ version='14' ]
3
[INFO] Found matching toolchain for type jdk: JDK[/home/sasa/Downloads/jdk11/jdk-14]



Check Java 14 Features in IntelliJ

IntelliJ will release a new version in April 2020 with support for Java 14 features. You can check those new features and how they are supported in IntelliJ: 

https://blog.jetbrains.com/idea/2020/03/java-14-and-intellij-idea/

Source Code for This Example

Source code for this example, can be found on GitHub: https://github.com/sasastarcevic/multiple-java-versions?files=1
I hope that this practical example will help you to speed up your migration process. Wishing you all the best; stay healthy, and have a nice day.
Java (programming language) Apache Maven

Opinions expressed by DZone contributors are their own.

Related

  • OpenAPI From Code With Spring and Java: A Recipe for Your CI
  • From Repetition to Reusability: How Maven Archetypes Save Time
  • Using Python Libraries in Java
  • Using Lombok Library With JDK 23

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook