You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@maven.apache.org by "gnodet (via GitHub)" <gi...@apache.org> on 2026/09/18 20:49:10 UTC
[PR] Fix #13189: mvnup generates invalid JDK version constraint '(,-1]' [maven]
gnodet opened a new pull request, #13193:
URL: https://github.com/apache/maven/pull/13193
## Summary
When `mvnup` processes a project whose `maven.compiler.release` (or `source`) is **≥ 8** and the running JDK is older than that source level, it incorrectly generates the invalid version constraint `(,-1]` in the toolchains plugin configuration, causing a toolchain resolution failure at build time.
## Root Cause
`ToolchainPluginStrategy.doApply()` calls:
```java
int latestJdk = JdkSourceLevelSupport.latestJdkForSourceLevel(sourceLevel);
addToolchainsPlugin(pomDocument, latestJdk);
```
`latestJdkForSourceLevel()` returns `-1` for source levels 8+ (meaning "still supported by all current JDKs — not retired"). This `-1` was passed directly to `addToolchainsPlugin()`, which inserts it into the version range string, producing:
```xml
<version>(,-1]</version>
```
This triggers:
```
[ERROR] Cannot find matching toolchain definitions for the following toolchain types:{version=(,-1]}
```
**When does this path trigger?** `supportsSourceLevel(runningJdk, sourceLevel)` returns `false` when `sourceLevel > runningJdk` — for example, running JDK 17 with `maven.compiler.release=21`. The toolchain injection path is reached, but `latestJdkForSourceLevel(21)` returns `-1`.
## Fix
Guard against `latestJdk <= 0` before calling `addToolchainsPlugin()`. When `latestJdk <= 0`, the source level is valid for all current JDKs — the running JDK is simply too old for the project's target level. Toolchain injection cannot help (there is no older JDK that supports `--source 21`). Instead, emit a warning explaining that the running JDK needs to be upgraded.
## Test
Added `noToolchainInjectionWhenSourceLevelTooNew` to `ToolchainPluginStrategyTest`:
- Simulates running JDK 17, project with `maven.compiler.release=21`
- Asserts that the POM is **not** modified
- Asserts that the invalid string `(,-1]` never appears in the output
## Related
Fixes #13189. Observed in the `netbeans-html4j` compat test run (gnodet/maven4-testing#39661).
---
_Hermes Agent (Claude Sonnet 4.6) on behalf of Guillaume Nodet_
--
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: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fix #13189: mvnup generates invalid JDK version constraint '(,-1]' [maven]
Posted by "gnodet-bot (via GitHub)" <gi...@apache.org>.
gnodet-bot commented on code in PR #13193:
URL: https://github.com/apache/maven/pull/13193#discussion_r4050770821
##########
impl/maven-cli/src/test/java/org/apache/maven/cling/invoker/mvnup/goals/ToolchainPluginStrategyTest.java:
##########
@@ -658,6 +658,42 @@ int getRunningJdkMajor() {
assertEquals(0, result.modifiedPoms().size());
}
+
+ @Test
+ @DisplayName("should not inject toolchain plugin when source level is too new for running JDK (fixes #13189)")
+ void noToolchainInjectionWhenSourceLevelTooNew() {
+ // Simulate running JDK 17, project targets source 21 (too new, not retired).
+ // latestJdkForSourceLevel(21) returns -1 — must NOT generate '(,-1]' constraint.
+ 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.release>21</maven.compiler.release>
+ </properties>
+ </project>
+ """;
+ Document doc = Document.of(pomXml);
+ UpgradeContext context = TestUtils.createMockContext();
+
+ UpgradeResult result = strategy.doApply(context, Map.of(POM_PATH, doc));
+
+ // POM must not be modified — toolchain cannot help when the source level
+ // is simply newer than the running JDK (no older JDK supports --source 21)
+ assertEquals(0, result.modifiedPoms().size());
+ // The invalid (,-1] version range must never appear in the POM
+ assertFalse(doc.toXml().contains(",-1]"), "POM must not contain invalid version range (,-1]");
Review Comment:
**[low]** The test asserts the POM is unchanged and the invalid string is absent, but doesn't assert that `context.warning()` was called. The warning is the only user-visible signal explaining why mvnup silently skipped the toolchain injection — if it were removed or changed to `context.info()`, no test would catch it.
`TestUtils.createMockContext()` currently only captures `logger.error()` calls. To verify the warning, either:
1. Have `createMockContext()` also capture `warn()` calls and expose them, or
2. Use a Mockito spy on the returned context and verify the call after `doApply()`.
Example approach (option 2):
```java
UpgradeContext context = spy(TestUtils.createMockContext());
UpgradeResult result = strategy.doApply(context, Map.of(POM_PATH, doc));
assertEquals(0, result.modifiedPoms().size());
assertFalse(doc.toXml().contains(",-1]"), "POM must not contain invalid version range (,-1]");
verify(context, atLeastOnce()).warning(contains("too old"));
```
##########
impl/maven-cli/src/test/java/org/apache/maven/cling/invoker/mvnup/goals/ToolchainPluginStrategyTest.java:
##########
@@ -658,6 +658,42 @@ int getRunningJdkMajor() {
assertEquals(0, result.modifiedPoms().size());
}
+
+ @Test
+ @DisplayName("should not inject toolchain plugin when source level is too new for running JDK (fixes #13189)")
Review Comment:
**[nit]** `@DisplayName` strings are for human-readable test report labels — embedding `(fixes #13189)` is non-standard here. The issue reference is already in the commit message. Suggest dropping it:
```suggestion
@DisplayName("should not inject toolchain plugin when source level is too new for running JDK")
```
--
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: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fix #13189: mvnup generates invalid JDK version constraint '(,-1]' [maven]
Posted by "gnodet-bot (via GitHub)" <gi...@apache.org>.
gnodet-bot commented on code in PR #13193:
URL: https://github.com/apache/maven/pull/13193#discussion_r4052492832
##########
impl/maven-cli/src/test/java/org/apache/maven/cling/invoker/mvnup/goals/ToolchainPluginStrategyTest.java:
##########
@@ -512,23 +513,13 @@ int getRunningJdkMajor() {
assertEquals(1, result.modifiedPoms().size());
assertTrue(strategy.hasToolchainsPluginWithSelectGoal(doc));
-
- // Verify that a warning about toolchain JDK availability was emitted
- String xml = doc.toXml();
- assertTrue(xml.contains("select-jdk-toolchain"), "POM should contain select-jdk-toolchain goal");
+ assertTrue(doc.toXml().contains("select-jdk-toolchain"), "POM should contain select-jdk-toolchain goal");
}
@Test
@DisplayName("should emit warning about JDK availability when adding toolchains plugin")
void shouldEmitJdkAvailabilityWarning() {
Review Comment:
**[medium]** `addsPluginForRetiredSourceLevel` and `shouldEmitJdkAvailabilityWarning` are identical: same POM (`--source 6`), same mock context, same strategy instance. The only difference is one asserts `verify(...).warn(...)`. Two tests that duplicate every line of setup are test noise — merge them into a single test that asserts both POM modification and warning emission:
```suggestion
void addsPluginForRetiredSourceLevel() {
// source 6 is retired (last supported by JDK 11) — toolchain must be added
// regardless of the JDK running mvnup, and a warning about JDK availability must be emitted
```
##########
impl/maven-cli/src/test/java/org/apache/maven/cling/invoker/mvnup/goals/ToolchainPluginStrategyTest.java:
##########
@@ -548,25 +539,14 @@ int getRunningJdkMajor() {
assertEquals(1, result.modifiedPoms().size());
assertTrue(strategy.hasToolchainsPluginWithSelectGoal(doc));
-
- // The output should contain the toolchains plugin and version constraint
- String xml = doc.toXml();
- assertTrue(xml.contains("select-jdk-toolchain"), "POM should contain select-jdk-toolchain goal");
- // Verify the warning about JDK availability was emitted
+ assertTrue(doc.toXml().contains("select-jdk-toolchain"), "POM should contain select-jdk-toolchain goal");
verify(context.logger).warn(contains("must be installed"));
}
@Test
- @DisplayName("should not modify POM when source level is compatible")
- void noModificationWhenCompatible() {
- // Simulate running JDK 17, project targets source 11
- ToolchainPluginStrategy strategy = new ToolchainPluginStrategy() {
- @Override
- int getRunningJdkMajor() {
- return 17;
- }
- };
-
+ @DisplayName("should not modify POM when source level is current (not retired)")
+ void noModificationForCurrentSourceLevel() {
Review Comment:
**[high]** The `noToolchainInjectionWhenSourceLevelTooNew` test was the regression pin for #13189: it explicitly asserted `assertFalse(doc.toXml().contains(",-1]"))` against `--source 21` (the exact value that triggered the bug). That assertion is now gone.
The new code still prevents the regression — `latestJdkForSourceLevel(21) = -1 ≤ 0 → continue` before `addToolchainsPlugin` — but there's no test that would fail if someone accidentally removed or inverted that guard in the future. `noModificationForCurrentSourceLevel` uses `--source 11` which exercises a different entry (`latestJdkForSourceLevel` returns `-1` for both, but 11 was never the failure case).
Add the direct regression assertion. Either change this test's source level to `21` and add the `assertFalse`, or add a dedicated test:
```suggestion
void noModificationForCurrentSourceLevel() {
// source 21 is not retired — must not generate '(,-1]' version range (regression: #13189)
```
--
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: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fix #13189: mvnup generates invalid JDK version constraint '(,-1]' [maven]
Posted by "gnodet (via GitHub)" <gi...@apache.org>.
gnodet merged PR #13193:
URL: https://github.com/apache/maven/pull/13193
--
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: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org