I am trying to keep my java project compatible with java 17 but actually compile and ships the jars with Java 21 compatibility level.
The rationale being that I am releasing a new major version and I would like to have Java 21 for baseline, but it might be a problem for some users. So the idea is to ship in Java 21 but keep the sources compatible with 17 in order to be able to bring back Java 17 build in a minor release if a user cannot upgrade with a valid reason. If all the users can migrate to Java 21 without issue, I will be able to use Java 21 only features without having to wait for a new major version of my software.
I am puzzled when configuring maven-compiler-plugin.
To ensure compatibility, the plan is:
- Have jdk 21 installed
- have the following configuration
maven-compiler-plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source> 17 </source>
<target> 21 </target>
<release> 17 </release>
</configuration>
</plugin>
However, after reading What is the --release flag in the Java 9 compiler?, I am wondering if there will be a conflict between the release flag and the others.
- I also intend to actually build and run in nightly CI runs on JDK 17, with only a JDK 17 installed:
<profile>
<id>test-nightly</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source> 17 </source>
<target> 17 </target>
<release> 17 </release>
</configuration>
</plugin>
</plugins>
</build>
</profile>
Am I configuring the maven-compiler-plugin correctly ?
Thanks in advance
After some investigation, it is not possible to do via the java build flags. The
javacdocumentation says:And especially the line Note: When using --release, you cannot also use the --source/-source or --target/-target options.
The solution I went for is based on: