Semver4j

repository·master·Indexed 42 days ago

https://github.com/vdurmont/semver4j

A lightweight Java library for handling semantic versioning. It supports multiple versioning modes including STRICT, NPM, CocoaPods, and Ivy, providing functionality for version comparisons, requirement satisfaction checks via the satisfies() method, and immutable version modification.

Tokens
1.6K
Snippets
6
Records
8
Agent score
4%

What's inside Semver4j

  1. Understand SemverType modes and requirements

    master

    Semver4j supports different versioning modes via SemverType. The mode determines which parts of the version string are mandatory and how the satisfies method interprets requirement strings.

    TypeMandatoryOptional
    STRICTmajor, minor, patchsuffix, build
    LOOSEmajorminor, patch, suffix, build
    NPMmajorminor, patch, suffix, build
    COCOAPODSmajorminor, patch, suffix, build
    IVYmajorminor, patch, suffix, build
  2. Install Semver4j via Maven or Gradle

    master

    Add the following dependency to your project to use Semver4j for semantic versioning handling.

    #### Using maven
    
    ```xml
    <dependency>
      <groupId>com.vdurmont</groupId>
      <artifactId>semver4j</artifactId>
      <version>3.1.0</version>
    </dependency>

    Using gradle

    compile 'com.vdurmont:semver4j:3.1.0'
  3. Compare versions

    master

    Use the following methods to compare a Semver instance against a version string:

    • isGreaterThan(String): Returns true if the version is strictly greater than the other.
    • isLowerThan(String): Returns true if the version is strictly lower than the other.
    • isEqualTo(String): Returns true if the versions are exactly the same (including build information).
    • isEquivalentTo(String): Returns true if the versions are the same, ignoring build information.
    Semver sem = new Semver("1.2.3");
    sem.isGreaterThan("1.2.2"); // true
    sem.isLowerThan("1.2.4"); // true
    
    Semver semEq = new Semver("1.2.3+sha123456789");
    semEq.isEqualTo("1.2.3+sha123456789"); // true
    semEq.isEqualTo("1.2.3+shaABCDEFGHI"); // false
    semEq.isEquivalentTo("1.2.3+shaABCDEFGHI"); // true
  4. Create a Semver object

    master

    You can instantiate a Semver object using one of two constructors. If the provided version string is invalid, a SemverException will be thrown.

    By default, the single-argument constructor uses STRICT mode. To use other modes like NPM, COCOAPODS, LOOSE, or IVY, use the two-argument constructor with SemverType.

    Semver sem1 = new Semver("1.2.3-beta.4+sha899d8g79f87"); // Defaults to STRICT mode
    Semver sem2 = new Semver("1.2.3-beta.4+sha899d8g79f87", SemverType.NPM); // Specify the mode
  5. Find the difference between versions using diff()

    master

    The diff(String) method returns a VersionDiff enum representing the largest difference between the current version and the provided version string.

    Possible VersionDiff values:

    • NONE
    • MAJOR
    • MINOR
    • PATCH
    • SUFFIX
    • BUILD
    Semver sem = new Semver("1.2.3-beta.4+sha899d8g79f87");
    sem.diff("1.2.3-beta.4+sha899d8g79f87"); // NONE
    sem.diff("2.3.4-alpha.5+sha32iddfu987"); // MAJOR
    sem.diff("1.3.4-alpha.5+sha32iddfu987"); // MINOR
    sem.diff("1.2.4-alpha.5+sha32iddfu987"); // PATCH
    sem.diff("1.2.3-alpha.5+sha32iddfu987"); // SUFFIX
    sem.diff("1.2.3-beta.4+sha32iddfu987"); // BUILD
  6. Modify a version using immutable methods

    master

    The Semver object is immutable. To change a version, use methods that return a new Semver instance:

    Incrementing parts:

    • withIncMajor(int increment): Increments the major part.
    • withIncMinor(int increment): Increments the minor part.
    • withIncPatch(int increment): Increments the patch part.

    Clearing parts:

    • withClearedSuffix(): Returns a version with no suffix.
    • withClearedBuild(): Returns a version with no build information.

    Convenience methods for next versions:

    • nextMajor(): e.g., 1.2.3-beta.4+sha32iddfu987 $\rightarrow$ 2.0.0
    • nextMinor(): e.g., 1.2.3-beta.4+sha32iddfu987 $\rightarrow$ 1.3.0
    • nextPatch(): e.g., 1.2.3-beta.4+sha32iddfu987 $\rightarrow$ 1.2.4
  7. Check if a version is stable

    master

    Use Semver#isStable() to determine if a version is stable. A version is considered stable if its major number is strictly positive and it contains no suffix.

    // TRUE
    new Semver("1.2.3").isStable();
    new Semver("1.2.3+sHa.0nSFGKjkjsdf").isStable();
    
    // FALSE
    new Semver("0.1.2").isStable();
    new Semver("0.1.2+sHa.0nSFGKjkjsdf").isStable();
    new Semver("1.2.3-BETA.11+sHa.0nSFGKjkjsdf").isStable();
  8. Check if a version satisfies a requirement

    master

    The satisfies(String) method checks if a version meets a specific requirement string. The interpretation of the requirement depends on the SemverType used:

    • STRICT/LOOSE: The requirement must be another version string.
    • NPM: Supports NPM versioning rules (e.g., >1.2.2, ~1.2.1, ^1.1.1).
    • COCOAPODS: Supports CocoaPods rules (e.g., > 1.2.2, ~> 1.2.1).
    • IVY: Supports Ivy/Gradle rules (e.g., 1.2.+, [0.2,1.4]).
    // NPM mode
    Semver semNPM = new Semver("1.2.3", SemverType.NPM);
    semNPM.satisfies(">1.2.2"); // true
    semNPM.satisfies("~1.2.1"); // true
    
    // COCOAPODS mode
    Semver semPOD = new Semver("1.2.3", SemverType.COCOAPODS);
    semPOD.satisfies("~> 1.2.1"); // true
    
    // IVY mode
    Semver semIVY = new Semver("1.2.3", SemverType.IVY);
    semIVY.satisfies("1.2.+"); // true