gnodet opened a new pull request, #12633: URL: https://github.com/apache/maven/pull/12633
## Summary - When the running JDK does not support a project's `--source`/`--release` level (e.g., JDK 17 cannot compile `--source 6`), Maven now automatically searches configured toolchains for a compatible JDK and selects it for compilation - Hooks into `DefaultToolchainManager.getToolchainFromBuildContext()` as a fallback when no explicit toolchain is set via `maven-toolchains-plugin` - Reads source level from both Model 4.1.0 `<source><targetVersion>` elements and legacy `maven.compiler.release`/`maven.compiler.source` properties ## Implementation - **`JdkSourceLevelSupport`**: New utility mapping JDK major versions to their supported `--source` levels, based on the javac retirement schedule ([JEP 182](https://openjdk.org/jeps/182)): - JDK 9: dropped source 1-5, minimum is 6 - JDK 12: dropped source 6, minimum is 7 - JDK 21: dropped source 7, minimum is 8 - **`DefaultToolchainManager`**: Enhanced `getToolchainFromBuildContext()` with auto-selection: 1. If an explicit toolchain is stored (via `maven-toolchains-plugin`), returns it immediately (no change) 2. For `"jdk"` type only: checks the project's required source level against the running JDK 3. If incompatible, searches configured toolchains for the **newest** compatible JDK 4. Caches the selection and emits warnings: ``` [WARNING] Project requires --source 6 which is not supported by JDK 17. [WARNING] Automatically selected JDK 11 (discovered at /usr/lib/jvm/java-11) for compilation. [WARNING] To suppress this warning, configure the maven-toolchains-plugin explicitly [WARNING] or set <targetVersion> to a value supported by your JDK. ``` ## Test plan - [x] `JdkSourceLevelSupportTest`: 14 tests covering JDK version → source level mapping, normalization of legacy formats (`1.5`→5), dotted versions (`21.0.1`→21), invalid inputs - [x] `DefaultToolchainManagerTest`: 12 new tests covering: - No auto-select when no targetVersion is configured - No auto-select when running JDK supports the level - Auto-select when running JDK doesn't support the level - Prefers newest compatible JDK when multiple available - No auto-select when no compatible toolchain exists - Legacy property detection (`maven.compiler.release`, `maven.compiler.source`) - Full `getToolchainFromBuildContext()` integration - Explicit toolchain takes precedence over auto-selection - Non-JDK types are not affected - `targetVersion` takes precedence over legacy properties - [x] Full `maven-impl` test suite passes (565 tests, 0 failures) - [x] Compat layer tests pass (13 tests, 0 failures) 🤖 Generated with [Claude Code](https://claude.com/claude-code) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
