Skip to content

Commit 45562cd

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

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@
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+
* 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:
58+
* 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>
5759
* </ul>
5860
*
5961
* @see <a href="https://cwiki.apache.org/confluence/display/MAVENOLD/Versioning">"Versioning" on Maven Wiki</a>
@@ -676,6 +678,14 @@ else if ( Character.isDigit( c ) )
676678
{
677679
if ( !isDigit && i > startIndex )
678680
{
681+
// 1.0.0.X1 < 1.0.0-X2
682+
// treat .X as -X for any string qualifier X
683+
if ( !list.isEmpty() )
684+
{
685+
list.add( list = new ListItem() );
686+
stack.push( list );
687+
}
688+
679689
list.add( new StringItem( version.substring( startIndex, i ), true ) );
680690
startIndex = i;
681691

@@ -702,6 +712,14 @@ else if ( Character.isDigit( c ) )
702712

703713
if ( version.length() > startIndex )
704714
{
715+
// 1.0.0.X1 < 1.0.0-X2
716+
// treat .X as -X for any string qualifier X
717+
if ( !isDigit && !list.isEmpty() )
718+
{
719+
list.add( list = new ListItem() );
720+
stack.push( list );
721+
}
722+
705723
list.add( parseItem( isDigit, version.substring( startIndex ) ) );
706724
}
707725

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)