In this article, we’ll explore how to create a custom banner in Spring Boot. Additionally, we’ll learn how to incorporate information from build tools, like Maven, in the banner.
What Is a Banner in Spring Boot?
When we run a Spring Boot application, it prints a default banner in the log. Moreover, along with a beautiful Spring Boot logo, it also displays the Spring Boot’s version.
The banner looks like the following:
Why Should We Customize the Banner?
We usually don’t use a custom banner in our Spring Boot applications. However, sometimes, just after we deploy some changes, we may not immediately find out whether the latest application changes are deployed just by seeing the application logs.
Therefore, we can use a custom banner to add more information to the logs along with the banner. Moreover, if we can add a few details from Maven, like the project version, we can quickly verify whether we have deployed the correct version of the application.
Using a Custom Banner
It’s easy! To have a custom banner, we can simply add a banner.txt
file to the classpath. Alternatively, we can set the spring.banner.location
property to the file path if it is at some other location. Moreover, we can also use an image as a banner. In this article, we’ll use a text file for banner.
To start with, let’s first create a simple Spring Boot application. Here, we can select Spring Web as a dependency and build-tool as Maven. Now, let’s create a custom banner.txt file and place it under src/main/resources
folder. The banner, I have used, has the following content:
. _ __ __ _ ========================================= /\\ |_ __ __ _| __ _ __ _ _ __ \\\\ ( ( ) | | | '_ '_ | | | )) \\\\ \\/ | | | | | | | | | |--|| //// ' | |ECHNICAL |_| |_| |_|USINGS AND |_|__))EYOND //// =========|_|===============================================
Next, let’s start the application. We can see that Spring Boot is now showing our custom banner in the startup log.
Adding Custom Variables in the Banner
As we can see, this banner has no details, unlike the default Spring Boot banner which displays the Spring Boot version.
So, let’s make the banner a little informative by adding more information to it. Notably, Spring Boot allows the use of placeholders in the banner. You can find examples of the placeholder variables here.
In this example, we are interested in adding some variables from our build file i.e. Maven’s pom.xml
. Therefore, let’s add a few custom properties to the pom.xml
for recording the release information.
<properties> <java.version>22</java.version> <release.id>REL30062024</release.id> <release.date>June 30, 2024</release.date> </properties>
Next, we want to add the release.id
and the release.date
variables to our banner. Furthermore, let’s also add the project version to the banner, so we know the version when the server starts up. Also, let’s add the Spring Boot version to our custom banner.
Notably, Spring Boot allows us to use the properties from the application.properties
file in the banner. So, to use the custom Maven properties in the banner, we’ll first add these properties to the application.properties
file.
application.version=@project.version@ release.id=@release.id@ release.date=@release.date@
Note, the special syntax, @variable@
, we’ve used to add the properties from Maven to the application.properties
.
Finally, let’s enrich our banner with these variables by adding the following to the banner.txt
.
spring-boot.version: ${spring-boot.version} application.version: ${application.version} release.id : ${release.id} release.date : ${release.date}
Let’s now run the Spring Boot application to see the final version of the custom banner in action.
Conclusion
This article shows how to customize the Spring Boot’s default banner to add more information to the logs. Furthermore, we’ve also seen a way to add information from Maven’s pom.xml
to the banner.
You can read more technical articles like this here.
Short and sweet article