26
26
import org .apache .logging .log4j .Logger ;
27
27
28
28
import javax .annotation .Nullable ;
29
- import java .io .FileReader ;
30
29
import java .net .JarURLConnection ;
31
30
import java .net .URL ;
32
- import java .nio .file .Files ;
33
- import java .nio .file .Path ;
34
31
import java .util .Properties ;
35
- import java .util .jar .Manifest ;
32
+ import java .util .jar .JarFile ;
36
33
37
34
/**
38
35
* A utility class for logging runtime information about the environment and the Tai-e application.
@@ -53,11 +50,12 @@ public class RuntimeInfoLogger {
53
50
"os.arch" ,
54
51
};
55
52
56
- private static final String VERSION_MANIFEST_KEY = "Tai-e-Version" ;
53
+ private static final String BUILD_PROPERTY_FILE_PATH =
54
+ "META-INF/tai-e-build.properties" ;
57
55
58
- private static final String COMMIT_MANIFEST_KEY = "Tai-e-Commit " ;
56
+ private static final String VERSION_KEY = "version " ;
59
57
60
- private static final String VERSION_PROPERTY_KEY = "projectVersion " ;
58
+ private static final String COMMIT_KEY = "commit " ;
61
59
62
60
private static final String UNKNOWN = "Unknown" ;
63
61
@@ -80,112 +78,54 @@ private static void logEnvInfo() {
80
78
}
81
79
82
80
/**
83
- * Logs Tai-e version and commit information by attempting to read from the manifest file
84
- * or fallback methods if the manifest is not available.
81
+ * Logs Tai-e version and commit information by attempting to read from the build properties file
82
+ * or fallback methods if the build properties are not available.
85
83
*/
86
84
private static void logTaieInfo () {
87
- Manifest manifest = getManifest ();
88
- String version = manifest != null ? readVersionFromManifest (manifest )
89
- : readVersionFromGradleProperties ();
85
+ Properties properties = getBuildProperties ();
86
+ String version = properties != null
87
+ ? properties .getProperty (VERSION_KEY )
88
+ : UNKNOWN ;
90
89
logger .info ("Tai-e Version: {}" , version );
91
- String commit = manifest != null ? readCommitFromManifest (manifest )
92
- : readCommitFromDotGit ();
90
+ String commit = properties != null
91
+ ? properties .getProperty (COMMIT_KEY )
92
+ : UNKNOWN ;
93
93
logger .info ("Tai-e Commit: {}" , commit );
94
94
}
95
95
96
96
/**
97
- * Reads the Tai-e version from the provided manifest .
97
+ * Retrieves the build properties of the current JAR file, if available .
98
98
*
99
- * @param manifest the manifest to read from
100
- * @return the Tai-e version, or {@code "Unknown"} if not found
101
- */
102
- private static String readVersionFromManifest (Manifest manifest ) {
103
- String version = manifest .getMainAttributes ().getValue (VERSION_MANIFEST_KEY );
104
- if (version == null ) {
105
- logger .warn ("Manifest does not contain Tai-e version information" );
106
- return UNKNOWN ;
107
- }
108
- return version ;
109
- }
110
-
111
- /**
112
- * Reads the Tai-e version from the gradle.properties file.
113
- *
114
- * @return the Tai-e version, or {@code "Unknown"} if an error occurs
115
- */
116
- private static String readVersionFromGradleProperties () {
117
- try {
118
- Properties properties = new Properties ();
119
- properties .load (new FileReader (Path .of ("gradle.properties" ).toFile ()));
120
- return properties .getProperty (VERSION_PROPERTY_KEY );
121
- } catch (Exception e ) {
122
- logger .warn ("Failed to read version from 'gradle.properties': {}" , e .toString ());
123
- }
124
- return UNKNOWN ;
125
- }
126
-
127
- /**
128
- * Reads the Tai-e commit hash from the provided manifest.
129
- *
130
- * @param manifest the manifest to read from
131
- * @return the Tai-e commit hash, or {@code "Unknown"} if not found
132
- */
133
- private static String readCommitFromManifest (Manifest manifest ) {
134
- String commit = manifest .getMainAttributes ().getValue (COMMIT_MANIFEST_KEY );
135
- if (commit == null ) {
136
- logger .warn ("Manifest does not contain Tai-e commit information" );
137
- return UNKNOWN ;
138
- }
139
- return commit ;
140
- }
141
-
142
- /**
143
- * Reads the current git commit hash from the .git directory.
144
- *
145
- * @return the current git commit hash, or {@code "Unknown"} if an error occurs
146
- */
147
- private static String readCommitFromDotGit () {
148
- try {
149
- String gitHead = Files .readString (Path .of (".git" , "HEAD" ));
150
- if (gitHead .startsWith ("ref: " )) {
151
- String ref = gitHead .substring (5 ).trim ();
152
- // path '.git/refs/heads/branchName'
153
- Path p = Path .of (".git" , ref );
154
- if (p .toFile ().exists ()) {
155
- return Files .readString (p ).trim ();
156
- } else {
157
- // read from '.git/info/refs' line by line
158
- return Files .lines (Path .of (".git" , "info" , "refs" ))
159
- .filter (line -> line .endsWith (ref ))
160
- .map (line -> line .split ("\t " )[0 ])
161
- .findFirst ()
162
- .orElse (UNKNOWN );
163
- }
164
- } else {
165
- return gitHead .trim ();
166
- }
167
- } catch (Exception e ) {
168
- logger .warn ("Failed to read Git commit hash: {}" , e .toString ());
169
- }
170
- return UNKNOWN ;
171
- }
172
-
173
- /**
174
- * Retrieves the manifest of the current JAR file, if available.
175
- *
176
- * @return the manifest, or {@code null} if an error occurs or the manifest is not found
99
+ * @return the build properties, or {@code null} if an error occurs or the build properties is not found
177
100
*/
178
101
@ Nullable
179
- private static Manifest getManifest () {
102
+ private static Properties getBuildProperties () {
180
103
try {
181
104
URL url = RuntimeInfoLogger .class .getProtectionDomain ().getCodeSource ().getLocation ();
182
105
if (url .getPath ().endsWith (".jar" )) {
183
106
var jarConnection = (JarURLConnection ) new URL ("jar:" + url + "!/" )
184
107
.openConnection ();
185
- return jarConnection .getManifest ();
108
+ JarFile jarFile = jarConnection .getJarFile ();
109
+ var buildPropsEntry = jarFile .getJarEntry (BUILD_PROPERTY_FILE_PATH );
110
+ if (buildPropsEntry != null ) {
111
+ try (var inputStream = jarFile .getInputStream (buildPropsEntry )) {
112
+ Properties properties = new Properties ();
113
+ properties .load (inputStream );
114
+ return properties ;
115
+ }
116
+ }
117
+ } else {
118
+ try (var inputStream = RuntimeInfoLogger .class
119
+ .getClassLoader ().getResourceAsStream (BUILD_PROPERTY_FILE_PATH )) {
120
+ if (inputStream != null ) {
121
+ Properties properties = new Properties ();
122
+ properties .load (inputStream );
123
+ return properties ;
124
+ }
125
+ }
186
126
}
187
127
} catch (Exception e ) {
188
- logger .warn ("Failed to read manifest : {}" , e .toString ());
128
+ logger .warn ("Failed to read build properties : {}" , e .toString ());
189
129
}
190
130
return null ;
191
131
}
0 commit comments