There exists maven enforcer plugin which can enforce build to be run only on specific JDK version.
I wonder if there any practical reasoning to have this? We have already build configuration to specify source and target versions. As far as I understand this should be more than enough, because Java is backward compatible. For example how it looks in gradle:
compileJava {
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
}
That's how it looks in maven:
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
If you have seen any reason to require exact jdk version - could you please also write it down.
UPD. question is more about if there is any practical difference to compile project of java source/target of version 8 with JDK's of 8,9,10 or 11...
The main reason for this might be some better optimizations in the compiler of the newer JDK. So even though the target bytecode level is the same as with the older compiler, the target bytecode itself might be improved.
According to Brian Goetz, this pulls its weight:
There are times when a better translation from source code to bytecode is made possible by JVM improvements. For example, prior to 5,
Foo.classwas translated to reflective call; after, to LDC.So, you may reasonably want to stick to a given language level across an org (because of shared code) but specific apps can still take advantage of VM improvements.
Edit: Sorry! The quoted tweet is about compiling with source lower than target (e.g. -source 8 -target 11), so it's different to what the OP asked about. Still, perhaps the newer compiler can produce better bytecode even when the target remains unchanged.
PS. As advised by Basil, let me mention the javac --release flag of JDK 9+, which prevents from using APIs of the newer JDKs while sticking to older language levels.
Not at all. Source is mainly about syntax. Target is about the specific byte code magic number and capabilities.
But you could very well write Java 7 conforming code that uses a single class X that only jdk 8 or newer provides. When you then use a Java 8 jdk, that compiler will find that class X and build fine. But when running that code on a Java 7 jvm, the Java 8 class X is missing, leading to a runtime exception.
So: forcing the jdk prevents you from using classes that were added to Java after your target version.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With