Java9 introduces with Multi-Release JARs.
Let\'s say that I have multimodule Gradle project using java8:
project-root
settings.gradle
build.g
As mentioned in the comments on the question, this blog post and the associated example project describe how to create a multi-release JAR with Gradle.
In case the blog post or example project should go away, you may also refer to the following setup which was derived from the example project and tailored a bit to the setup that is given in the question (as far as details are provided).
project-root/
├── build.gradle
├── module1
│ └── src
│ └── main
│ ├── java
│ │ └── com
│ │ └── acme
│ │ ├── JdkSpecific.java
│ │ └── Shared.java
│ └── java9
│ └── com
│ └── acme
│ └── JdkSpecific.java
├── module2
│ └── whatever
├── module3
│ └── whatever
└── settings.gradle
build.gradle
allprojects {
apply plugin: 'java'
compileJava {
sourceCompatibility = 8
targetCompatibility = 8
}
}
dependencies {
implementation project(':module1')
}
project(':module1') {
sourceSets {
java9 {
java {
srcDirs = ['src/main/java9']
}
}
}
compileJava9Java {
sourceCompatibility = 9
targetCompatibility = 9
}
dependencies {
java9Implementation files(sourceSets.main.output.classesDirs) {
builtBy compileJava
}
}
jar {
into('META-INF/versions/9') {
from sourceSets.java9.output
}
manifest.attributes('Multi-Release': 'true')
}
}
settings.gradle
include 'module1', 'module2', 'module3'