public class SemanticVersion extends java.lang.Object implements java.io.Serializable, java.lang.Comparable<SemanticVersion>
toString() to obtain the version string. However often there is
a need for other, more detailed inspection of version number structure.
When adopting Semantic Versioning approach, a convenient class is made available by this library for Gradle scripts. The version may be defined either directly in the script or loaded from an external resource, such as a file or even URL.
import cz.auderis.tools.gradle.SemanticVersion
// build.gradle
buildscript {
// Define where to find the plugin module, e.g. mavenCentral()
}
// Use direct specification
project.version = SemanticVersion.is("1.0.0-SNAPSHOT")
// Load version specification from a file
project version = SemanticVersion.from("version.txt")
An instance of SemanticVersion class has, among others, the following properties:
majorVersion (with convenient alias major) returns first
numeric part X from version X.Y.ZminorVersion (with convenient alias minor) returns numeric
part Y from version X.Y.ZpatchRevision (with convenient aliases patch and patchLevel)
returns numeric part Z from version X.Y.Zprerelease is a boolean flag that indicates that either major version is 0
or there are pre-release identifiers present (part P in version string X.Y.Z-P).
There is an alias preRelease available as well.stable is a boolean flag that is effectively a negation of prerelease propertysnapshot is a boolean flag that indicates whether a special pre-release ID
SNAPSHOT is present in the version string (i.e. "1.4.2-SNAPSHOT")| Modifier and Type | Class and Description |
|---|---|
static class |
SemanticVersion.OrderBy |
| Modifier and Type | Field and Description |
|---|---|
static java.util.regex.Pattern |
IDENTIFIER_PATTERN |
static java.util.regex.Pattern |
PATTERN |
static java.lang.String |
SNAPSHOT_ID |
| Modifier and Type | Method and Description |
|---|---|
int |
compareTo(SemanticVersion other) |
boolean |
equals(java.lang.Object obj) |
static SemanticVersion |
from(java.io.File versionHolder)
Loads a version string from a file and parses it into an instance of semantic version.
|
static SemanticVersion |
from(java.io.InputStream stream)
Loads a version string from the provided stream and parses it into an instance of semantic version.
|
static SemanticVersion |
from(java.lang.String source)
Tries to interpret the
source argument as URL and uses it to parse a version string from its contents;
if the argument is not a valid URL, it is considered to be a local file path. |
static SemanticVersion |
from(java.net.URL url)
Loads a version string from the provided URL and parses it into an instance of semantic version.
|
java.util.List<java.lang.String> |
getBuildMetadataIdentifiers() |
protected java.util.List<java.lang.String> |
getBuildMetadataIds() |
int |
getMajor()
Deprecated.
|
int |
getMajorVersion() |
int |
getMinor()
Deprecated.
|
int |
getMinorVersion() |
int |
getPatch()
Deprecated.
|
int |
getPatchLevel()
Deprecated.
|
int |
getPatchRevision() |
java.util.List<java.lang.String> |
getPreReleaseIdentifiers() |
protected java.util.List<java.lang.String> |
getPreReleaseIds() |
boolean |
hasBuildMetadataIdentifier(java.lang.String id) |
int |
hashCode() |
boolean |
hasPreReleaseIdentifier(java.lang.String id) |
static SemanticVersion |
is(java.lang.String specification)
Creates an instance of semantic version directly from the provided version string
|
boolean |
isPrerelease() |
boolean |
isPreRelease() |
boolean |
isSnapshot() |
boolean |
isStable() |
SemanticVersion |
stripAllIdentifiers() |
SemanticVersion |
stripBuildMetadataIdentifiers() |
SemanticVersion |
stripPreReleaseIdentifiers() |
java.lang.String |
toString() |
SemanticVersion |
withBuildMetadataIdentifier(java.lang.String id) |
SemanticVersion |
withBuildMetadataIdentifiers(java.lang.String... ids) |
SemanticVersion |
withOptionalBuildMetadataIdentifier(java.lang.String id) |
SemanticVersion |
withOptionalPreReleaseIdentifier(java.lang.String id) |
SemanticVersion |
withPreReleaseIdentifier(java.lang.String id) |
SemanticVersion |
withPreReleaseIdentifiers(java.lang.String... ids) |
public static final java.util.regex.Pattern PATTERN
public static final java.util.regex.Pattern IDENTIFIER_PATTERN
public static final java.lang.String SNAPSHOT_ID
public static SemanticVersion is(java.lang.String specification)
specification - text representation of semantic versionjava.lang.NullPointerException - when specification is nullorg.gradle.api.InvalidUserDataException - when specification does not conform to semantic version rulespublic static SemanticVersion from(java.io.InputStream stream)
The provided stream is expected to adhere to certain rules:
BufferedReader.readLine()# or //) are skipped.InvalidUserDataException exception.stream - source to be parsedjava.lang.NullPointerException - when stream is nullorg.gradle.api.InvalidUserDataException - when contents of stream does not adhere to the rules or when
the parsed version specification does not conform to semantic version rulespublic static SemanticVersion from(java.io.File versionHolder)
The provided stream is expected to adhere to rules defined in from(InputStream).
versionHolder - source to be parsedjava.lang.NullPointerException - when versionHolder is nullorg.gradle.api.InvalidUserDataException - when contents of versionHolder file does not adhere
to the rules or when the parsed version specification does not conform to semantic version rulespublic static SemanticVersion from(java.net.URL url)
The provided stream is expected to adhere to rules defined in from(InputStream).
url - source object to be parsedjava.lang.NullPointerException - when url is nullorg.gradle.api.InvalidUserDataException - when url is not accessible, contents of url
does not adhere to the rules or when the parsed version specification does not conform to
semantic version rulespublic static SemanticVersion from(java.lang.String source)
source argument as URL and uses it to parse a version string from its contents;
if the argument is not a valid URL, it is considered to be a local file path.source - text specification that must be either a valid URL or file pathjava.lang.NullPointerException - when source is nullorg.gradle.api.InvalidUserDataException - when source is not accessible, its contents
does not adhere to the rules or when the parsed version specification does not conform to
semantic version rules@Deprecated public int getMajor()
public int getMajorVersion()
@Deprecated public int getMinor()
public int getMinorVersion()
@Deprecated public int getPatch()
@Deprecated public int getPatchLevel()
public int getPatchRevision()
public boolean isPrerelease()
public boolean isPreRelease()
public boolean isStable()
public boolean isSnapshot()
public java.util.List<java.lang.String> getPreReleaseIdentifiers()
public boolean hasPreReleaseIdentifier(java.lang.String id)
public SemanticVersion withPreReleaseIdentifier(java.lang.String id)
public SemanticVersion withOptionalPreReleaseIdentifier(java.lang.String id)
public SemanticVersion withPreReleaseIdentifiers(java.lang.String... ids)
public SemanticVersion stripPreReleaseIdentifiers()
protected java.util.List<java.lang.String> getPreReleaseIds()
public java.util.List<java.lang.String> getBuildMetadataIdentifiers()
public boolean hasBuildMetadataIdentifier(java.lang.String id)
public SemanticVersion withBuildMetadataIdentifier(java.lang.String id)
public SemanticVersion withOptionalBuildMetadataIdentifier(java.lang.String id)
public SemanticVersion withBuildMetadataIdentifiers(java.lang.String... ids)
public SemanticVersion stripBuildMetadataIdentifiers()
protected java.util.List<java.lang.String> getBuildMetadataIds()
public SemanticVersion stripAllIdentifiers()
public boolean equals(java.lang.Object obj)
equals in class java.lang.Objectpublic int hashCode()
hashCode in class java.lang.Objectpublic int compareTo(SemanticVersion other)
compareTo in interface java.lang.Comparable<SemanticVersion>public java.lang.String toString()
toString in class java.lang.Object