Some Learning Materials About The Java Multi-Release JAR Feature

Here are some learning materials related with the Java Multi-Release JAR feature:

Here is the relative sample project:

Here is the setup of the sample project in pom.xml:

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <java9.sourceDirectory>${project.basedir}/src/main/java9</java9.sourceDirectory>
    <java9.build.outputDirectory>${project.build.directory}/classes-java9</java9.build.outputDirectory>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.8</version>
            <executions>
                <execution>
                    <id>compile-java9</id>
                    <phase>compile</phase>
                    <configuration>
                        <tasks>
                            <mkdir dir="${java9.build.outputDirectory}"/>
                            <javac srcdir="${java9.sourceDirectory}" destdir="${java9.build.outputDirectory}"
                                   classpath="${project.build.outputDirectory}" includeantruntime="false"/>
                        </tasks>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>3.0.2</version>
            <executions>
                <execution>
                    <id>copy-resources</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.outputDirectory}/META-INF/versions/9</outputDirectory>
                        <resources>
                            <resource>
                                <directory>${java9.build.outputDirectory}</directory>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.0.2</version>
            <configuration>
                <archive>
                    <manifestEntries>
                        <Multi-Release>true</Multi-Release>
                        <Main-Class>org.hibernate.demos.mrjar.Main</Main-Class>
                    </manifestEntries>
                </archive>
                <finalName>mr-jar-demo</finalName>
            </configuration>
        </plugin>
    </plugins>
</build>

The java9 code is compiled by the maven-antrun-plugin and copied into META-INF/versions/9. In addition, the maven-jar-plugin setup the manifest file with:

<manifestEntries>
    <Multi-Release>true</Multi-Release>
    <Main-Class>org.hibernate.demos.mrjar.Main</Main-Class>
</manifestEntries>

The structure is setup according to:

Here is the structure of the generated JAR file:

➤ unzip -l mr-jar-demo.jar                                                                                                                                                                                                                                                                  00:05:24
Archive:  mr-jar-demo.jar
  Length      Date    Time    Name
---------  ---------- -----   ----
      157  11-29-2023 01:07   META-INF/MANIFEST.MF
        0  11-29-2023 01:07   META-INF/
        0  11-29-2023 01:05   org/
        0  11-29-2023 01:05   org/hibernate/
        0  11-29-2023 01:05   org/hibernate/demos/
        0  11-29-2023 01:05   org/hibernate/demos/mrjar/
        0  11-29-2023 01:07   META-INF/versions/
        0  11-29-2023 01:07   META-INF/versions/9/
        0  11-29-2023 01:07   META-INF/versions/9/org/
        0  11-29-2023 01:07   META-INF/versions/9/org/hibernate/
        0  11-29-2023 01:07   META-INF/versions/9/org/hibernate/demos/
        0  11-29-2023 01:07   META-INF/versions/9/org/hibernate/demos/mrjar/
        0  11-29-2023 01:07   META-INF/maven/
        0  11-29-2023 01:07   META-INF/maven/org.hibernate.demos/
        0  11-29-2023 01:07   META-INF/maven/org.hibernate.demos/multi-release-jar-demo/
      661  11-29-2023 01:05   org/hibernate/demos/mrjar/ProcessIdDescriptor.class
     1002  11-29-2023 01:05   org/hibernate/demos/mrjar/ProcessIdProvider.class
     1116  11-29-2023 01:05   org/hibernate/demos/mrjar/Main.class
      363  11-29-2023 01:07   META-INF/versions/9/org/hibernate/demos/mrjar/ProcessIdProvider.class
     3747  11-29-2023 01:02   META-INF/maven/org.hibernate.demos/multi-release-jar-demo/pom.xml
      114  11-29-2023 01:07   META-INF/maven/org.hibernate.demos/multi-release-jar-demo/pom.properties
---------                     -------
     7160                     21 files

The generated JAR can then be run in different Java versions.

My Github Page: https://github.com/liweinan

Powered by Jekyll and Theme by solid

If you have any question want to ask or find bugs regarding with my blog posts, please report it here:
https://github.com/liweinan/liweinan.github.io/issues