Re: [DISCUSS] Automatic JDK toolchain selection when running JDK is incompatible (4.1.0)
Romain Manni-Bucau <[email protected]> Mon, 3 Aug 2026 16:45:22 +0200
| Newsgroups | gmane.comp.jakarta.turbine.maven.devel |
|---|---|
| Message-ID | <CACLE=7PpXxz7mtVYKNmEJtuSfvuTSFsNKe1GWLjvEKJOnP1xTg@mail.gmail.com> |
--00000000000027c31406582597d3 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hi, All good like that for me, I'd maybe ensure there is a warning for 3 (and not just an info) detailling how to fix it - installing the right version or setting up toolchain but as soon as we fall in best effort case, no need to be fancy or 100% accurate IMHO. Even just a "first matching" distro sounds good enough. Romain Manni-Bucau @rmannibucau <https://x.com/rmannibucau> | .NET Blog <https://dotnetbirdie.github.io/> | Blog <https://rmannibucau.github.io/> |= Old Blog <http://rmannibucau.wordpress.com> | Github <https://github.com/rmannibucau> | LinkedIn <https://www.linkedin.com/in/rmannibucau> | Book <https://www.packtpub.com/en-us/product/java-ee-8-high-performance-97817884= 73064> Javaccino <https://javaccino.dev/> founder (Java/.NET service - contact via linkedin) Le lun. 3 ao=C3=BBt 2026 =C3=A0 14:18, Guillaume Nodet <[email protected]> = a =C3=A9crit : > Hi all, > > I've opened a draft PR for a quality-of-life feature in Maven 4.1.0: > automatic JDK toolchain selection when the running JDK cannot compile the > project's declared source/release level. > > PR: https://github.com/apache/maven/pull/12633 > > The problem > > When a project declares <maven.compiler.source>6</maven.compiler.source> > (or uses <release>, <targetVersion>, or compiler plugin > <configuration><source>), and you run Maven with JDK 21 =E2=80=94 which d= ropped > --source 6 support =E2=80=94 the build fails with a cryptic javac error. = The user > has to figure out they need to install a compatible JDK and either > configure toolchains.xml or add the maven-toolchains-plugin to their buil= d. > This is a frequent stumbling block, especially when maintaining older > projects. > > The solution > > Maven now automatically detects the incompatibility and searches for a > compatible JDK =E2=80=94 first in configured toolchains (toolchains.xml),= then by > lazily discovering JDK installations on the filesystem. If a compatible J= DK > is found, it's selected as the compilation toolchain and a warning is > emitted: > > [WARNING] Project requires --source 6 which is not supported by JDK 21. > [WARNING] Automatically selected JDK 11 (discovered at > /usr/lib/jvm/java-11) for compilation. > > > This is a zero-cost feature: the auto-selection logic only runs when the > running JDK genuinely cannot handle the project's source level. Normal > builds are completely unaffected. > > Design decisions worth discussing > > 1. Filesystem discovery =E2=80=94 The discoverer scans well-known locatio= ns > (SDKMAN, IntelliJ .jdks/, Gradle, jEnv, JBang, asdf, mise, OS-specific > paths like /usr/lib/jvm). Version is read from the JDK release file =E2= =80=94 no > java processes are spawned. This is essentially what > maven-toolchains-plugin's ToolchainDiscoverer does, but moved into core a= nd > made lazy. Does this make the plugin's auto-discovery redundant? Should w= e > deprecate it? > > 2. Source level detection =E2=80=94 We read the source level from multipl= e places > in priority order: Model 4.1.0 <source><targetVersion>, then > maven.compiler.release/maven.compiler.source properties, then compiler > plugin <configuration><release>/<source>. Is this the right precedence? A= re > there other places we should check? > > 3. "Newest compatible" strategy =E2=80=94 When multiple compatible JDKs a= re found, > we pick the newest one (highest major version that still supports the > required source level). The rationale is that a newer JDK gives better > performance and diagnostics. Should this be configurable? > > 4. Compat layer =E2=80=94 The v3 ToolchainManagerFactory bridge now passe= s the > discoverer through, so plugins using the v3 API also benefit. This felt > important for the transition period. > > CI is green on all platforms (Linux/macOS/Windows =C3=97 JDK 17/21/25). > > Feedback and reviews welcome. > > Guillaume > --00000000000027c31406582597d3--