Skip to content

Commit 8b04973

Browse files
drumoniiphilwebb
authored andcommitted
Add a SpringBootVersion class like SpringVersion
Provide a static utility method to get the Spring Boot version based on the implementation version of SpringApplication's package. Similar to Spring Frameworks' SpringVersion class. Fixes gh-2300
1 parent 69c44fa commit 8b04973

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

spring-boot/src/main/java/org/springframework/boot/ResourceBanner.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2014 the original author or authors.
2+
* Copyright 2012-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -106,7 +106,7 @@ protected String getApplicationVersion(Class<?> sourceClass) {
106106
}
107107

108108
protected String getBootVersion() {
109-
return Banner.class.getPackage().getImplementationVersion();
109+
return SpringBootVersion.getVersion();
110110
}
111111

112112
private String getVersionString(String version, boolean format) {

spring-boot/src/main/java/org/springframework/boot/SpringBootBanner.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2013 the original author or authors.
2+
* Copyright 2012-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -50,7 +50,7 @@ public void printBanner(Environment environment, Class<?> sourceClass,
5050
for (String line : BANNER) {
5151
printStream.println(line);
5252
}
53-
String version = Banner.class.getPackage().getImplementationVersion();
53+
String version = SpringBootVersion.getVersion();
5454
version = (version == null ? "" : " (v" + version + ")");
5555
String padding = "";
5656
while (padding.length() < STRAP_LINE_SIZE
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2012-2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot;
18+
19+
/**
20+
* Class that exposes the Spring Boot version. Fetches the "Implementation-Version"
21+
* manifest attribute from the jar file.
22+
* <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.
27+
*
28+
* @author Drummond Dawson
29+
* @since 1.3.0
30+
*/
31+
public class SpringBootVersion {
32+
33+
/**
34+
* Return the full version string of the present Spring Boot codebase, or {@code null}
35+
* if it cannot be determined.
36+
* @return the version of Spring Boot or {@code null}
37+
* @see Package#getImplementationVersion()
38+
*/
39+
public static String getVersion() {
40+
Package pkg = SpringApplication.class.getPackage();
41+
return (pkg != null ? pkg.getImplementationVersion() : null);
42+
}
43+
44+
}

0 commit comments

Comments
 (0)