Spring Boot - Changing the default banner example

Posted on November 26, 2017


Technologies used: Spring Boot 1.5.8.RELEASE | Java SE 1.8 | Maven 3.5.2 | Eclipse Neon.3 (4.6.3)

This post shows you how to change the Spring Boot’s default banner by adding a text or image file on the classpath.

The default banner of a Spring boot application look like as follows.

spring-boot-banner_02.png

To change the default banner of your Spring boot application, you can create either a banner.txt or banner image (PNG/GIF/JPG) file as shown in the following project structure.

Project Structure

Final project structure of our application will look like as follows.

spring-boot-banner.png

Related - How to create a maven project in eclipse IDE.

Jar dependencies

Edit pom.xml file of your maven project, add the dependencies below.

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.boraji.tutorial.springboot</groupId>
  <artifactId>spring-boot-banner-example</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>My Spring Boot Application</name>
  <properties>
    <java.version>1.8</java.version>
  </properties>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.8.RELEASE</version>
  </parent>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>

Customizing the Banner using text file

To replace the existing/default banner of Spring boot application, create a text file under src/main/resources folder as follows.

banner.txt

--------------------------------------------------------------------------
Application Name : ${application.title}
Application Version : ${application.version}
Application Formatted Version : ${application.formatted-version}
Spring Boot Version : ${spring-boot-version}
Spring Boot Formatted Version : ${spring-boot.formatted-version}
--------------------------------------------------------------------------
${AnsiColor.BRIGHT_RED}	 Spring boot banner customization example
--------------------------------------------------------------------------

The expression ${…} is known as banner variables.

${application.title} is used to print the title of your application, which is taken from the jar manifest (i.e. MANIFEST.MF).

${application.version} is used to print the version number, which is taken from the jar manifest.

${application.formatted-version} is used to print the version number, surrounded with brackets and prefixed with v. For example (v1.2).

${spring-boot-version} is used to print the Spring Boot version number.

${spring-boot.formatted-version} is used to print the Spring Boot version number, surrounded with brackets and prefixed with v.

${AnsiColor.NAME} and ${AnsiBackground.NAME} are used to change the console foreground and background color. Where NAME is the name of an ANSI escape code.

 

Customizing the Banner using Image file

In addition to banner.txt file, you can use the image file to customize the banner of Spring Boot application. You need to add the banner.png or banner.jpg or banner.gif file to your classpath, Spring Boot converts the image file to ASCII art representation automatically and print it above the text banner.

In this example, I am using the following image file. You can create your own image(png/jpg/gif) file for banner.

Add the following image file in src/main/resources folder.

banner.png

spring-boot-banner_03.png

Creating main class

Create a main class, annotated with @SpringBootApplication annotation, to test your custom banner as follows.

MainApp.java

package com.boraji.tutorial.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MainApp {
  public static void main(String[] args) {
    SpringApplication app = new SpringApplication(MainApp.class);
    app.run(args);
  }
}

Packaging Spring Boot application

You can use the following maven command to package your Spring Boot application as jar.

mvn clean package

Running Spring Boot application

Use the following the following command to run the jar from the CMD.

java -jar spring-boot-banner-example-0.0.1-SNAPSHOT.jar

After executing the above command in CMD, your custom banner will look like as follows.

spring-boot-banner_01.png

Disabling banner

You can use the SpringApplication.setBannerMode(Mode.OFF) method to disable the default banner or custom banner of Spring Boot application.

The follow example shows how to disable the  banner.
 

@SpringBootApplication
public class MainApp {
  public static void main(String[] args) {
    SpringApplication app = new SpringApplication(MainApp.class);
    app.setBannerMode(Mode.OFF); // Disable banner
    app.run(args);
  }
}

 

Download Sources