playframeworksbtwebjarssbt-web

How to reference exported assets in a multi-module play project


I have a multi-module Play Framework project with two modules, common which is a standard project containing reusable code and assets, and app which is the play project I am trying to run. Using the Sbt-Web plugin, I am able to create and modify assets in common/src/main/public which are copied into the folder app/target/web/web-modules/main/webjars/lib/common each time the app reloads. My issue is with referencing these assets in my Play views. My routes file includes the lines

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

and I'm trying to access these exported assets in my index.scala.html file in app by e.g.

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

This file does exist at app/target/web/web-modules/main/webjars/lib/common/css/directives.css but WebJarAssts.locate returns

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

I have tried specifying more and less of the path and also to access it as a standard asset via Assets.at, but no luck. Does anyone know how to reference exported assets properly?

Relevant lines from my build.sbt file are

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)

Solution

  • I believe I found the proper way of using my exported assets. I was on the wrong track with treating them as webjars in my Play views; the Sbt-Web documentation was a bit misleading to me in this respect:

    The assets are exported in the webjar format and are imported in the same way as other webjar dependencies.

    It turns out that the exported assets are available in two places, (1) in app/target/web/web-modules/main/webjars/lib/common as described in my question and (2) in app/target/web/public/main/lib/common. I could not access the former using the webjar-play plugin but the latter is accessible as an ordinary asset by e.g.

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

    As far as I can understand, this will also work in production.