1
1
/*
2
- * Copyright 2012-2018 the original author or authors.
2
+ * Copyright 2012-2019 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
16
16
17
17
package org .springframework .boot ;
18
18
19
+ import java .io .File ;
20
+ import java .io .IOException ;
21
+ import java .net .JarURLConnection ;
22
+ import java .net .URL ;
23
+ import java .net .URLConnection ;
24
+ import java .util .jar .Attributes ;
25
+ import java .util .jar .Attributes .Name ;
26
+ import java .util .jar .JarFile ;
27
+
19
28
/**
20
- * Class that exposes the Spring Boot version. Fetches the "Implementation-Version"
21
- * manifest attribute from the jar file.
29
+ * Class that exposes the Spring Boot version. Fetches the
30
+ * {@link Name#IMPLEMENTATION_VERSION Implementation-Version} manifest attribute from the
31
+ * jar file via {@link Package#getImplementationVersion()}, falling back to locating the
32
+ * jar file that contains this class and reading the {@code Implementation-Version}
33
+ * attribute from its manifest.
22
34
* <p>
23
- * Note that some ClassLoaders do not expose the package metadata, hence this class might
24
- * not be able to determine the Spring Boot version in all environments. Consider using a
25
- * reflection-based check instead: For example, checking for the presence of a specific
26
- * Spring Boot method that you intend to call.
35
+ * This class might not be able to determine the Spring Boot version in all environments.
36
+ * Consider using a reflection-based check instead: For example, checking for the presence
37
+ * of a specific Spring Boot method that you intend to call.
27
38
*
28
39
* @author Drummond Dawson
40
+ * @author Hendrig Sellik
41
+ * @author Andy Wilkinson
29
42
* @since 1.3.0
30
43
*/
31
44
public final class SpringBootVersion {
@@ -40,8 +53,35 @@ private SpringBootVersion() {
40
53
* @see Package#getImplementationVersion()
41
54
*/
42
55
public static String getVersion () {
43
- Package pkg = SpringBootVersion .class .getPackage ();
44
- return (pkg != null ) ? pkg .getImplementationVersion () : null ;
56
+ return determineSpringBootVersion ();
57
+ }
58
+
59
+ private static String determineSpringBootVersion () {
60
+ String implementationVersion = SpringBootVersion .class .getPackage ()
61
+ .getImplementationVersion ();
62
+ if (implementationVersion != null ) {
63
+ return implementationVersion ;
64
+ }
65
+ URL codeSourceLocation = SpringBootVersion .class .getProtectionDomain ()
66
+ .getCodeSource ().getLocation ();
67
+ try {
68
+ URLConnection connection = codeSourceLocation .openConnection ();
69
+ if (connection instanceof JarURLConnection ) {
70
+ return getImplementationVersion (
71
+ ((JarURLConnection ) connection ).getJarFile ());
72
+ }
73
+ try (JarFile jarFile = new JarFile (new File (codeSourceLocation .toURI ()))) {
74
+ return getImplementationVersion (jarFile );
75
+ }
76
+ }
77
+ catch (Exception ex ) {
78
+ return null ;
79
+ }
80
+ }
81
+
82
+ private static String getImplementationVersion (JarFile jarFile ) throws IOException {
83
+ return jarFile .getManifest ().getMainAttributes ()
84
+ .getValue (Attributes .Name .IMPLEMENTATION_VERSION );
45
85
}
46
86
47
87
}
0 commit comments