Skip to content

Commit d60a42e

Browse files
committed
[MNG-7644] Fix version comparison where .X1 < -X2 for any string qualifier X
1 parent 7d45894 commit d60a42e

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@
5353
* </ul>
5454
* Unknown qualifiers are considered after known qualifiers, with lexical order (always case insensitive),
5555
* </li>
56-
* <li>a hyphen usually precedes a qualifier, and is always less important than something preceded with a dot.</li>
56+
* <li>a hyphen usually precedes a qualifier, and is always less important than digits/number, for example
57+
* {@code 1.0.RC2 < 1.0-RC3 < 1.0.1}; but prefer {@code 1.0.0-RC1} over {@code 1.0.0.RC1}, and more
58+
* generally: {@code 1.0.X2 < 1.0-X3 < 1.0.1} for any string {@code X}; but prefer {@code 1.0.0-X1}
59+
* over {@code 1.0.0.X1}.</li>
5760
* </ul>
5861
*
5962
* @see <a href="https://cwiki.apache.org/confluence/display/MAVENOLD/Versioning">"Versioning" on Maven Wiki</a>
@@ -676,6 +679,14 @@ else if ( Character.isDigit( c ) )
676679
{
677680
if ( !isDigit && i > startIndex )
678681
{
682+
// 1.0.0.X1 < 1.0.0-X2
683+
// treat .X as -X for any string qualifier X
684+
if ( !list.isEmpty() )
685+
{
686+
list.add( list = new ListItem() );
687+
stack.push( list );
688+
}
689+
679690
list.add( new StringItem( version.substring( startIndex, i ), true ) );
680691
startIndex = i;
681692

@@ -702,6 +713,14 @@ else if ( Character.isDigit( c ) )
702713

703714
if ( version.length() > startIndex )
704715
{
716+
// 1.0.0.X1 < 1.0.0-X2
717+
// treat .X as -X for any string qualifier X
718+
if ( !isDigit && !list.isEmpty() )
719+
{
720+
list.add( list = new ListItem() );
721+
stack.push( list );
722+
}
723+
705724
list.add( parseItem( isDigit, version.substring( startIndex ) ) );
706725
}
707726

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
@@ -51,8 +51,8 @@ private Comparable newComparable( String version )
5151
"1-1", "1-2", "1-123" };
5252

5353
private static final String[] VERSIONS_NUMBER =
54-
{ "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",
55-
"2.2", "2.123", "11.a2", "11.a11", "11.b2", "11.b11", "11.m2", "11.m11", "11", "11.a", "11b", "11c", "11m" };
54+
{ "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",
55+
"2.123", "11.a2", "11.a11", "11.b2", "11.b11", "11.m2", "11.m11", "11", "11.a", "11b", "11c", "11m" };
5656

5757
private void checkVersionsOrder( String[] versions )
5858
{
@@ -337,4 +337,21 @@ public void testReuse()
337337

338338
assertEquals( "reused instance should be equivalent to new instance", c1, c2 );
339339
}
340+
341+
/**
342+
* Test <a href="https://issues.apache.org/jira/browse/MNG-7644">MNG-7644</a> edge cases
343+
* 1.0.0.RC1 < 1.0.0-RC2 and more generally:
344+
* 1.0.0.X1 < 1.0.0-X2 for any string X
345+
*/
346+
public void testMng7644()
347+
{
348+
for ( String x : new String[]{ "abc", "alpha", "a", "beta", "b", "def", "milestone", "m", "RC" } ) {
349+
// 1.0.0.X1 < 1.0.0-X2 for any string x
350+
checkVersionsOrder( "1.0.0." + x + "1", "1.0.0-" + x + "2" );
351+
// 2.0.X == 2-X == 2.0.0.X for any string x
352+
checkVersionsEqual( "2-" + x, "2.0." + x ); // previously ordered, now equals
353+
checkVersionsEqual( "2-" + x, "2.0.0." + x ); // previously ordered, now equals
354+
checkVersionsEqual( "2.0." + x, "2.0.0." + x ); // previously ordered, now equals
355+
}
356+
}
340357
}

0 commit comments

Comments
 (0)