com.itemis.maven.plugins.unleash.util.MavenVersionUtil Maven / Gradle / Ivy
package com.itemis.maven.plugins.unleash.util;
import com.google.common.base.Objects;
import com.google.common.base.Strings;
/**
* A utility class for maven version management.
*
* @author Stanley Hillner
*/
public final class MavenVersionUtil {
public static final String VERSION_QUALIFIER_SNAPSHOT = "-SNAPSHOT";
public static final String VERSION_LATEST = "LATEST";
private MavenVersionUtil() {
// static utility methods only
}
/**
* Checks whether the passed version represents a SNAPSHOT version by checking if the version String ends with
* '-SNAPSHOT' or equals 'LATEST'.
*
* @param version the version to check
* @return {@code true} if the version String represents a SNAPSHOT version.
*/
public static boolean isSnapshot(String version) {
return version.toUpperCase().endsWith(VERSION_QUALIFIER_SNAPSHOT) || version.toUpperCase().equals(VERSION_LATEST);
}
/**
* Calculates the next higher SNAPSHOT version of the passed version String by incrementing the lowest possible number
* segment of the version String by one.
* Some examples:
*
*
* Input version
* Result
*
*
* 1.0.0
* 1.0.1-SNAPSHOT
*
*
* 1.12
* 1.13-SNAPSHOT
*
*
* 3-SNAPSHOT
* 4-SNAPSHOT
*
*
* 1.3-SNAPSH
* 1.4-SNAPSH-SNAPSHOT
*
*
* 3-Alpha1
* 3-Alpha2-SNAPSHOT
*
*
* 3-Alpha1-SNAPSHOT
* 3-Alpha2-SNAPSHOT
*
*
*
* @param version the current version from which the follow-up SNAPSHOT version shall be calculated.
* @return the next higher SNAPSHOT version.
*/
public static String calculateNextSnapshotVersion(String version) {
StringBuilder sb;
if (version.endsWith(VERSION_QUALIFIER_SNAPSHOT)) {
sb = new StringBuilder(version.substring(0, version.length() - VERSION_QUALIFIER_SNAPSHOT.length()));
} else {
sb = new StringBuilder(version);
}
int start = -1;
int end = -1;
for (int i = sb.length() - 1; i >= 0; i--) {
try {
Integer.parseInt(sb.substring(i, i + 1));
if (end == -1) {
end = i;
}
} catch (NumberFormatException e) {
if (end > 0 && start == -1) {
start = i + 1;
break;
}
}
}
if (end >= 0 && start == -1) {
start = 0;
}
int versionSegmentToIncrease = Integer.parseInt(sb.substring(start, end + 1));
sb.replace(start, end + 1, Integer.toString(versionSegmentToIncrease + 1));
sb.append(VERSION_QUALIFIER_SNAPSHOT);
return sb.toString();
}
/**
* Calculates a release version String based on the passed version String which may (or not) be a SNAPSHOT version
* String.
* Some examples:
*
*
* Input version
* Result
*
*
* 1.0.0
* 1.0.0
*
*
* 1.12-SNAPSHOT
* 1.12
*
*
* 3-SNAPSHOT
* 3-SNAPSHOT
*
*
*
* @param version the current version from which the release version shall be calculated.
* @return the calculated release version which might be identical to the passed version.
*/
public static String calculateReleaseVersion(String version) {
if (version.toUpperCase().endsWith(VERSION_QUALIFIER_SNAPSHOT)) {
return version.substring(0, version.length() - VERSION_QUALIFIER_SNAPSHOT.length());
}
return version;
}
/**
* Checks whether version1 is newer than version 2.
*
* @param version1 the first version to check
* @param version2 the second version to check (the anchestor)
* @return {@code true} if version1 is newer than version2.
*/
public static boolean isNewerVersion(String version1, String version2) {
String v1 = Strings.emptyToNull(version1);
String v2 = Strings.emptyToNull(version2);
if (Objects.equal(v1, v2)) {
return false;
} else if (v1 == null) {
return false;
} else if (v2 == null) {
return true;
} else if (Objects.equal(VERSION_LATEST, v1)) {
return true;
} else if (Objects.equal(VERSION_LATEST, v2)) {
return false;
}
String commonPrefix = Strings.commonPrefix(v1, v2);
v1 = v1.substring(commonPrefix.length());
v2 = v2.substring(commonPrefix.length());
String commonSuffix = Strings.commonSuffix(v1, v2);
v1 = v1.substring(0, v1.length() - commonSuffix.length());
v2 = v2.substring(0, v2.length() - commonSuffix.length());
if (v1.isEmpty()) {
if (Objects.equal(VERSION_QUALIFIER_SNAPSHOT, v2.toUpperCase())) {
return true;
} else {
return false;
}
} else if (Objects.equal(VERSION_QUALIFIER_SNAPSHOT, v1.toUpperCase())) {
return false;
} else {
if (Objects.equal(VERSION_QUALIFIER_SNAPSHOT, v2.toUpperCase())) {
return true;
} else {
return v1.compareTo(v2) > 0;
}
}
}
}