Is it possible to create a Gradle Project in a way so that the major part can be used with one Java version (e.g. Java 11) while some parts rely on a higher Java version (e.g. Java 17) and can only be used by clients with Java 17?
It should be possible to use dependencies compiled for Java 17 in Java-17-parts. The part of the project that is compiled for Java 11 should also be usable from the Java 17 code.
It should also be possible to import the project in the exact same way into other projects and use the Java 17 parts of the project only if they use Java 17 by themselves.
Multi-Release JARs can be used for this. Multi-Release JARs allow to create classes that are loaded only in specific Java versions.
You can do that by specifying a source set for Java 17 and keeping your main source set for Java 11. You make the Java 17 source set to use be able to access Java 11 code as well.
Furthermore, you specify a task for compiling Java 17 code with a custom source and target compatibility and the source set for Java 17.
Normal (Java 11) source code goes to
src/main/java
will Java 17 source code goes tosrc/java17/java
.When creating a JAR, the Java 17 build output is moved to
META-INF/versions/16
making it available for Java 16+ only.Using the Kotlin DSL, your
build.gradle.kts
could look like this: