1. Overview

With the advancement of DevOps technologies, it’s common to build and deploy an application multiple times in a day.

Therefore, every build is assigned a unique version number so we can distinguish between builds. Sometimes, a need arises to compare the version strings programmatically.

In this article, we’ll explore a few ways to compare version strings in Java through various libraries. At last, we’ll write a custom program to handle generic version-string comparison.

2. Using maven-artifact

To start with, let’s explore how Maven handles version comparison.

2.1. Maven Dependency

First, we’ll add the latest maven-artifact Maven dependency to our pom.xml:

<dependency>
    <groupId>org.apache.maven</groupId>
    <artifactId>maven-artifact</artifactId>
    <version>3.6.3</version>
</dependency>

2.2. ComparableVersion

Let’s explore the ComparableVersion class. It provides a generic implementation of version comparison with an unlimited number of version components.

It contains a compareTo method, and the result of the comparison will be greater than or less than 0 when one version is greater than or less than the other, respectively:

ComparableVersion version1_1 = new ComparableVersion("1.1");
ComparableVersion version1_2 = new ComparableVersion("1.2");
ComparableVersion version1_3 = new ComparableVersion("1.3");

assertTrue(version1_1.compareTo(version1_2) < 0);
assertTrue(version1_3.compareTo(version1_2) > 0);

Here, we can confirm that the 1.1 version is less than the 1.2 version, and the 1.3 version is greater than the 1.2 version.

However, we will get 0 as a result when comparing the same versions:

ComparableVersion version1_1_0 = new ComparableVersion("1.1.0");
assertEquals(0, version1_1.compareTo(version1_1_0));

2.3. Version Separators and Qualifiers

Additionally, the ComparableVersion class respects the dot (.) and hyphen (-) as separators, where the dot separates major and minor versions, and the hyphen defines qualifiers:

ComparableVersion version1_1_alpha = new ComparableVersion("1.1-alpha");
assertTrue(version1_1.compareTo(version1_1_alpha) > 0);

Here, we can confirm that the 1.1 version is greater than the 1.1-alpha version.

There are a few well-known qualifiers supported by the ComparableVersion like the alpha, beta, milestone, RC, and snapshot (in the order of lowest to highest):

ComparableVersion version1_1_beta = new ComparableVersion("1.1-beta");
ComparableVersion version1_1_milestone = new ComparableVersion("1.1-milestone");
ComparableVersion version1_1_rc = new ComparableVersion("1.1-rc");
ComparableVersion version1_1_snapshot = new ComparableVersion("1.1-snapshot");

assertTrue(version1_1_alpha.compareTo(version1_1_beta) < 0);
assertTrue(version1_1_beta.compareTo(version1_1_milestone) < 0);
assertTrue(version1_1_rc.compareTo(version1_1_snapshot) < 0);
assertTrue(version1_1_snapshot.compareTo(version1_1) < 0);

Also, it allows us to define unknown qualifiers and respects their order, after the already discussed known qualifiers, with case-insensitive lexical order:

ComparableVersion version1_1_c = new ComparableVersion("1.1-c");
ComparableVersion version1_1_z = new ComparableVersion("1.1-z");
ComparableVersion version1_1_1 = new ComparableVersion("1.1.1");

assertTrue(version1_1_c.compareTo(version1_1_z) < 0);
assertTrue(version1_1_z.compareTo(version1_1_1) < 0);

#java #developer

Version Comparison in Java
5.45 GEEK