2

我有一个包含两个模块的多模块 Play Framework 项目,common这是一个包含可重用代码和资产的标准项目,app也是我要运行的 play 项目。使用 Sbt-Web 插件,我可以创建和修改每次重新加载应用程序时common/src/main/public复制到文件夹中的资产。app/target/web/web-modules/main/webjars/lib/common我的问题是在我的 Play 视图中引用这些资产。我的routes文件包含以下行

GET  /assets/*file    controllers.Assets.at(path="/public", file)
GET  /webjars/*file   controllers.WebJarAssets.at(file)

我正在尝试通过例如访问我的index.scala.html文件中的这些导出资产app

<link rel="stylesheet" media="screen" href="@routes.WebJarAssets.at(WebJarAssets.locate("common/css/directives.css"))">

该文件确实存在app/target/web/web-modules/main/webjars/lib/common/css/directives.cssWebJarAssts.locate返回

{"status":{"errors":[{"message":"common/css/directives.css could not be found. Make sure you've added the corresponding WebJar and please check for typos."}]}}

我已经尝试指定更多和更少的路径,并通过 将其作为标准资产进行访问Assets.at,但没有运气。有谁知道如何正确引用导出的资产?

build.sbt我文件中的相关行是

val playVersion = play.core.PlayVersion.current

lazy val common = project.enablePlugins(SbtWeb, SbtTwirl).settings(
    libraryDependencies ++= Seq(
        "com.typesafe.play" %% "play" % playVersion % "provided",
    )
)   

lazy val app = project.enablePlugins(PlayJava).settings(
    libraryDependencies ++= Seq(
        "org.webjars" %% "webjars-play" % "2.3.0"
    )
).dependsOn(common % "compile->compile;test->test")

lazy val suite = project.in(file(".")).aggregate(common, app)
4

1 回答 1

1

我相信我找到了使用导出资产的正确方法。在我的 Play 视图中将它们视为 webjar,我走错了路;Sbt-Web 文档在这方面对我有点误导:

资产以 webjar 格式导出,并以与其他 webjar 依赖项相同的方式导入。

事实证明,导出的资产在两个地方可用,(1)app/target/web/web-modules/main/webjars/lib/common在我的问题中描述,(2)在app/target/web/public/main/lib/common. 我无法使用 webjar-play 插件访问前者,但后者可以作为普通资产访问,例如

<link rel="stylesheet" media="screen" href="@routes.Assets.at("lib/common/css/directives.css")">

据我所知,这也适用于生产。

于 2014-10-29T08:28:21.913 回答