Groovy web console

subscribe to the feed Subscribe
to this
site

Untitled

Published 2 months ago by Anonymous
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
versionA = '3.6.20.0'
versionB = '3.6.18.0'

// Split the version numbers into their individual components and remove any potential snapshot modifiers.
def versionAComponents = versionA.replace('-SNAPSHOT','').split("\\.");
def versionBComponents = versionB.replace('-SNAPSHOT','').split("\\.");

// Remember if any of the two version numbers are snapshot versions.
def isVersionASnapshot = versionA.contains("SNAPSHOT");
def isVersionBSnapshot = versionB.contains("SNAPSHOT");

assert versionAComponents.length == versionBComponents.length : 'The provided version numbers are not equal in length. Please provide version numbers with the same amount of dots in them';

def comparisonResult = 0;

// Unfortunately, eachWithIndex does not allow aborting the loop if a result is found.
for (index in 0..(versionAComponents.length - 1)) {
    if(versionAComponents[index] < versionBComponents[index]) {
        comparisonResult = -1
        break;
    } else if (versionAComponents[index] > versionBComponents[index]) {
        comparisonResult = 1
        break;
    }
}

// If they are identical in terms of their version digits we compare if either of them has a "-SNAPSHOT" identifier which is considered lower with otherwise identical version digits.
if(comparisonResult == 0) {
    if(isVersionASnapshot == true &&  isVersionBSnapshot == false) {
        comparisonResult = -1
    } else if(isVersionASnapshot == false &&  isVersionBSnapshot == true) {
        comparisonResult = 1
    }
}

print comparisonResult