自定义SpringBoot Banner

自定义SpringBoot Banner

该篇文章主要结束在springboot工程中如何自定义banner并同步maven pom中的版本号到banner中,启动服务时同步显示jar包版本。

1、创建banner.txt

创建banner.txt文件并存放到springboot工程中的resources目录下

2、使用生成ASCII

该网站可以在线生成字符

https://www.asciiart.eu/text-to-ascii-art

3、将banner.txt复制倒springboot工程中

${AnsiColor.BLUE}

 _   _      _ _        __        __         _     _ _ 
| | | | ___| | | ___   \ \      / /__  _ __| | __| | |
| |_| |/ _ \ | |/ _ \   \ \ /\ / / _ \| '__| |/ _` | |
|  _  |  __/ | | (_) |   \ V  V / (_) | |  | | (_| |_|
|_| |_|\___|_|_|\___/     \_/\_/ \___/|_|  |_|\__,_(_)

${AnsiColor.BLUE}
Application Version: ${application.version}
Spring Boot Version: ${spring-boot.version}

由于我们添加了Application Version所以需要将pom中添加以下配置,将项目的project.version(在发布版本时更新project.version将会同步更新banner中的版本) 映射到MANIFEST.MF文件中的Implementation-Version配置下(该方式只能使用Spring Boot Launchers 时才会生效)。通过idea run 看不到版本号,其实也无伤大雅,在banner中输出版本号也是为了在生产环境中查看当前启动的包具体是何版本。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifestEntries>
                <Implementation-Version>${project.version}</Implementation-Version>
            </manifestEntries>
        </archive>
        <classesDirectory/>
        <outputDirectory/>
        <testClassesDirectory/>
    </configuration>
</plugin>

4、启动服务

启动服务后即可查看到使用我们自定义的banner已经将springboot默认的banner替换掉了。

QQ20250113-164327.png

5、maven聚合项目中使用

由于当下多数采用微服务架构,并使用maven聚合多模块来构建项目,所以如果在maven聚合项目中使用时需要创建一个公共模块,将banner文件放在该模块的resources目录下,在其他模块的web工程中依赖该模块即可。

LICENSED UNDER CC BY-NC-SA 4.0
Comment