无法在build.sbt中引用对象,"错误:未找到:值挂钩"

时间:2016-10-22 19:41:15

标签: scala playframework sbt

我试图将gulp添加到我的Play应用程序中,我创建了一个PlayRunHook对象,应该允许我触发gulp命令,但是当我执行sbt run时我收到一条错误消息,说找不到该对象。这是我的钩子:

package hooks

object Gulp extends CommandHook {

  override def beforeStarted(): Unit = {
    exec("gulp")
  }
}

然后在build.sbt

lazy val root = (project in file(".")).enablePlugins(PlayScala)

scalaVersion := "2.11.7"

scalacOptions ++= Seq("-deprecation")

libraryDependencies ++= Seq(
  "junit" % "junit" % "4.10" % "test",
  "org.reactivemongo" %% "play2-reactivemongo" % "0.11.14",
  "com.typesafe.play" %% "play" % "2.5.0",
  "com.typesafe.play" %% "play-netty-server" % "2.5.0"
)

PlayKeys.playRunHooks += hooks.Gulp()

但我明白了:

build.sbt:18: error: not found: value hooks
PlayKeys.playRunHooks += hooks.Gulp()

2 个答案:

答案 0 :(得分:2)

您必须在 object 目录中创建Gulp project/ ,如下所示:

import play.sbt.PlayRunHook
import sbt._

object Gulp {
  def apply(base: File): PlayRunHook = {

    object GulpProcess extends PlayRunHook {

      override def beforeStarted(): Unit = {
        Process("gulp", base).run
      }
    }

    GulpProcess
  }
}

然后在build.sbt

PlayKeys.playRunHooks += Gulp(baseDirectory.value)

有关详细信息,请查看此guide

答案 1 :(得分:0)

编辑:我实际上最终接受了接受的答案,这个替代答案会在每次向您的应用发出请求时触发gulp bundle命令。通过使用PlayRunHook方法,您可以获得不同的生命周期钩子,最后我会挂钩beforeStarted(如在接受的答案中所做的那样)以在启动应用时触发gulp watch

我可能会改变这一点,以便在开发时gulp watchgulp build进行部署,但我尚未处于该阶段。

接受的答案解释了为什么build.sbt无法找到值hooks(代码需要位于目录project/中),但要解决我想要运行{{1}的问题在编译期间,我选择了这个解决方案:

gulp bundle