Semver 语义化版本 Java 实现

Semver Java About 3,146 words

Semver4j

开源地址

star185

最后更新:2019.9

https://github.com/vdurmont/semver4j

添加依赖

<dependency>
  <groupId>com.vdurmont</groupId>
  <artifactId>semver4j</artifactId>
  <version>3.1.0</version>
</dependency>

相关代码

不合法的版本号会抛出SemverException异常。

try {
    Semver semver1 = new Semver("a.b.c");
} catch (SemverException e) {
    throw new RuntimeException(e);
}

输出

Caused by: com.vdurmont.semver4j.SemverException: Invalid version (no major version): a.b.c
    at com.vdurmont.semver4j.Semver.<init>(Semver.java:61)
    at com.vdurmont.semver4j.Semver.<init>(Semver.java:20)
    at org.example.SemverTest.main(SemverTest.java:24)

相关API

public class SemverTest {

    public static void main(String[] args) {
        Semver semver = new Semver("1.2.3-beta.4+sha899d8g79f87");
        Integer major = semver.getMajor();
        Integer minor = semver.getMinor();
        Integer patch = semver.getPatch();
        String build = semver.getBuild();
        boolean greaterThan = semver.isGreaterThan("1.3.0");
        boolean lowerThan = semver.isLowerThan("2.0.0-beta");
        System.out.println("major#" + major);
        System.out.println("minor#" + minor);
        System.out.println("patch#" + patch);
        System.out.println("build#" + build);
        System.out.println("greaterThan#" + greaterThan);
        System.out.println("lowerThan#" + lowerThan);
    }

}

输出

major#1
minor#2
patch#3
build#sha899d8g79f87
greaterThan#false
lowerThan#true

jsemver

开源地址

star387

最后更新:2015.3

https://github.com/zafarkhaja/jsemver

添加依赖

<dependency>
    <groupId>com.github.zafarkhaja</groupId>
    <artifactId>java-semver</artifactId>
    <version>0.9.0</version>
</dependency>

相关代码

public class JSemVerTest {

    public static void main(String[] args) {
        Version v = Version.valueOf("1.0.0-rc.1+build.1");
        int major = v.getMajorVersion();
        int minor = v.getMinorVersion();
        int patch = v.getPatchVersion();

        String normal     = v.getNormalVersion();     // "1.0.0"
        String preRelease = v.getPreReleaseVersion(); // "rc.1"
        String build      = v.getBuildMetadata();     // "build.1"
        boolean greaterThan = v.greaterThan(Version.valueOf("1.1.0"));
        boolean lessThan = v.lessThan(Version.valueOf("2.2.2"));

        System.out.println("major#" + major);
        System.out.println("minor#" + minor);
        System.out.println("patch#" + patch);
        System.out.println("normal#" + normal);
        System.out.println("preRelease#" + preRelease);
        System.out.println("build#" + build);
        System.out.println("greaterThan#" + greaterThan);
        System.out.println("lessThan#" + lessThan);

        try {
            Version version = Version.valueOf("a.b.c");
        } catch (Exception e) {
            System.out.println(e);
        }
    }

}

输出

major#1
minor#0
patch#0
normal#1.0.0
preRelease#rc.1
build#build.1
greaterThan#false
lessThan#true
Unexpected character 'LETTER(a)' at position '0', expecting '[DIGIT]'

更多参考

https://www.baeldung.com/java-comparing-versions

Views: 1,856 · Posted: 2023-03-29

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh