1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package com.irurueta.numerical;
17
18 import java.lang.ref.SoftReference;
19 import java.text.SimpleDateFormat;
20 import java.util.Date;
21 import java.util.Locale;
22 import java.util.Properties;
23 import java.util.logging.Level;
24 import java.util.logging.Logger;
25
26
27
28
29 public class BuildInfo {
30
31
32
33
34 private static final Logger LOGGER = Logger.getLogger(BuildInfo.class.getName());
35
36
37
38
39
40
41 private static final String BUILD_INFO_PROPERTIES = "build-info.properties";
42
43
44
45
46 private static final String BUILD_TIMESTAMP_KEY = "BUILD_TIMESTAMP";
47
48
49
50
51 private static final String GROUP_ID_KEY = "GROUP_ID";
52
53
54
55
56 private static final String ARTIFACT_ID_KEY = "ARTIFACT_ID";
57
58
59
60
61 private static final String VERSION_KEY = "VERSION";
62
63
64
65
66 private static final String BUILD_NUMBER_KEY = "BUILD_NUMBER";
67
68
69
70
71 private static final String COMMIT_KEY = "COMMIT";
72
73
74
75
76 private static final String BRANCH_KEY = "BRANCH";
77
78
79
80
81 private static final String TIMESTAMP_FORMAT = "yy-MM-dd HH:mm:ss";
82
83
84
85
86
87 private static SoftReference<BuildInfo> reference;
88
89
90
91
92 private Date buildTimestamp;
93
94
95
96
97 private String groupId;
98
99
100
101
102 private String artifactId;
103
104
105
106
107 private String version;
108
109
110
111
112 private String buildNumber;
113
114
115
116
117 private String commit;
118
119
120
121
122 private String branch;
123
124
125
126
127 private BuildInfo() {
128
129 try (final var stream = BuildInfo.class.getResourceAsStream(BUILD_INFO_PROPERTIES)) {
130 final var props = new Properties();
131 props.load(stream);
132
133 final var buildTimestampString = props.getProperty(BUILD_TIMESTAMP_KEY);
134 final var format = new SimpleDateFormat(TIMESTAMP_FORMAT, Locale.ENGLISH);
135 buildTimestamp = format.parse(buildTimestampString);
136
137 groupId = props.getProperty(GROUP_ID_KEY);
138 artifactId = props.getProperty(ARTIFACT_ID_KEY);
139 version = props.getProperty(VERSION_KEY);
140 buildNumber = props.getProperty(BUILD_NUMBER_KEY);
141 commit = props.getProperty(COMMIT_KEY);
142 branch = props.getProperty(BRANCH_KEY);
143 } catch (final Exception e) {
144 LOGGER.log(Level.WARNING, "Failed to load build info", e);
145 }
146 }
147
148
149
150
151
152
153 public static synchronized BuildInfo getInstance() {
154 BuildInfo info;
155 if (reference == null || (info = reference.get()) == null) {
156 info = new BuildInfo();
157 reference = new SoftReference<>(info);
158 }
159
160 return info;
161 }
162
163
164
165
166
167
168 public Date getBuildTimestamp() {
169 return (Date) buildTimestamp.clone();
170 }
171
172
173
174
175
176
177 public String getGroupId() {
178 return groupId;
179 }
180
181
182
183
184
185
186 public String getArtifactId() {
187 return artifactId;
188 }
189
190
191
192
193
194
195 public String getVersion() {
196 return version;
197 }
198
199
200
201
202
203
204 public String getBuildNumber() {
205 return buildNumber;
206 }
207
208
209
210
211
212
213 public String getCommit() {
214 return commit;
215 }
216
217
218
219
220
221
222 public String getBranch() {
223 return branch;
224 }
225 }