简体   繁体   中英

Configure Play! 2.0 to copy html files from assets folder to public folder?

I'm currently building a website using the Play! 2.0 (2.1-RC2) framework and Scala. I have a bunch of "static" html files that define my views used by AngularJS (I'd much rather use Jade, but I can't seem to find a way to get Jade to be precompiled with Play/Scala.. which makes sense, since Jade templates usually aren't static).

What makes the most sense for me, as a developer, and my project's structure is to have these html files in the same place as my coffee and less files (/assets/*). I understand that Play wants me to put them in my public folder, however I have a hard time with that. To me, the public folder should be for libraries and generated files.

What I'd like to do is somehow get Play to copy the html files in /assets/views to /public/views as part of the build. How could I accomplish this?

Many thanks in advance!

如果将静态html文件放在public / htmls下,则可以通过以下方式在模板中获取单个文件的路径:

@routes.Assets.at("htmls/yourfile.html")

I was able to figure out how to do this in a Scalatra test project, so I just need to modify the following code to adapt it to Play's folder structure. It does, however, work. What it does is defines a new sbt "Plugin" and, at compile time, copies any files in /src/main/html to /resource_managed/main/views.

Hopefully someone will find this useful!

import sbt._
import Keys._
import java.io.File
import org.apache.commons.io.FileUtils._

object CopyViews extends sbt.Plugin {
  import CopyViewsKeys._

  object CopyViewsKeys {
    val copy = TaskKey[Unit]("copy-views", "Copy views into resourceManaged.")
  }

  private def copyViewsTask = (streams, sourceDirectory in copy, resourceManaged in copy) map {
    (out, source, destination) =>
      out.log.info("Copying Views to " + destination.getAbsolutePath())
      copyDirectory(source, destination)
  }

  def copyViewsSettingsIn(c: Configuration): Seq[Setting[_]] =
    inConfig(c)(Seq(
      sourceDirectory in copy <<= (sourceDirectory in c) { _ / "html" },
      resourceManaged in copy <<= (resourceManaged in c) { _ / "views" },
      copy <<= copyViewsTask
    )) ++ Seq(
      compile in c <<= (compile in c).dependsOn(copy in c)
    )

  def copyViewsSettings: Seq[Setting[_]] = 
    copyViewsSettingsIn(Compile)
}

object ModFallBuild extends Build {
  import CopyViews._ // Import in our Build so we can use in our build.sbt file.

  lazy val modfall = Project("modfall", file("."))
}

Now, in our build.sbt file we can add

seq(copyViewsSettings:_*)

And the views are now copied at compile time :)

There is probably an easier way to do this, I'm sure, but this works for me and was able to let me get an understanding of building a sbt plugin! :P

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM