java: prefer to use maven.compiler.release
Currrently our pom.xml.in are using:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
</properties>But since Java 9 released, the compiler introduced the -release flag to solve a long-standing issue with cross-compilation.
The Pitfall is if we compile our project on a newer JDK (such as JDK 25 (most distro) or JDK 27 (brew)),
- The compiler ensures your syntax is compliant with Java 21 (source).
- The compiler generates bytecode compatible with Java 21 JVMs (target).
However, the compiler links against the boot classpath of your current running JDK (JDK 25/27). If accidentally (swig) code use an API or method that was introduced in Java 23, code will compile perfectly but will crash at runtime with a NoSuchMethodError or NoClassDefFoundError when run on a Java 21 virtual machine.
If using <maven.compiler.release>21</maven.compiler.release> property instead.
This flag automatically:
- Sets the source compatibility to Java 21.
- Sets the target compatibility to Java 21 bytecode.
- Strictly compiles against the Java 21 platform APIs.
Then any use of API or library method that does not exist in JDK 21, the compilation will fail immediately on the build machine, preventing runtime errors in production.
Source: google/or-tools