19
19
20
20
package co .elastic .clients .transport ;
21
21
22
+ import co .elastic .clients .ApiClient ;
23
+
22
24
import javax .annotation .Nullable ;
25
+ import java .io .InputStream ;
23
26
import java .util .Objects ;
27
+ import java .util .Properties ;
24
28
25
29
/**
26
30
* This class represents a SemVer version, with an optional patch revision.
@@ -45,20 +49,20 @@ public static Version parse(String version) {
45
49
int hyphen = version .indexOf ('-' );
46
50
if (hyphen >= 0 ) {
47
51
// Has prerelease. May be followed buy build information
48
- prerelease = version .substring (hyphen + 1 );
52
+ prerelease = version .substring (hyphen + 1 );
49
53
version = version .substring (0 , hyphen );
50
54
51
55
int plus = prerelease .indexOf ('+' );
52
56
if (plus >= 0 ) {
53
- build = prerelease .substring (0 , plus + 1 );
57
+ build = prerelease .substring (0 , plus + 1 );
54
58
prerelease = prerelease .substring (0 , plus );
55
59
}
56
60
}
57
61
58
62
int plus = version .indexOf ('+' );
59
63
if (plus >= 0 ) {
60
64
// Has build information
61
- build = version .substring (0 , plus + 1 );
65
+ build = version .substring (0 , plus + 1 );
62
66
version = version .substring (0 , plus );
63
67
}
64
68
@@ -68,8 +72,7 @@ public static Version parse(String version) {
68
72
int minor = (bits .length >= 2 ) ? Integer .parseInt (bits [1 ]) : 0 ;
69
73
int maintenance = (bits .length >= 3 ) ? Integer .parseInt (bits [2 ]) : -1 ;
70
74
return new Version (major , minor , maintenance , prerelease , build );
71
- }
72
- catch (NumberFormatException ex ) {
75
+ } catch (NumberFormatException ex ) {
73
76
return null ;
74
77
}
75
78
}
@@ -78,7 +81,8 @@ public Version(int major, int minor, int maintenance, boolean isPreRelease) {
78
81
this (major , minor , maintenance , isPreRelease ? "p" : null , null );
79
82
}
80
83
81
- public Version (int major , int minor , int maintenance , @ Nullable String prerelease , @ Nullable String build ) {
84
+ public Version (int major , int minor , int maintenance , @ Nullable String prerelease ,
85
+ @ Nullable String build ) {
82
86
this .major = major ;
83
87
this .minor = minor ;
84
88
this .maintenance = maintenance ;
@@ -108,10 +112,10 @@ public boolean equals(Object other) {
108
112
if (!(other instanceof Version )) return false ;
109
113
Version that = (Version ) other ;
110
114
return (major == that .major &&
111
- minor == that .minor &&
112
- maintenance == that .maintenance &&
113
- Objects .equals (prerelease , that .prerelease ) &&
114
- Objects .equals (build , that .build ));
115
+ minor == that .minor &&
116
+ maintenance == that .maintenance &&
117
+ Objects .equals (prerelease , that .prerelease ) &&
118
+ Objects .equals (build , that .build ));
115
119
}
116
120
117
121
@ Override
@@ -146,10 +150,19 @@ public String toString() {
146
150
147
151
static {
148
152
Version version = null ;
149
- try {
150
- version = Version .parse (VersionInfo .VERSION );
151
- } catch (Exception e ) {
152
- // Failed to parse version
153
+ InputStream in = ApiClient .class .getResourceAsStream ("version.properties" );
154
+ if (in != null ) {
155
+ Properties properties = new Properties ();
156
+ try {
157
+ properties .load (in );
158
+ String versionStr = properties .getProperty ("version" );
159
+ if (versionStr != null ) {
160
+ version = Version .parse (versionStr );
161
+ }
162
+ } catch (Exception e ) {
163
+ // Failed to parse version from file, trying from VersionInfo
164
+ version = Version .parse (VersionInfo .VERSION );
165
+ }
153
166
}
154
167
VERSION = version ;
155
168
}
0 commit comments