3

We are maintaining a java library and the majority of our customers is still using java 8. Since java 8 is getting a bit rusty, we would like to be able to use new language features of newer java versions, say java 17, at compile time, without shutting older customers down. Is there any possibility to achieve this?

To be precise: I only want to use new language features like var, not new APIs.

How do library-owners usually deal with this?

treeno
  • 2,510
  • 1
  • 20
  • 36
  • 2
    To the extent this is possible at all, it is done by setting the [`-target` of the Java compiler](https://maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-source-and-target.html). – Louis Wasserman Feb 28 '22 at 22:53
  • I tried using sourceCompatibility and targetCompatibilty without success. It is not possible to set a targetCompatibility < sourceCompatibility... as far as I know – treeno Feb 28 '22 at 22:57
  • 2
    Then you're out of luck, and won't be able to use features from any version of Java > 8. – Louis Wasserman Feb 28 '22 at 23:00
  • 4
    Java is steering towards “bundle the application with Java” as preferred deployment anyway. Creating and even optimizing such a setup is the purpose of `jlink`. So, check whether that would work with your customers. While using `var` [would not affect the bytecode compatibility](https://stackoverflow.com/a/49495168/2711488), there are other technical obstacles. [This answer](https://stackoverflow.com/a/56064482/2711488) lists some of them regarding JDK 11 to JDK 8 transformation and you can expect the list to become longer for JDK 17 to JDK 8. – Holger Mar 01 '22 at 10:43

1 Answers1

2

One solution is to release a new major version of your library for JDK 17, and keep the current major version for clients using JDK 8. You would then have to maintain both versions for a while, until your most important customers have migrated to JDK 17 themselves.

When you want to release new features, you would then use a new minor version for each of the two major versions.

gjoranv
  • 4,376
  • 3
  • 21
  • 37