• [gentoo-dev] [PATCH] java-pkg-simple.eclass: support Multi-Release JAR

    From Volkmar W. Pogatzki@21:1/5 to All on Fri Feb 21 20:20:02 2025
    See https://openjdk.org/jeps/238

    This commit adds basic support for building multi-release jar files.
    A multi-release jar file has release-specific classes in directories
    under META-INF/versions/ and its MANIFEST.MF contains a line with 'Multi-Release: true'.

    The probably most common case of a multi-release jar file has only one
    single such class which is 'META-INF/versions/9/module-info.class'.

    To do so, we add JAVA_RELEASE_SRC_DIRS as a new eclass variable which
    is also used as the condition to trigger the new functionality. A new
    local variable 'multi_release' is added to the packaging section (the
    part using the 'jar -create' command). Only when JAVA_RELEASE_SRC_DIRS
    is set, additional actions take place:

    - Compilation (those are the parts with 'ejavac') will additionally loop
    over the release-specific directories listed in JAVA_RELEASE_SRC_DIRS
    and compile the release-specific classes into corresponding directories
    under target/versions/.

    - Packaging (the part using the 'jar -create' command) will add the
    details to the 'multi_release' variable so that the release-specific
    directories under target/versions/ can be packaged into the jar file.

    This commit also adds funtionality to generate 'module-info.java' files.
    It is useful for packages where module-info.java is not provided in the
    sources but needs to be generated by the build system. We use the built-in jdeps function with the --generate-module-info option which became available with Java 11.

    It generates the module-info.java file based on an intermediate jar file
    and places it in the "${JAVA_MODULE_INFO_OUT}/${JAVA_INTERMEDIATE_JAR_NAME}/" directory.

    For this purpose we add three new eclass variables:
    - JAVA_INTERMEDIATE_JAR_NAME
    - JAVA_MODULE_INFO_OUT
    - JAVA_MODULE_INFO_RELEASE

    When both JAVA_MODULE_INFO_OUT and JAVA_INTERMEDIATE_JAR_NAME are defined in the
    ebuild we
    - compile the sources still without module-info
    - package them as an intermediate {JAVA_INTERMEDIATE_JAR_NAME}.jar
    - let java-pkg-simple_generate-module-info generate the module-info
    - compile module-info.java with the --patch-module option
    - package the final jar file including the module-info.class

    When the JAVA_MODULE_INFO_RELEASE variable is set, module-info.java is generated into a release specific directory "${JAVA_MODULE_INFO_OUT}/${JAVA_INTERMEDIATE_JAR_NAME}/versions/{JAVA_MODULE_INFO_RELEASE}".

    Bug: https://bugs.gentoo.org/900433
    Signed-off-by: Volkmar W. Pogatzki <gentoo@pogatzki.net>
    ---
    eclass/java-pkg-simple.eclass | 267 +++++++++++++++++++++++++++++++++-
    1 file changed, 262 insertions(+), 5 deletions(-)

    diff --git a/eclass/java-pkg-simple.eclass b/eclass/java-pkg-simple.eclass index ce4a62f048da..79b90b927db0 100644
    --- a/eclass/java-pkg-simple.eclass
    +++ b/eclass/java-pkg-simple.eclass
    @@ -1,4 +1,4 @@
    -# Copyright 2004-2024 Gentoo Authors
    +# Copyright 2004-2025 Gentoo Authors
    # Distributed under the terms of the GNU General Public License v2

    # @ECLASS: java-pkg-simple.eclass
    @@ -11,7 +11,9 @@
    # @DESCRIPTION:
    # This class is intended to build pure Java packages from Java sources
    # without the use of any build instructions shipped with the sources.
    -# There is no support for generating source files, or for controlling
    +# It can generate module-info.java files and supports adding the Main-Class
    +# and the Automatic-Module-Name attributes to MANIFEST.MF. There is no
    +# further support for generating source files, or for controlling
    # the META-INF of the res