I have an open-source Java library project, for which I would like the built artifact to maintain compatibility with Java 8 as long as possible.
I recently received a pull request, to add a module-info.java
file and setup Gradle for Java 9+ support. Between that PR and the Gradle documentation, I have re-worked the relevant portion of build.gradle
to look like this:
sourceCompatibility = 1.9
targetCompatibility = 1.8
ext.moduleName = 'vault.java.driver'
compileJava {
inputs.property("moduleName", moduleName)
doFirst {
options.compilerArgs = [
'--module-path', classpath.asPath,
]
classpath = files()
}
}
compileTestJava {
inputs.property("moduleName", moduleName)
doFirst {
options.compilerArgs = [
'--module-path', classpath.asPath,
'--add-modules', 'junit',
'--add-reads', "$moduleName=junit",
'--patch-module', "$moduleName=" + files(sourceSets.test.java.srcDirs).asPath,
]
classpath = files()
}
}
This is great, but I'm noticing some serious confusion between Gradle and IntelliJ.
When I import the project in IntelliJ, it does so cleanly, and the project structure clearly shows "Language Level" 9. IntelliJ seems to understand my build.gradle
just fine, as it changes the language level to 8 when I temporarily set sourceCompatibility=1.8
and refresh.
However, when I click "Build Project" from the "Build" pull-down menu, I get the error:
Error:(1, 1) java: modules are not supported in -source 8
(use -source 9 or higher to enable modules)
Meanwhile, when I try to bypass IntelliJ and run ./gradlew build
from the command-line, Gradle doesn't seem to understand my sourceCompatibility
or targetCompatability
settings:
* What went wrong:
Execution failed for task ':compileJava'.
> invalid source release: 1.9
I'm really confused. How does on create a build.gradle
file, which both:
- allows for Java 9+,
- while still maintaining built artifact compatibility with Java 8, and
- makes both Gradle and IntelliJ happy?