Skip to content

Commit f27b560

Browse files
committed
[MNG-7644] Fix version comparison ( .X1 < -X2 for any string qualifier X )
1 parent 29f96a3 commit f27b560

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

maven-artifact/src/main/java/org/apache/maven/artifact/versioning/ComparableVersion.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@
5252
* </ul>
5353
* Unknown qualifiers are considered after known qualifiers, with lexical order (always case insensitive),
5454
* </li>
55-
* <li>a hyphen usually precedes a qualifier, and is always less important than something preceded with a dot.</li>
55+
* <li>a hyphen usually precedes a qualifier, and is always less important than digits/number, for example
56+
* 1.0.RC2 &lt; 1.0-RC3 &lt; 1.0.1 ; but prefer '1.0.0-RC1' over '1.0.0.RC1' </li>
5657
* </ul>
5758
*
5859
* @see <a href="https://cwiki.apache.org/confluence/display/MAVENOLD/Versioning">"Versioning" on Maven Wiki</a>
@@ -570,6 +571,13 @@ public final void parseVersion(String version) {
570571
stack.push(list);
571572
} else if (Character.isDigit(c)) {
572573
if (!isDigit && i > startIndex) {
574+
// 1.0.0.X1 < 1.0.0-X2
575+
// treat .X as -X for any string qualifier X
576+
if (!list.isEmpty()) {
577+
list.add(list = new ListItem());
578+
stack.push(list);
579+
}
580+
573581
list.add(new StringItem(version.substring(startIndex, i), true));
574582
startIndex = i;
575583

@@ -592,6 +600,13 @@ public final void parseVersion(String version) {
592600
}
593601

594602
if (version.length() > startIndex) {
603+
// 1.0.0.X1 < 1.0.0-X2
604+
// treat .X as -X for any string qualifier X
605+
if (!isDigit && !list.isEmpty()) {
606+
list.add(list = new ListItem());
607+
stack.push(list);
608+
}
609+
595610
list.add(parseItem(isDigit, version.substring(startIndex)));
596611
}
597612

maven-artifact/src/test/java/org/apache/maven/artifact/versioning/ComparableVersionTest.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ private Comparable newComparable(String version) {
7171
};
7272

7373
private static final String[] VERSIONS_NUMBER = {
74-
"2.0", "2-1", "2.0.a", "2.0.0.a", "2.0.2", "2.0.123", "2.1.0", "2.1-a", "2.1b", "2.1-c", "2.1-1", "2.1.0.1",
75-
"2.2", "2.123", "11.a2", "11.a11", "11.b2", "11.b11", "11.m2", "11.m11", "11", "11.a", "11b", "11c", "11m"
74+
"2.0", "2.0.a", "2-1", "2.0.2", "2.0.123", "2.1.0", "2.1-a", "2.1b", "2.1-c", "2.1-1", "2.1.0.1", "2.2",
75+
"2.123", "11.a2", "11.a11", "11.b2", "11.b11", "11.m2", "11.m11", "11", "11.a", "11b", "11c", "11m"
7676
};
7777

7878
private void checkVersionsOrder(String[] versions) {
@@ -346,4 +346,20 @@ public void testReuse() {
346346

347347
assertEquals(c1, c2, "reused instance should be equivalent to new instance");
348348
}
349+
350+
/**
351+
* Test <a href="https://issues.apache.org/jira/browse/MNG-7644">MNG-7644</a> edge cases
352+
* 1.0.0.RC1 < 1.0.0-RC2
353+
*/
354+
@Test
355+
public void testMng7644() {
356+
for (String x : new String[] {"alpha", "a", "beta", "b", "milestone", "m", "RC"}) {
357+
// 1.0.0.X1 < 1.0.0-X2 for any string x
358+
checkVersionsOrder("1.0.0." + x + "1", "1.0.0-" + x + "2");
359+
// 2.0.X == 2-X == 2.0.0.X for any string x
360+
checkVersionsEqual("2-" + x, "2.0." + x); // previously ordered, now equals
361+
checkVersionsEqual("2-" + x, "2.0.0." + x); // previously ordered, now equals
362+
checkVersionsEqual("2.0." + x, "2.0.0." + x); // previously ordered, now equals
363+
}
364+
}
349365
}

0 commit comments

Comments
 (0)