apache / apache/maven-toolchains-plugin
Version comparator in ToolchainDiscoverer uses lexicographic String.compareTo instead of numeric version comparison
- Dominant language
- Java
- Stars
- 27
- Forks
- 31
- PR merge metrics
- No merged PRs in 30d
Description
## Summary
The `version()` comparator in `ToolchainDiscoverer.java` uses `String.compareTo()` for version comparison, which is lexicographic rather than numeric. This produces incorrect sort orders for JDK versions with different digit counts.
## Location
`ToolchainDiscoverer.java:290-306`
https://github.com/apache/maven-toolchains-plugin/blob/master/src/main/java/org/apache/maven/plugins/toolchain/jdk/ToolchainDiscoverer.java#L290-L306
## Code
```java
Comparator version() {
return comparing((ToolchainModel tc) -> tc.getProvides().getProperty(VERSION), (v1, v2) -> {
String[] a = v1.split("\\.");
String[] b = v2.split("\\.");
int length = Math.min(a.length, b.length);
for (int i = 0; i < length; i++) {
String oa = a[i];
String ob = b[i];
if (!Objects.equals(oa, ob)) {
if (oa == null || ob == null) {
return oa == null ? -1 : 1;
}
int v = oa.compareTo(ob);
if (v != 0) {
return v;
}
}
}
return a.length - b.length;
})
.reversed();
}
```
## Problem
`String.compareTo()` compares strings lexicographically, not numerically. This causes incorrect ordering:
- `"8" > "11"` (because '8' > '1' in ASCII) -- WRONG, 8 < 11
- `"8" > "17"` -- WRONG
- `"9" > "10"` -- WRONG
- `"10" > "8"` -- WRONG
The `.reversed()` at the end means higher versions should sort first, but this bug corrupts the ordering for any comparison between single-digit and multi-digit major versions.
## Impact
JDK toolchain discovery and selection produces wrong sort order, which means `select-jdk-toolchain` may choose a suboptimal JDK. For example, if JDK 8 and JDK 11 are both available and match requirements, JDK 8 could be incorrectly preferred over JDK 11.
## Suggested Fix
Use proper numeric version comparison (e.g., split segments and compare each segment as integers, or use a dedicated version comparator like `ComparableVersion` from Maven artifact API).
Contributor guide
No contributing guide indexed for this repository
Research direction
Start at ToolchainDiscoverer.java:290-306 and inspect the version() comparator and its reversed ordering. Replace the lexicographic segment comparison with numeric version ordering, then verify that JDK versions such as 8, 10, 11, and 17 sort with the highest version first.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- build-system
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 78/100