Skip to content

Commit ba9efb0

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

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@
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'. and more generally:
57+
* 1.0.X2 &lt; 1.0-X3 &lt; 1.0.1 for any string X ; but prefer '1.0.0-X1' over '1.0.0.X1'.</li>
5658
* </ul>
5759
*
5860
* @see <a href="https://cwiki.apache.org/confluence/display/MAVENOLD/Versioning">"Versioning" on Maven Wiki</a>
@@ -570,6 +572,13 @@ public final void parseVersion(String version) {
570572
stack.push(list);
571573
} else if (Character.isDigit(c)) {
572574
if (!isDigit && i > startIndex) {
575+
// 1.0.0.X1 < 1.0.0-X2
576+
// treat .X as -X for any string qualifier X
577+
if (!list.isEmpty()) {
578+
list.add(list = new ListItem());
579+
stack.push(list);
580+
}
581+
573582
list.add(new StringItem(version.substring(startIndex, i), true));
574583
startIndex = i;
575584

@@ -592,6 +601,13 @@ public final void parseVersion(String version) {
592601
}
593602

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

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

Lines changed: 19 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,21 @@ 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 and more generally:
353+
* 1.0.0.X1 < 1.0.0-X2 for any string X
354+
*/
355+
@Test
356+
public void testMng7644() {
357+
for (String x : new String[] {"abc", "alpha", "a", "beta", "b", "def", "milestone", "m", "RC"}) {
358+
// 1.0.0.X1 < 1.0.0-X2 for any string x
359+
checkVersionsOrder("1.0.0." + x + "1", "1.0.0-" + x + "2");
360+
// 2.0.X == 2-X == 2.0.0.X for any string x
361+
checkVersionsEqual("2-" + x, "2.0." + x); // previously ordered, now equals
362+
checkVersionsEqual("2-" + x, "2.0.0." + x); // previously ordered, now equals
363+
checkVersionsEqual("2.0." + x, "2.0.0." + x); // previously ordered, now equals
364+
}
365+
}
349366
}

0 commit comments

Comments
 (0)