Reputation: 33
I have been scratching my head for awhile trying to set up my library to use multi-release jars to use java 9+ features with backup java 8 implementations. However, it is only needed for a module of my project.
My current build.gradle for the module looks like this:
apply plugin: 'java'
apply plugin: 'java-library'
dependencies {
compile project(":common")
compile 'org.ow2.asm:asm:6.2'
compile 'org.ow2.asm:asm-util:6.2'
testCompile "junit:junit:$junit_version"
testCompileOnly "com.google.auto.service:auto-service:$autoservice_version"
java9Implementation files(sourceSets.main.output.classesDirs) { builtBy compileJava }
}
compileJava {
sourceCompatibility = 8
targetCompatibility = 8
options.compilerArgs.addAll(['--release', '8'])
}
compileJava9Java {
sourceCompatibility = 9
targetCompatibility = 9
options.compilerArgs.addAll(['--release', '9'])
}
sourceSets {
java9 {
java {
srcDirs = ['src/main/java9']
}
}
}
jar {
into('META-INF/versions/9') {
from sourceSets.java9.output
}
manifest {
attributes(
"Manifest-Version": "1.0",
"Multi-Release": true
)
}
}
However refreshing my build.gradle in intellij is getting me this error:
Could not find method java9Implementation() for arguments [file collection] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
I should also note this is on gradle 4.8.1.
Upvotes: 3
Views: 4000
Reputation: 2354
You're problem is, that your sourceSet
is defined after the dependencies
block. Put it above dependencies
and it should work.
The reference you used also explains it:
A source set represents a set of sources that are going to be compiled together. A jar is built from the result of the compilation of one or more source sets. For each source set, Gradle will automatically create a corresponding compile task that you can configure. This means that if we have sources for Java 8 and sources for Java 9, then they should live in separate source sets. That’s what we do by creating a Java 9 specific source set that will contain the specialized version of our class.
So before you can use 'java9'Implementation, gradle needs to know what 'java9' is supposed to mean.
Upvotes: 1