如何通过SBT为Play应用设置自定义控制台横幅
可以修改Play Framework启动横幅,具体实现方法如下:
方法1:在build.sbt中直接配置
在项目根目录的build.sbt文件里添加以下代码,自定义你的横幅内容:
onLoadMessage := { val customBanner = """ [info] ************************* [info] * 我的自定义Play应用 * [info] ************************* [info] """ // 可选择拼接原有版本信息和提示,也可完全替换 customBanner + s"[info] Version ${play.core.PlayVersion.current} running Java ${System.getProperty("java.version")}" + "\n" + "[info] \n[info] Play is run entirely by the community. Please consider contributing and/or donating:" + "\n" + "[info] https://www.playframework.com/sponsors" }
方法2:创建自定义sbt插件(适合多项目复用)
- 在项目的
project目录下新建CustomBannerPlugin.scala文件,内容如下:
import sbt._ import Keys._ object CustomBannerPlugin extends AutoPlugin { override def trigger = allRequirements override def projectSettings = Seq( onLoadMessage := { val customBanner = """ [info] ┌─────────────────────────┐ [info] │ 自定义Play应用横幅 │ [info] └─────────────────────────┘ [info] """ customBanner + s"[info] Version ${play.core.PlayVersion.current} running Java ${System.getProperty("java.version")}" + "\n" + "[info] \n[info] Play is run entirely by the community. Please consider contributing and/or donating:" + "\n" + "[info] https://www.playframework.com/sponsors" } ) }
- 该插件为AutoPlugin类型,无需额外配置引用会自动生效。
关键说明
- 横幅内容里的
[info]前缀需保留,确保输出和sbt日志格式统一。 - 若不需要保留原有版本信息和捐赠提示,可直接在
onLoadMessage中编写完整自定义内容,无需拼接原有内容。
内容的提问来源于stack exchange,提问作者Anish B.
相关产品推荐
相关产品推荐

