It's difficult to compile the classes for a modular JAR file that's intended to work on the class path of a pre-9 release. The `module-info.java` file must be compiled to target 9 (or later), but all the other source files must be compiled to target the earliest release of the platform on which the JAR file is intended to run.
One way to do this is to invoke the compiler twice, once to compile `module-info.java` for 9 (or later), and then again to compile your other source files to Java 8 (or earlier). Under this approach your code is never actually compiled as a module, however, so there's no guarantee that it will actually work as such.
Another way is to invoke the compiler twice, once to compile everything for Java 9 (or later), and then again to compile everything except `module-info.java` to Java 8 (or earlier). This approach ensures that your code compiles as a module, and is suggested by the Maven maintainers [1].
We should consider a small enhancement to javac that would allow a single invocation to compile a module, in module mode [2], but target class files other than `module-info.class` to an earlier release.
One possibility would be to define `--module-source`, `--module-target`, and `--module-release` options, akin to the existing `--source`, `--target`, and `--release` options, that would allow the source and target releases of module declarations to be specified independently of all other compilation units.
[1] https://maven.apache.org/plugins/maven-compiler-plugin/examples/module-info.html
[2] http://openjdk.java.net/jeps/261#Compile-time
One way to do this is to invoke the compiler twice, once to compile `module-info.java` for 9 (or later), and then again to compile your other source files to Java 8 (or earlier). Under this approach your code is never actually compiled as a module, however, so there's no guarantee that it will actually work as such.
Another way is to invoke the compiler twice, once to compile everything for Java 9 (or later), and then again to compile everything except `module-info.java` to Java 8 (or earlier). This approach ensures that your code compiles as a module, and is suggested by the Maven maintainers [1].
We should consider a small enhancement to javac that would allow a single invocation to compile a module, in module mode [2], but target class files other than `module-info.class` to an earlier release.
One possibility would be to define `--module-source`, `--module-target`, and `--module-release` options, akin to the existing `--source`, `--target`, and `--release` options, that would allow the source and target releases of module declarations to be specified independently of all other compilation units.
[1] https://maven.apache.org/plugins/maven-compiler-plugin/examples/module-info.html
[2] http://openjdk.java.net/jeps/261#Compile-time