15

I have a project that uses a lot of reflection, also on "new" Java features such as records and sealed classes. I'm writing a class like this:

public class RecordHelper {
    public static boolean isRecord(Class<?> type) {
        return type.isRecord();
    }
}

Of course, this only works in Java 16 and higher, so I'm trying to set up a multi-release JAR file, with a default implementation like this:

public class RecordHelper {
    public static boolean isRecord(Class<?> type) {
        return false;
    }
}

I've been able to set that up with Maven using this post on Baeldung, and it works great. It's nice, because it doesn't rely on separate profiles for various versions, which keeps my pom file clean.

But now I need to write tests.

I want to be able to run the test suite on all platforms that I want to support, which means everything from JDK 8 and up. (I don't mind compiling on a different JDK before I run the tests.) Of course, on JDK 16 and up, I also want to test records-related things (and on 17 and up, sealed classes), so that means that I have to compile some records, which means I will inevitably have some class files that won't work on older JDKs.

In my mind, it would make sense to have something like a multi-release JAR file for tests as well, where the records tests get placed in the appropriate place in META-INF/versions, but of course tests aren't usually packaged in a JAR, so that doesn't work.

Is there a way to get this working in a single-module Maven project without too much repetition?

Of course, it would have to 'accumulate' test classes as the JDK version goes up, e.g. on JDK 8 I only have the 'regular' test classes, on JDK 16 I have the regular ones and the java16 ones, and on JDK 17 I have the regular ones, the java16 ones and the java17 ones. I haven't found a way yet to express this kind of thing in Maven in a concise way, but I'm not a Maven expert.

Or am I looking in the wrong direction, and is it preferable to make a multi-module Maven project, with the main code in one module, and the tests in another, and then generate a multi-module jar for the tests as well? If so, how would I run this jar file on different JDKs?

3
  • An option could be to run the JUnit tests conditionally? Also ref to Baeldung :) junit-5-conditional-test-execution , junit-conditional-assume
    – Dirk Deyne
    Commented Dec 31, 2021 at 18:47
  • @DirkDeyne I considered that, but I have class files compiled for different versions, so if I run the tests on, say, JDK 8, the class files with records will cause the JVM to fail...
    – jqno
    Commented Dec 31, 2021 at 19:04
  • 1
    You can check github.com/khmarbaise/mrelease might be option as well... Uses a single JDK to build (JDK 17) produces a multi release jar... tests can be made within the appropriate modules (related to the JDK). This can be enhanced using toolchains to really use the appropriate JDK instead of a single JDK...and no it is not a single module solution because I'm the opinion such things should be separated.
    – khmarbaise
    Commented Jan 9, 2022 at 20:27

3 Answers 3

2

To test a MRJAR the classes must be packaged as a jar, so don't use surefire with target/classes, but instead use failsafe during the verify phase. And you must run it at least twice, once per targeted Java version. I would write a unittest, that works for all Java versions, but might skip certain tests.

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assumptions.assumeTrue;

import org.junit.jupiter.api.Test;

class RecordHelperTest
{

    @Test
    void isNotARecord()
    {
        assertFalse( RecordHelper.isRecord(Object.class));
    }

    @Test
    void isARecord() throws Exception
    {
        assumeTrue( Integer.parseInt( System.getProperty( "java.specification.version" ) ) >= 16 );
        
        Class c = Class.forName( "jdk.net.UnixDomainPrincipal" );
        assertTrue( RecordHelper.isRecord(c));
    }

}

How to run it twice, that's up to you, e.g. configure the failsafe plugin twice with different a Toolchain, or rely on a CI server, that builds the project with both JDKs.

https://maven.apache.org/plugins/maven-compiler-plugin/multirelease.html describes several options with their pros and cons as there is no one-fits-all solution.

1
  • 2
    Instead of using assume via system property the @EnabledOnJre or @EnabledForJreRange might be a more convenient solution.
    – khmarbaise
    Commented Jan 9, 2022 at 20:29
1

I ended up creating a multi-module build, as @khmarbaise suggests in one of the comments below the question. He also provides an example.

With modules named core, m16, m17, etc. I can place the bulk of the tests in the core module, and the JDK specific tests in the corresponding modules. I can even test the integration of the JDK-specific code with the rest of the core module, because apparently in this setup, Maven picks the implementation of a class in m17 over the implementation of the same class in core, thereby simulating what happens in a real multi-release jar file. I'm not 100% certain if this is programming by coincidence or not, but it works 😅

Another advantage of a multi-module approach, is that if I use new language features in the JDK-specific submodules, my IDE will just understand it. With the approach in the Baeldung article I cited in the question, that doesn't work and I see squiggles everywhere.

In order to test the build on different JDKs, I use profiles that activate on the JDK version, and that select appropriate submodules, like this:

        <profile>
            <id>modules-jdk8</id>
            <activation>
                <jdk>[1.8,11)</jdk>
            </activation>
            <modules>
                <module>core</module>
            </modules>
        </profile>

        <profile>
            <id>modules-jdk16</id>
            <activation>
                <jdk>[16,17)</jdk>
            </activation>
            <modules>
                <module>core</module>
                <module>m16</module>
            </modules>
        </profile>

        <profile>
            <id>modules-jdk17</id>
            <activation>
                <jdk>[17,)</jdk>
            </activation>
            <modules>
                <module>core</module>
                <module>m16</module>
                <module>m17</module>
                <module>release</module>
            </modules>
        </profile>

(Maybe an approach using Toolchains would have been better.)

Note that there must also be a release module, which has dependencies on all other modules, and which uses the Maven Assembly plugin to actually create the jar file, and which places the JDK-specific classes in the appropriate directories in the jar.

I have added a <maven.install>true</maven.install> property to all modules except the release module, so that only the actual multi-release jar file will be installed into my local repostory. This is important, because I also want to publish to Maven Central, and I don't want to litter it with all kinds of half-product jar files.

1

TL;DR

A Maven multi-module solution like the one from @jqno and @khmarbaise should be preferred over this solution. The Maven Invoker Plugin should be used only when a multi-module setup is not an option.

Detailed Answer

The Maven Invoker Plugin could be used for this purpose.

Following the standard directory layout suggested by the plugin and assuming versions 8, 11, and 16 as JDK targets, JDK specific tests should be structured in separate directories:

  • src/it/jdk-8 for tests targeting the JDK 8.
  • src/it/jdk-11 for tests targeting the JDK 11.
  • src/it/jdk-16 for tests targeting the JDK 16.

Each group should have its own POM where the Java version is specified in the maven-compiler-plugin configuration, for example by using the maven.compiler.release property.

Each test group can run on multiple Java versions without duplicating the test classes. For example, to run JDK 11 tests on both Java 11 and 16 executions:

  • A test JAR should be created in the JDK 11 related POM using the maven-jar-plugin
  • The test JAR should be installed in the local Maven repository used by the invoker (can be done setting invoker.goals = install for the test specific group)
  • The test JAR should be declared as a dependency and added to the Maven Surefire Plugin configuration of the JDK 16 related POM, using the dependenciesToScan parameter.

Pros

  • Single-module projects do not need to switch to a multi-module setup only for test executions.
  • All the complexity is locked into src/it.

Cons

  • Files under src/it are not IDE friendly, e.g., the src/it/jdk-*/pom.xml files are not detected as Maven projects and the src/it/jdk-*/src/test subdirectories are not detected as test sources root.

A full example is available at https://github.com/scordio/invoker-plugin-example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.