rmannibucau commented on code in PR #12633:
URL: https://github.com/apache/maven/pull/12633#discussion_r3873994302


##########
impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultToolchainManager.java:
##########
@@ -98,6 +113,160 @@ public void storeToolchainToBuildContext(@Nonnull Session 
session, @Nonnull Tool
         context.put("toolchain-" + toolchain.getType(), toolchain.getModel());
     }
 
+    /**
+     * Checks whether the running JDK supports the project's required {@code 
--source}/{@code --release}
+     * level. If not, emits a clear, actionable error message instead of 
letting the build fail later
+     * with a cryptic javac error.
+     * <p>
+     * The error tells the user exactly which JDK version they need and how to 
fix it
+     * (run {@code mvnup} to add the {@code maven-toolchains-plugin} with 
automatic JDK discovery).
+     */
+    void checkJdkSourceLevelCompatibility(Session session) {
+        // Avoid repeating the error for every module in --fail-at-end mode
+        if (sourceLevelCheckEmitted) {
+            return;
+        }
+
+        int requiredSourceLevel = getProjectRequiredSourceLevel(session);
+        if (requiredSourceLevel <= 0) {
+            return;
+        }
+
+        int runningJdkMajor = getRunningJdkMajor();
+        if (JdkSourceLevelSupport.supportsSourceLevel(runningJdkMajor, 
requiredSourceLevel)) {
+            return;
+        }
+
+        // Running JDK is incompatible — emit clear, actionable error (once 
per build)
+        sourceLevelCheckEmitted = true;
+        int latestJdk = 
JdkSourceLevelSupport.latestJdkForSourceLevel(requiredSourceLevel);
+        logger.error(
+                "Project requires --source {} which needs JDK <= {}, but the 
running JDK {} no longer supports it.",
+                requiredSourceLevel,
+                latestJdk,
+                runningJdkMajor);
+        logger.error("To fix: run 'mvnup' to add the maven-toolchains-plugin 
with automatic JDK discovery,");
+        logger.error(
+                "or install JDK {} and configure it in toolchains.xml or via 
the maven-toolchains-plugin.", latestJdk);
+    }
+
+    /**
+     * Reads the project's required source level from Model 4.1.0
+     * {@code <source><targetVersion>} elements, legacy properties
+     * ({@code maven.compiler.release}, {@code maven.compiler.source}),
+     * or compiler plugin configuration ({@code <release>}, {@code <source>}).
+     *
+     * @return the required source level as a major version, or {@code -1} if 
none is specified
+     */
+    int getProjectRequiredSourceLevel(Session session) {
+        Optional<Project> current = 
session.getService(Lookup.class).lookupOptional(Project.class);
+        if (current.isEmpty()) {
+            return -1;
+        }
+
+        Project project = current.get();
+
+        // Check Model 4.1.0 <source><targetVersion> elements
+        Build build = project.getModel().getBuild();
+        if (build != null) {
+            List<Source> sources = build.getSources();
+            if (sources != null) {
+                for (Source source : sources) {
+                    String targetVersion = source.getTargetVersion();
+                    if (targetVersion != null && !targetVersion.isEmpty()) {
+                        int level = 
JdkSourceLevelSupport.normalizeSourceLevel(targetVersion);
+                        if (level > 0) {

Review Comment:
   why first one? 
   shouldn't we take them all to have the global constraint since versions will 
slide in jre? 🤔 



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

Reply via email to