scala 如何通过SBT为Play应用程序设置自定义控制台横幅

lfapxunr  于 8个月前  发布在  Scala
关注(0)|答案(2)|浏览(97)

当我从终端运行命令时:sbt run
它在控制台上的日志如下所示:

[info] welcome to sbt 1.3.13 (Azul Systems, Inc. Java 1.8.0_362)
[info] loading global plugins from /.sbt/1.0/plugins
[info] loading settings for project from plugins.sbt ...
[info] loading project definition from /demo/project
[info] loading settings for project root from build.sbt ...
[info]   __              __
[info]   \ \     ____   / /____ _ __  __
[info]    \ \   / __ \ / // __ `// / / /
[info]    / /  / /_/ // // /_/ // /_/ /
[info]   /_/  / .___//_/ \__,_/ \__, /
[info]       /_/               /____/
[info] 
[info] Version 2.8.18 running Java 1.8.0_362
[info] 
[info] Play is run entirely by the community. Please consider contributing and/or donating:
[info] https://www.playframework.com/sponsors

现在,我想修改下面的默认横幅自定义横幅.

[info]   __              __
[info]   \ \     ____   / /____ _ __  __
[info]    \ \   / __ \ / // __ `// / / /
[info]    / /  / /_/ // // /_/ // /_/ /
[info]   /_/  / .___//_/ \__,_/ \__, /
[info]       /_/               /____/
[info]

还能修改吗?如果是,请说明如何做到这一点。

gpnt7bae

gpnt7bae1#

在您的项目的build.sbt中插入onLoadMessage键:

import play.sbt.Colors

onLoadMessage := {
 """|  __              __
    |  \ \     ____   / /____ _ __  __
    |   \ \   / __ \ / // __ `// / / /
    |   / /  / /_/ // // /_/ // /_/ /
    |  /_/  / .___//_/ \__,_/ \__, /
    |      /_/               /____/
    |""".stripMargin.linesIterator.map(Colors.green(_)).mkString("\n")
}

如果您需要自定义横幅,例如就我个人而言,我更喜欢使用sbt-welcome来为用户提供一些不错的欢迎信息、有用的任务和如何开始使用代码的提示。它将使用某种结构化格式设置onLoadMessage

c2e8gylq

c2e8gylq2#

这条消息来自sbt的播放插件。您可以在PlayFramework repo中找到该消息

// Settings for a Play service (not a web project)
  lazy val serviceSettings = Seq[Setting[_]](
    onLoadMessage := {
      val javaVersion = sys.props("java.specification.version")
      """|  __              __
         |  \ \     ____   / /____ _ __  __
         |   \ \   / __ \ / // __ `// / / /
         |   / /  / /_/ // // /_/ // /_/ /
         |  /_/  / .___//_/ \__,_/ \__, /
         |      /_/               /____/
         |""".stripMargin.linesIterator.map(Colors.green(_)).mkString("\n") +
        s"""|
            |
            |Version ${play.core.PlayVersion.current} running Java ${System.getProperty("java.version")}
            |
            |${Colors.bold(
             "Play is run entirely by the community. Please consider contributing and/or donating:"
           )}
            |https://www.playframework.com/sponsors
            |
            |""".stripMargin +
        (if (javaVersion == "17")
           s"""Running Play on Java ${sys.props("java.specification.version")} is experimental. Tweaks are necessary:
              |https://github.com/playframework/playframework/releases/2.8.15
              |
              |""".stripMargin
         else if (javaVersion != "1.8" && javaVersion != "11")
           s"""!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
              |  Java version is ${sys
               .props("java.specification.version")}. Play supports only 8, 11 and, experimentally, 17.
              |!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
              |
              |""".stripMargin
         else "")
    }

如果您查找PlaySettings.serviceSettings,您将能够找到在Play的AutoPlugin类的定义中使用的属性

object PlayService extends AutoPlugin {
  override def requires = JavaServerAppPackaging
  val autoImport        = PlayImport

  override def globalSettings  = PlaySettings.serviceGlobalSettings
  override def projectSettings = PlaySettings.serviceSettings
}

然后您可以发现必须在build.sbt文件的项目定义中启用的PlayPlugin扩展了PlayService

object PlayScala extends AutoPlugin {
  override def requires        = PlayWeb
  override def projectSettings = PlaySettings.defaultScalaSettings
}

你有不同的方法。

  • 覆盖onLoadMessage的值
import play.sbt.Colors

lazy val root = (project in file("."))
  .settings(
    name := "play-with-dino-logo",
    onLoadMessage := {
      """
        |                       __
        |                      / _)
        |             _.----._/ /
        |            /         /
        |         __/ (  | (  |
        |        /__.-'|_|--|_|
                      """
        .stripMargin
        .linesIterator
        .map(Colors.blue(_))
        .mkString("\n")
    }
  ).enablePlugins(PlayScala)

一旦启动Play应用程序,您将看到新消息

  • 另一种方法是创建自己的sbt plugin,扩展播放插件并覆盖onLoadMessage。如果您有多个项目,如果您希望所有项目在每次启动时都具有相同的消息,则可以在一个位置更改消息。

相关问题