Skip to content

Commit 09043b2

Browse files
committed
[MNG-7644] Fix version comparison ( .RC1 < -RC2 )
1 parent 7d45894 commit 09043b2

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@
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' </li>
5758
* </ul>
5859
*
5960
* @see <a href="https://cwiki.apache.org/confluence/display/MAVENOLD/Versioning">"Versioning" on Maven Wiki</a>
@@ -676,6 +677,14 @@ else if ( Character.isDigit( c ) )
676677
{
677678
if ( !isDigit && i > startIndex )
678679
{
680+
// 1.0.0.RC1 < 1.0.0-RC2
681+
// treat .RC as -RC
682+
if ( !list.isEmpty() )
683+
{
684+
list.add( list = new ListItem() );
685+
stack.push( list );
686+
}
687+
679688
list.add( new StringItem( version.substring( startIndex, i ), true ) );
680689
startIndex = i;
681690

@@ -702,6 +711,14 @@ else if ( Character.isDigit( c ) )
702711

703712
if ( version.length() > startIndex )
704713
{
714+
// 1.0.0.RC1 < 1.0.0-RC2
715+
// treat .RC as -RC
716+
if ( !isDigit && !list.isEmpty() )
717+
{
718+
list.add( list = new ListItem() );
719+
stack.push( list );
720+
}
721+
705722
list.add( parseItem( isDigit, version.substring( startIndex ) ) );
706723
}
707724

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

Lines changed: 12 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,14 @@ 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
344+
*/
345+
public void testMng7644()
346+
{
347+
checkVersionsOrder( "1.0.0.RC1", "1.0.0-RC2" );
348+
checkVersionsEqual( "2.0.a", "2.0.0.a" ); // previously ordered, now equals
349+
}
340350
}

0 commit comments

Comments
 (0)