@@ -495,13 +495,72 @@ patchModules.config = [
495
495
Compilation
496
496
===
497
497
498
+ Compilation to a specific Java release
499
+ ----
500
+
501
+ You might want to run your builds on a recent JDK (e.g. JDK 12), but target an older version of Java, e.g.:
502
+ - Java 11, which is the current [ Long-Term Support (LTS) release] ( https://www.oracle.com/technetwork/java/java-se-support-roadmap.html ) ,
503
+ - Java 8, whose production use in 2018 was almost 85%, according to [ this survey] ( https://www.baeldung.com/java-in-2018 ) .
504
+
505
+ You can do that by setting the Java compiler [ ` --release ` ] [ javacRelease ] option
506
+ (e.g. to ` 6 ` for Java 6, etc.). Note that when you build using:
507
+ - JDK 11: you can only target Java 6-11 using its
508
+ [ ` --release ` ] ( https://docs.oracle.com/en/java/javase/11/tools/javac.html ) option,
509
+ - JDK 12: you can only target Java 7-12 using its
510
+ [ ` --release ` ] ( https://docs.oracle.com/en/java/javase/12/tools/javac.html ) option,
511
+ - etc.
512
+
513
+ Finally, note that JPMS was introduced in Java 9, so you can't compile ` module-info.java ` to Java release 6-8
514
+ (this plugin provides a workaround for that, though &mdash ; see below).
515
+
516
+ Concluding, to configure your project to support JPMS and target:
517
+ - Java ** 6-8** : call the [ ` modularity.mixedJavaRelease ` ] [ ModularityExtension ] function
518
+ (see [ Separate compilation of ` module-info.java ` ] ( #separate-compilation-of-module-infojava ) for details),
519
+ - Java ** 9+** : call the [ ` modularity.standardJavaRelease ` ] [ ModularityExtension ] function,
520
+
521
+ and the plugin will take care of setting the [ ` --release ` ] [ javacRelease ] option(s) appropriately.
522
+
523
+
498
524
Separate compilation of ` module-info.java `
499
525
----
500
526
501
527
If you need to compile the main ` module-info.java ` separately from the rest of ` src/main/java `
502
528
files, you can enable ` compileModuleInfoSeparately ` option on ` compileJava ` task. It will exclude ` module-info.java `
503
529
from ` compileJava ` and introduce a dedicated ` compileModuleInfoJava ` task.
504
530
531
+ Typically, this feature would be used by libraries which target JDK 6-8 but want to make the most of JPMS by:
532
+ - providing ` module-info.class ` for consumers who put the library on module path,
533
+ - compiling ` module-info.java ` against the remaining classes of this module and against other modules
534
+ (which provides better encapsulation and prevents introducing split packages).
535
+
536
+ This plugin provides an easy way to do just that by means of its
537
+ [ ` modularity.mixedJavaRelease ` ] [ ModularityExtension ] function, which implicitly sets
538
+ ` compileJava.compileModuleInfoSeparately = true ` and configures the [ ` --release ` ] [ javacRelease ] compiler options.
539
+
540
+ For example, if your library targets JDK 8, and you want your ` module-info.class ` to target JDK 9
541
+ (default), put the following line in your ` build.gradle(.kts) ` :
542
+
543
+ <details open >
544
+ <summary >Groovy DSL</summary >
545
+
546
+ ``` groovy
547
+ modularity.mixedJavaRelease 8
548
+ ```
549
+
550
+ </details >
551
+ <details >
552
+ <summary >Kotlin DSL</summary >
553
+
554
+ ``` kotlin
555
+ modularity.mixedJavaRelease(8 )
556
+ ```
557
+
558
+ </details >
559
+
560
+ Note that ` modularity.mixedJavaRelease ` does * not* configure a
561
+ [ multi-release JAR] ( https://docs.oracle.com/javase/9/docs/specs/jar/jar.html#Multi-release )
562
+ (in other words, ` module-info.class ` remains in the root directory of the JAR).
563
+
505
564
Limitations
506
565
===
507
566
@@ -524,3 +583,7 @@ Contributions are very much welcome.
524
583
Please open a Pull Request with your changes.
525
584
Make sure to rebase before creating the PR so that the PR only contains your changes, this makes the review process much easier.
526
585
Again, bonus points for providing tests for your changes.
586
+
587
+
588
+ [ javacRelease ] : http://openjdk.java.net/jeps/247
589
+ [ ModularityExtension ] : src/main/java/org/javamodularity/moduleplugin/extensions/ModularityExtension.java
0 commit comments