public final class Version extends Object
Describe the version of a specific Java delivery (app or library).
The version contains three pieces of information:
${project.artifactId}${project.version}Application or library developer put a file named .version into a dir corresponding to app/lib’s package inside the resources dir. For example if a library package name is org.mrcool.swissknife, then put the .version file under
src/main/resources/org/mrcool/swissknife
The content of the .version file should be
# artifact is optional, if not provided the package name will be used
artifact=${project.artifactId}
# version is mandatory, if not provided then UNKNOWN version will be returned
version=${project.version}
# build is optional, if not provided then empty string will be used
build=${buildNumber} # optional
Note don’t forget to set resource filter in the library’s pom.xml file:
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
Note as a general rule, do NOT put the .version file for the first level package, e.g. org, com, net etc. Version tool will never read the .version file for a package name that does not contain a . inside
In order to obtain the version information about a library or app’s class, one can use the API provided by this Version class:
Version swissKnifeVersion = Version.of(org.mrcool.siwssknife.SwissKnife.class);
System.out.println(swissKnifeVersion.getArtifactId()); // print `swissknife`
System.out.println(swissKnifeVersion.getProjectVersion()); // print `1.0`
System.out.println(swissKnifeVersion.getBuildNumber()); // print `ebf1`
System.out.println(swissKnifeVersion.getVersion()); // print `r1.0-ebf1`
System.out.println(swissKnifeVersion); // print `swissknife-r1.0-ebf1`
| Modifier and Type | Field and Description |
|---|---|
static Version |
UNKNOWN |
static String |
UNKNOWN_STR |
| Constructor and Description |
|---|
Version(String packageName,
String artifactId,
String projectVersion,
String buildNumber)
Construct a
Version instance with packageName, artifactId, projectVersion and buildName. |
| Modifier and Type | Method and Description |
|---|---|
boolean |
equals(Object o) |
static Version |
get()
Returns
Version of the caller class. |
String |
getArtifactId()
Returns artifact id, i.e.
|
String |
getBuildNumber()
Returns the build number which is defined in
${buildNumber} maven environment variable when buildnumber-maven-plugin is provided |
String |
getPackageName()
Returns app/library package name.
|
String |
getProjectVersion()
Returns project version which is defined in
${project.version} maven environment variable |
String |
getVersion()
Returns version tag: a full version string.
|
int |
hashCode() |
boolean |
isUnknown()
Check if a
Version instance is UNKNOWN. |
static Version |
of(Class<?> clazz)
Returns a
Version of the library contains the class specified. |
static Version |
of(Package pkg)
Returns a
Version of the library contains the package specified. |
static Version |
ofPackage(String packageName)
Returns a
Version corresponding to the package name specified. |
String |
toString() |
public static final String UNKNOWN_STR
public static final Version UNKNOWN
public Version(String packageName, String artifactId, String projectVersion, String buildNumber)
Construct a Version instance with packageName, artifactId, projectVersion and buildName.
packageName - the package nameartifactId - the artifact idprojectVersion - the project versionbuildNumber - the build numberpublic String getArtifactId()
Returns artifact id, i.e. the name of the library or application
public String getPackageName()
Returns app/library package name.
package name is used to find the .version file when loading the version information
public String getProjectVersion()
Returns project version which is defined in ${project.version} maven environment variable
public String getVersion()
Returns version tag: a full version string.
When buildNumber exists the version tag is composed of projectVersion a buildNumber with the following pattern:
${patched-projectVersion}-${patched-buildNumber}
Where patched-projectVersion could be one of the following:
projectVersion ends with -SNAPSHOT, then "v" + projectVersion"R" + projectVersionpatched-buildNumber is "b" + buildNumber
If buildNumber is not defined then the version tag is the patched-projectVersion as described above
public String getBuildNumber()
Returns the build number which is defined in ${buildNumber} maven environment variable when buildnumber-maven-plugin is provided
public boolean isUnknown()
Check if a Version instance is UNKNOWN.
true if this version is unknown or false otherwisepublic static Version get()
Returns Version of the caller class.
public static Version ofPackage(String packageName)
Returns a Version corresponding to the package name specified.
This method will tried to check if local cache contains the package name first, and return the version instance mapped to the package name in local cache, or if not found in local cache try to load the .version file as a resource corresponding to the package name.
If the .version file not found, this method will try to get version of parent package until the package name is empty, in which case the UNKNOWN will be returned
packageName - the package nameVersion instance for that packagepublic static Version of(Class<?> clazz)
Returns a Version of the library contains the class specified.
clazz - the classVersion for that class if provided or UNKNOWN if not providedNullPointerException - if the class specified is nullofPackage(String)public static Version of(Package pkg)
Returns a Version of the library contains the package specified.
pkg - the packageVersion for the package if provided or UNKNOWN if not providedNullPointerException - if the class specified is nullofPackage(String)Copyright © 2017 OSGL (Open Source General Library). All rights reserved.