Class ComparableVersion

java.lang.Object
org.apache.maven.artifact.versioning.ComparableVersion
All Implemented Interfaces:
Comparable<ComparableVersion>

public class ComparableVersion extends Object implements Comparable<ComparableVersion>

Generic implementation of version comparison.

Features:
  • mixing of '-' (hyphen) and '.' (dot) separators,
  • transition between characters and digits also constitutes a separator: 1.0alpha1 => [1, 0, alpha, 1]
  • unlimited number of version components,
  • version components in the text can be digits or strings,
  • String qualifiers are ordered lexically (case insensitive), with the following exceptions:
    • 'snapshot' < '' < 'sp'
    and alias -> replacement (all case insensitive):
    • 'a' -> 'alpha'
    • 'b' -> 'beta'
    • 'm' -> 'milestone'
    • 'cr' -> 'rc'
    • 'final' -> ''
    • 'final' -> ''
    • 'final' -> ''
  • Following semver rules is encouraged, and some qualifiers are discouraged (no matter the case):
    • The usage of 'CR' qualifier is discouraged. Use 'RC' instead.
    • The usage of 'final', 'ga', and 'release' qualifiers is discouraged. Use no qualifier instead.
    • The usage of 'SP' qualifier is discouraged. Increment the patch version instead.
    For other qualifiers, natural ordering is used (case insensitive):
    • alpha = a < beta = b < milestone = m < rc = cr < snapshot < '' = final = ga = release < sp
  • a hyphen usually precedes a qualifier, and is always less important than digits/number, for example 1.0.RC2 < 1.0-RC3 < 1.0.1 ; but prefer '1.0.0-RC1' over '1.0.0.RC1'
Author:
Kenney Westerhof, Hervé Boutemy
See Also:
  • Constructor Details

    • ComparableVersion

      public ComparableVersion(String version)
  • Method Details

    • parseVersion

      public final void parseVersion(String version)
    • compareTo

      public int compareTo(ComparableVersion o)
      Specified by:
      compareTo in interface Comparable<ComparableVersion>
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • getCanonical

      public String getCanonical()
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • main

      public static void main(String... args)
      Main to test version parsing and comparison.

      To check how "1.2.7" compares to "1.2-SNAPSHOT", for example, you can issue

      java -jar ${maven.repo.local}/org/apache/maven/maven-artifact/${maven.version}/maven-artifact-${maven.version}.jar "1.2.7" "1.2-SNAPSHOT"
      command to command line. Result of given command will be something like this:
       Display parameters as parsed by Maven (in canonical form) and comparison result:
       1. 1.2.7 == 1.2.7
          1.2.7 > 1.2-SNAPSHOT
       2. 1.2-SNAPSHOT == 1.2-snapshot
       
      Parameters:
      args - the version strings to parse and compare. You can pass arbitrary number of version strings and always two adjacent will be compared