Is there practical reasoning to enforce JDK version for build?

Igor Konoplyanko :

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...

Tomasz Linkowski :

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.class was 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.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=88003&siteId=1