You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by gn...@apache.org on 2026/08/06 08:53:29 UTC

(maven) 13/14: Fix mvnup ToolchainPluginStrategy producing invalid version range (,-1]

This is an automated email from the ASF dual-hosted git repository.

gnodet pushed a commit to branch fix-module-name-strategy
in repository https://gitbox.apache.org/repos/asf/maven.git

commit 0e13f823122ffa47ef093f6cd0089cebcfc07023
Author: Guillaume Nodet <gn...@gmail.com>
AuthorDate: Thu Aug 6 09:17:36 2026 +0200

    Fix mvnup ToolchainPluginStrategy producing invalid version range (,-1]
    
    When the project's source level is higher than the running JDK (e.g.
    --source 20 on JDK 17), the project needs a NEWER JDK, not an older
    one. Previously, latestJdkForSourceLevel() returned -1 ("still
    supported") and addToolchainsPlugin() blindly used it, producing the
    invalid version range (,-1] which caused:
    
      Cannot find matching toolchain definitions for the following
      toolchain types:{version=(,-1]}
    
    Now skip adding the toolchains plugin entirely when the source level
    is still supported by modern JDKs — the issue is the running JDK
    being too old, not a source retirement problem.
    
    Discovered via netbeans-html4j which has maven.compiler.source=20
    and failed on JDK 17 runners.
    
    Co-Authored-By: Claude Opus 4.6 <no...@anthropic.com>
---
 .../mvnup/goals/ToolchainPluginStrategy.java       |  8 +++++
 .../mvnup/goals/ToolchainPluginStrategyTest.java   | 34 ++++++++++++++++++++++
 2 files changed, 42 insertions(+)

diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvnup/goals/ToolchainPluginStrategy.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvnup/goals/ToolchainPluginStrategy.java
index 42ff9250ab..8360c0908f 100644
--- a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvnup/goals/ToolchainPluginStrategy.java
+++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvnup/goals/ToolchainPluginStrategy.java
@@ -140,6 +140,14 @@ protected UpgradeResult doApply(UpgradeContext context, Map<Path, Document> pomM
                 }
 
                 int latestJdk = JdkSourceLevelSupport.latestJdkForSourceLevel(sourceLevel);
+                if (latestJdk <= 0) {
+                    // Source level is still supported by modern JDKs — the project needs a newer JDK,
+                    // not an older one. Don't add the toolchains plugin (it would produce an invalid
+                    // version range like "(,-1]"). Let the build fail with a clear "unsupported source" error.
+                    context.success("Project requires --source " + sourceLevel + " which needs a newer JDK than "
+                            + runningJdkMajor + "; upgrade the JDK rather than adding toolchains");
+                    continue;
+                }
                 addToolchainsPlugin(pomDocument, latestJdk);
                 modifiedPoms.add(pomPath);
                 context.success("Added maven-toolchains-plugin with " + SELECT_JDK_TOOLCHAIN_GOAL + " goal (--source "
diff --git a/impl/maven-cli/src/test/java/org/apache/maven/cling/invoker/mvnup/goals/ToolchainPluginStrategyTest.java b/impl/maven-cli/src/test/java/org/apache/maven/cling/invoker/mvnup/goals/ToolchainPluginStrategyTest.java
index 7414d262a1..87020a9d15 100644
--- a/impl/maven-cli/src/test/java/org/apache/maven/cling/invoker/mvnup/goals/ToolchainPluginStrategyTest.java
+++ b/impl/maven-cli/src/test/java/org/apache/maven/cling/invoker/mvnup/goals/ToolchainPluginStrategyTest.java
@@ -483,6 +483,40 @@ int getRunningJdkMajor() {
             assertEquals(0, result.modifiedPoms().size());
         }
 
+        @Test
+        @DisplayName("should not add toolchains plugin when source level is higher than running JDK")
+        void noToolchainWhenSourceTooHigh() {
+            // Simulate running JDK 17, project targets source 20
+            // The project needs a NEWER JDK, not an older one — adding a toolchain
+            // with version range (,-1] would be invalid
+            ToolchainPluginStrategy strategy = new ToolchainPluginStrategy() {
+                @Override
+                int getRunningJdkMajor() {
+                    return 17;
+                }
+            };
+
+            String pomXml = """
+                    <?xml version="1.0" encoding="UTF-8"?>
+                    <project xmlns="http://maven.apache.org/POM/4.0.0">
+                        <modelVersion>4.0.0</modelVersion>
+                        <groupId>com.example</groupId>
+                        <artifactId>test</artifactId>
+                        <version>1.0</version>
+                        <properties>
+                            <maven.compiler.source>20</maven.compiler.source>
+                        </properties>
+                    </project>
+                    """;
+            Document doc = Document.of(pomXml);
+            UpgradeContext context = TestUtils.createMockContext();
+
+            UpgradeResult result = strategy.doApply(context, Map.of(POM_PATH, doc));
+
+            assertEquals(0, result.modifiedPoms().size());
+            assertFalse(strategy.hasToolchainsPluginWithSelectGoal(doc));
+        }
+
         @Test
         @DisplayName("should not add duplicate plugin when already present")
         void noDuplicatePlugin() {